# 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.

## 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 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 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!

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632950
• Total Posts
3009385
• ### Who's Online (See full list)

There are no registered users currently online

×