Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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.

  • You cannot reply to this topic
3 replies to this topic

#1 gfxgangsta   Members   -  Reputation: 608

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?

Thanks in advance

Sponsor:

#2 french_hustler   Members   -  Reputation: 382

Like
3Likes
Like

Posted 24 July 2012 - 10:29 AM

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.

#3 Rld_   Members   -  Reputation: 1498

Like
0Likes
Like

Posted 24 July 2012 - 01:02 PM

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

#4 Farraj   Members   -  Reputation: 152

Like
0Likes
Like

Posted 24 July 2012 - 01:05 PM

How about this:

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 :P

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