• Create Account

# Move object to desired location

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1gfxgangsta  Members   -  Reputation: 697

Like
0Likes
Like

Posted 24 July 2012 - 10:10 AM

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?

### #2french_hustler  Members   -  Reputation: 424

Like
3Likes
Like

Posted 24 July 2012 - 10:29 AM

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.

### #3Rld_  Members   -  Reputation: 1757

Like
0Likes
Like

Posted 24 July 2012 - 01:02 PM

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

### #4Farraj  Members   -  Reputation: 152

Like
0Likes
Like

Posted 24 July 2012 - 01:05 PM

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

Just simplfy. Think lazy. Think like a programmer

Cheers ^^

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS