Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
IF/Else and Collision
Old
Sharpie
Junior Member
 
Status: Offline
Posts: 12
Join Date: Mar 2014
Default IF/Else and Collision - 04-10-2014, 09:24 PM

Greetings Alice Community, have a couple of questions if anyone is willing to help!
  1. 1 How do I get the "distance to" function to work in relevance to another object in Alice?
  2. 2 How do I get the "within distance" function to work inside an If/Else statement without canceling or ignoring the IF code?
  3. 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!

Last edited by Sharpie; 04-10-2014 at 09:29 PM.
   
Reply With Quote
Hi, here's one way!
Old
jrrTolkien
Senior Member
 
jrrTolkien's Avatar
 
Status: Offline
Posts: 166
Join Date: Feb 2014
Location: Middle-Earth (Merrimack, NH)
Cool Hi, here's one way! - 04-11-2014, 12: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
   
Reply With Quote
Also
Old
MrMoke
Senior Member
 
MrMoke's Avatar
 
Status: Offline
Posts: 501
Join Date: Sep 2007
Location: Austin, Texas
Default Also - 04-11-2014, 02: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.
   
Reply With Quote
Old
Sharpie
Junior Member
 
Status: Offline
Posts: 12
Join Date: Mar 2014
Default 04-12-2014, 02:14 PM

Quote:
Originally Posted by jrrTolkien View Post
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.



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


Last edited by Sharpie; 04-12-2014 at 02:44 PM.
   
Reply With Quote
It's Tricky, but only the first time.
Old
MrMoke
Senior Member
 
MrMoke's Avatar
 
Status: Offline
Posts: 501
Join Date: Sep 2007
Location: Austin, Texas
Smile It's Tricky, but only the first time. - 04-12-2014, 06: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.
   
Reply With Quote
Example
Old
jrrTolkien
Senior Member
 
jrrTolkien's Avatar
 
Status: Offline
Posts: 166
Join Date: Feb 2014
Location: Middle-Earth (Merrimack, NH)
Cool Example - 04-17-2014, 03: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
Attached Files
File Type: a2w 8bitfun.a2w (246.8 KB, 12 views)
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



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