Sign in to follow this  

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

This topic is 3668 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
Hi,

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;

//Where
//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

This topic is 3668 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.

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