# how to correctly make a linear animation towards a target

This topic is 3658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, i want to have an object move towards a position in 3D space and stop exactly at that position. so what i do is, each frame i set the new position of the object along the direction-vector to the target multiplied by it's speed and the duration of the last frame. once it is near enough to the target (below a certain epsilon), the animation stops. this approach seems to have two problems: 1. if the framerate is very low, it is possible, that the animated object "overshoots" the destination. 2. if the animation stops, the object is probably not at the very exact position, but rather somewhere in between the epsilon. i could of course just set the position to the destination if the object is in this epsilon, but that leads to a jump if the epsilon is large and/or the speed is slow. i think if problem 1 is solved, problem 2 will solve automatically. thanks for any help on these issues!

##### Share on other sites
If the animation starts at t0, and the distance from the start position to the target position is D, and the speed of the object should be v, then the target position is reached exactly at
t0 + T where T := D / v

Now, the video framerate will in general not allow you to hit t0 + T exactly. Instead, you need to render the frame at ticks
t0, t1, t2, ..., tn up to tn <= t0 + T and tn+1 > t0 + T

The corresponding positions are
p(t) := p0 + v * ( t - t0 )
where p0 denotes the start position, and v the velocity vector computed like you've said in the OP, until t0 + T is exceeded. If it is exceeded, then you use the target position as is.

Notice please that this method computes the positions due to the actual time moment, and not due to the duration of the last recent video frame. So it adapts to the current frame rate without looking into the history of motion.

##### Share on other sites
thanks a lot haegarr!

that solved both my problems!

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 10
• 9
• 14
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007295

×