PDA

View Full Version : Moving a object along a path.

nexusone
02-24-2002, 06:09 AM
I having a object move from point a to point b, at a set rate.

Here is the function that I am using, but it dances when it gets to point b.
Is there maybe a better way to do this?

currnet_x = current_x +(0.1 * (current_x < target_x ) + (-0.1 *(current_x > target_x ))
currnet_y = current_y +(0.1 * (current_y < target_y ) + (-0.1 *(current_y > target_y ))
currnet_z = current_z +(0.1 * (current_z < target_z ) + (-0.1 *(current_z > target_z ))

DFrey
02-24-2002, 06:37 AM
The method you are currently using does not move it a constant rate, as it appears to be independent of time. So that code on other computers would run at a different speed. The dancing around you are seeing is because of small floating point errors. To eliminate that simply clamp the position to the target when it passes it. Or better yet, simply use a parametric method that linearly interpolates between the current and final destinations, i.e. X=Xc+(Xf-Xc)t, where Xc is the current position, and Xf is the final position, and t is a time based parameter that runs from 0 to 1. At t=0, X=Xc and at t=1, X=Xf which is exactly what you say you want.

Jan
02-24-2002, 12:08 PM
Your method is really not perfect. I think the dancing appears because your object never really reaches the destinationpoint. Because of floating-point errors (as mentioned before), your object never really reaches the point, but it appears to be a bit in front of it (say 0.05 units) and after the next step it is a bit behind it (0.05 units behind it). So you tell it to go forward, if it is in front of it and you tell it to go back if it is behind. So it is obvious why it dances.

Use another method. That one mentioned earlier is really better.

Jan.

nexusone
02-24-2002, 05:26 PM
This is only part of the routine, it is located in a glutTimerEvent. Which updates the object every 10ms.

I have thought about added a extra statement that when the current = target to stop calling that routine.

I will try out your function and see how it works. Just have to adjust my timing routine to use it.

Originally posted by DFrey:
The method you are currently using does not move it a constant rate, as it appears to be independent of time. So that code on other computers would run at a different speed. The dancing around you are seeing is because of small floating point errors. To eliminate that simply clamp the position to the target when it passes it. Or better yet, simply use a parametric method that linearly interpolates between the current and final destinations, i.e. X=Xc+(Xf-Xc)t, where Xc is the current position, and Xf is the final position, and t is a time based parameter that runs from 0 to 1. At t=0, X=Xc and at t=1, X=Xf which is exactly what you say you want.

DFrey
02-24-2002, 05:35 PM
You can't (well you can but I wouldn't advise it) test for current==target due to the floating point errors and aliasing errors. A parametric algorithm is the way to go.

Furrage
02-25-2002, 05:47 AM
I would test for object being near the target. Something like if (((Current - Target) < SMALLDIST) &#0124; &#0124; ((Current - Target) > -SMALLDIST) Stop(). SMALLDIST would be an acceptable distance from target to stop. The object stops near the target instead of at it.

Alternatively if you want to stop on the target check if your move would lead to overshoot and if it does set current to target.

nexusone
02-25-2002, 06:27 AM
I tried to current == target, my other thought was to round off the number, leaving a whole number. Then again maybe the dancing is a good effect, I am working on writing my first GL demo....

http://www.opengl.org/discussion_boards/ubb/smile.gif

Originally posted by DFrey:
You can't (well you can but I wouldn't advise it) test for current==target due to the floating point errors and aliasing errors. A parametric algorithm is the way to go.