Jump to content
  • Advertisement
Sign in to follow this  

moving an object from one point to another in a given time

This topic is 3973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi there, ive been trying to get the maths behind a simple physics problem. i want to move an object by a constant velocity from one point to another. it should be activated at a give time and move from point one to point two in N seconds. with this i mean i have to give that object a parameter that tells it how long it should take from point one to point two AND how many pixels how should walk per frame could anyone help me out with this thanks

Share this post

Link to post
Share on other sites

I assume that you are using "pixels" as units of location. I'd suggest you to use floating point precision or fixed point math since the calculations required here usually will produce fractional results. You can always draw your objects to full pixel locations, but keep the exact real location for the calculations.

Anyway, first of all, you'll need to know the start position and the end position. By subtracting the start position from the end position you'll have a delta vector which tells the distance between the two points.

A = Start position (x,y)
B = End position (x,y)
Delta = B - A

Then you'd divide this delta vector by the desired time to accomplish the distance

MovementPerSecond = Delta / N;

Now comes the question: Do you have a fixed time step system or a system related to the frames drawn per second?

Fixed time step version:

MovementPerFrame = MovementPerSecond / FRAMES_PER_SECOND;

Per frame :

Position += MovementPerFrame;

Variable time step version (simpler to implement, but may result weird behaviour when timestep becomes very small or large):

Per frame :

Position += MovementPerSecond * TimeStep;

//TimeStep is the delta time in seconds between this update and the previous update

Best regards!

Share this post

Link to post
Share on other sites
thanks kauna,
im based on a variable time step. ive been able to get it working just as you said, and i do noticed it reaches the target position with a very small diference. around 0.0012 secs, but i think i can work with this.

about the fixed timestep, i tried using Gaffer's way, but for some reason it never worked that well, so i dumped it..

thanks again for the help!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!