Alice Community Problem with distance
 User Name Remember Me? Password

 Thread Tools Display Modes
 Problem with distance
 jdeisenberg Guest   Status: Posts: n/a Problem with distance - 02-11-2008, 04:22 PM I have a world set up with the island/palmtree, a penguin, and a fish. I want the penguin to be halfway between the tree and the fish (or very close to it), so I set up this loop: Code: ```penguin.move to palmtree penguin.turn to face fish while penguin.distance to fish != penguin.distance to palmtree penguin.move 0.1 meter towards fish``` Yes, I know that the penguin moves through the island. That doesn't bother me. What is worrisome is that the penguin moves all the way to the fish and starts bouncing back and forth. I am making some incredibly simple error, but for the life of me I cannot see what it is. The world is at http://evc-cit.info/alice/halfway.a2w
 jdeisenberg Guest   Status: Posts: n/a 02-11-2008, 04:39 PM Argh. Never mind; I figured it out by having the penguin "say" its distance from the fish and the tree. Here's the problem: since they are never exactly equal, the penguin will keep moving until it is very near the fish. The next move takes the penguin to the other side of the fish, so it has to move back. They're never exactly equal. The correct way to do it is: Code: ```penguin.moveto palmtree penguin.turn to face fish penguin.move (penguin.distance to fish / 2) towards fish```
DickBaldwin
Guest

Status:
Posts: n/a
02-11-2008, 04:57 PM

Quote:
 Originally Posted by jdeisenberg Argh. Never mind; I figured it out by having the penguin "say" its distance from the fish and the tree. Here's the problem: since they are never exactly equal, the penguin will keep moving until it is very near the fish. The next move takes the penguin to the other side of the fish, so it has to move back. They're never exactly equal. The correct way to do it is: Code: ```penguin.moveto palmtree penguin.turn to face fish penguin.move (penguin.distance to fish / 2) towards fish```
You might try storing the distance to move in a variable. Initialize that variable to half the distance to the fish. Go into a loop. During each iteration, make one move toward the fish moving the distance that is currently stored in the variable. Then replace the value in the variable by half the distance to the fish before starting another iteration of the loop.

Theoretically, the penguin would never get to the fish. Practically, however, you could allow it to get arbitrarily close before terminating the loop.

To keep the animation from being jerky, cause each move to start and end abruptly.

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

Last edited by DickBaldwin; 02-11-2008 at 05:00 PM.

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     Community News and Announcements Alice 3     How Do I?     Works In Progress     Share Your World     Share Custom Classes     Bugs and Trouble Shooting     Suggestion Box Alice 2     How do I...?     Works-In-Progress     Share Worlds     Share Objects     Bugs and Troubleshooting     Suggestion Box Educators     Teaching with Alice     Teaching Using Alice 3     Teaching Using Alice 2     CS Principles and Alice     AP CSA and Intro to Java and Alice     Workshops     Alice Player and VR General Discussion     Questions and Comments     The Lounge

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

 Contact Us - Alice.org - Archive - Top