PDA

View Full Version : IF/Else and Collision

Sharpie
04-10-2014, 10:24 PM
Greetings Alice Community, have a couple of questions if anyone is willing to help!

1 How do I get the "distance to" function to work in relevance to another object in Alice?
2 How do I get the "within distance" function to work inside an If/Else statement without canceling or ignoring the IF code?
3 How can I keep multiple random-moving objects from colliding into each other by using "distance to" function?

Pretty much puzzled over these IF/Else statements. The coding looks logical but each time I run they end up canceling out. Please help! :eek:

jrrTolkien
04-11-2014, 01:18 PM
One way is if you drag in an if/else, then say if (object) is within .5 meters of (other object), do nothing, else (move forward). If you are still confused, I can make an Alice world as an example and post it up here.

-jrr ;)

MrMoke
04-11-2014, 03:30 PM
Also remember that each object has a origin-point, distances are measured between those single points, and that the points aren't always in the middle. For example, drag an Egyptian into a scene, and then a simple sphere. If you then tell the sphere to set it's "point of view" to the Egyptian, it's center will move to the Egyptian's feet. Now shrink the ball to knee height.
So, if you are checking for a distance that is less than the height of the Egyptian, the ball could easily pass through the head, but never come close to the feet.
The problem isn't as pronounced with some larger objects like buildings, but can be a challenge.

Try an experiment:
1) Drag two spheres of "different" sizes into a scene(maybe on left and right sides of view screen).
2) Build a loop of your choice that moves the balls toward each other in small measured increments while the distance between the two is > the sum of the two radii.
3) If they stop when they touch, you win.;)

It might sound like a lot of work, but if you have a large "list" of different objects of differing sizes, it might come in handy.

Sharpie
04-12-2014, 03:14 PM
One way is if you drag in an if/else, then say if (object) is within .5 meters of (other object), do nothing, else (move forward). If you are still confused, I can make an Alice world as an example and post it up here.

-jrr ;)

Would be great if you could provide an example! I have a similar code that i've been working on. The bunny object ignores the spring in the if segment and keeps on hopping in the else instead.

http://i58.tinypic.com/2ekuj9s.jpg

@MrMoke

Thanks Moke! I've tried your suggestion but I got lost in the math function part. When I tried to drag and drop it it from World menu the function wasn't green and it wouldn't let me select it. Is there a part that I am missing like a placeholder?

http://i59.tinypic.com/34xnz9h.jpg

MrMoke
04-12-2014, 07:50 PM
Ifs and Whiles require a Boolean response (true or false), so dragging in any logical test satisfies the start, even (1>1).
Dragging a numeric function like "distanceTo" into either side gives one numeric value, so A.distanceTo(B) > 1.
Use the down arrow on the opposite side, select math and change it to 1+1, so A.distanceTo(B) > ( 1 + 1).

Lastly, use the same technique to replace the ones with A.getWidth()/2 and B.getWidth()/2, so

while(A.distanceTo(B) > ( (A.getWidth()/2)+(B.getWidth()/2) )

Probably seems like a lot of work, but if you have a routine that's constantly checking one object's distance to a list or array of 20 others, it requires negligible additional coding. You just replace say "B" with a reference to a list item.

jrrTolkien
04-17-2014, 04:50 PM
Here's an example for you. This is a little world I made. Take a look at it, but it's a little sloppy. You should still be able to read it, though.

-jrr