# Move object to desired location

## Recommended Posts

Hi,

I have an object that I'd like to move around to different, pre-determined locations in 3d space. I have a velocity vector which I multiply by delta time, to get the distance I should travel during one frame. The way I'm checking if I'm already at the desired location is to compute the distance to that point. If the distance is less than some specific value, then I stop moving. But that value is tied to delta time, and, if the game slowed down enough, the distance traveled could overshoot the checked distance value, and the object would just keep going. Is it better to just see if the object has moved past the desired point, in the direction of the velocity vector, and then stop? What other methods are used in games?

##### Share on other sites
I have used this article to understand what's going on (and it has become almost the standard reference it seems like):
[url="http://gafferongames.com/game-physics/fix-your-timestep/"]http://gafferongames.com/game-physics/fix-your-timestep/[/url]

Also, member L. Spiro has a good article:
[url="http://lspiroengine.com/?p=378"]http://lspiroengine.com/?p=378[/url]

These should explain to you how to code your simulation so that it is independent of the frame rate of your game.

##### Share on other sites

But to still answer your question, the most simple way of doing this is what you already suggested partially yourself. Check if it is already past your desired point and stop, although in most cases, this will lead to undesired behavior (like a bouncing ball that will get stuck in the ground), so in addition to that, you might want to move it back to the position of the previous frame:

[CODE]
vector3 pos(0, 0, 0);
vector3 vel(0, 5, 0);
void simulate()
{
float timestep = 1/60;
vector3 lastpos = pos;
pos += vel;
if(pos.y > desiredpointordistance)
{
pos = prevpos;
}
}
[/CODE]

above is a semi pseudocode that shows how. Obviously it would be even better to be able to predict exactly where it should end up, and you can, but this is in comparison an expensive calculation. Check out my [url="http://blog.joeyfladderak.com/2011/10/lets-talk-physics-ballistic-motion/"]blogpost [/url]if you want to see it, it's at the bottom of the page

##### Share on other sites

[sub]distance_travled = velocity_vector * dt;[/sub]
[sub]raminign_distance = finial_poition - current_poition;[/sub]
[sub]if(raminign_distance > distance_travled)[/sub]
[sub] // move forward[/sub]
[sub]else if(raminign_distance <= distance_travled)[/sub]
[sub] // move the remaining distance, then stop[/sub]

[sub]Just simplfy. Think lazy. Think like a programmer [/sub]

[sub]Cheers ^^[/sub]