Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   IF/Else and Collision (http://www.alice.org/community/showthread.php?t=10577)

Sharpie 04-10-2014 09:24 PM

IF/Else and Collision
 
Greetings Alice Community, have a couple of questions if anyone is willing to help!
[LIST=1][*]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?[/LIST]
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 12:18 PM

Hi, here's one way!
 
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 02:30 PM

Also
 
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 02:14 PM

[QUOTE=jrrTolkien;54225]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 ;)[/QUOTE]

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.

[IMG]http://i58.tinypic.com/2ekuj9s.jpg[/IMG]

@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?

[IMG]http://i59.tinypic.com/34xnz9h.jpg[/IMG]

MrMoke 04-12-2014 06:50 PM

It's Tricky, but only the first time.
 
Ifs and Whiles require a Boolean response (true or false), so dragging in any logical test satisfies the start, even (1>1).
Dragging a [U]numeric[/U] 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 [U]negligible[/U] additional coding. You just replace say "B" with a reference to a list item.

jrrTolkien 04-17-2014 03:50 PM

Example
 
1 Attachment(s)
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


All times are GMT -5. The time now is 02:29 AM.

Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.