Move object to desired location

Started by
2 comments, last by Farraj 11 years, 9 months ago
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?

Thanks in advance
Advertisement
Your game simulation should not be dependent of your game speed.
I have used this article to understand what's going on (and it has become almost the standard reference it seems like):
http://gafferongames.com/game-physics/fix-your-timestep/

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

These should explain to you how to code your simulation so that it is independent of the frame rate of your game.
Both the links by french_hustler are good reads.

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:


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;
}
}


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 blogpost if you want to see it, it's at the bottom of the page
[sub]How about this:[/sub]

[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 :P[/sub]

[sub]Cheers ^^[/sub]

This topic is closed to new replies.

Advertisement