Sign in to follow this  
gfxgangsta

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?

Thanks in advance

Share this post


Link to post
Share on other sites
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):
[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 this post


Link to post
Share on other sites
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:

[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 this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this