PDA

Click to See Complete Forum and Search --> : Calculating position given initial position, velocity and acceleration


SLH
May 23rd, 2007, 11:36 AM
I'm creating a 2D game that supports replays by creatubg keyframes every time the object's properties change not according to a formula.

Currently i'm moving the object using the following formulas (where Timing.dt) is the amount of time passed:

float XChange = Velocity.X * Timing.dt + Acceleration.X * (Timing.dt * Timing.dt) / 2.0f;
float YChange = Velocity.Y * Timing.dt + Acceleration.Y * (Timing.dt * Timing.dt) / 2.0f;
Position.X += XChange;
Velocity.X += Acceleration.X * Timing.dt;
Position.Y += YChange;
Velocity.Y += Acceleration.Y * Timing.dt;

However, what i'd like to be able to do is, when replaying, jump straight to time t, rather than have to continually apply this algorithm in a loop until i get from my keyframe, to the required time. Unless i'm mistaken, if i change Timing.dt the object's position won't end up the same for a given time (i.e. i can't double Timing.dt and do half as many steps).

So i want a formula for the object's position at time t, given it's initial position, initial velocity and acceleration (constant).

Any help is, of course, greatly appreciated.

sunburnt
May 23rd, 2007, 08:04 PM
To find the X position of something at time T, the formula is:

x(t) = x0 + vx0*t + (1/2)a*t2

where:

x0: initial x position
vx0: initial velocity in the x direction
a: acceleration
t: time.

Repeat the forumla with the y components to find the Y position.

Hope this helps!

SLH
May 24th, 2007, 08:24 AM
Thanks, that's what i thought it was, but the fact that somewhere i read that i had to alter the velocity at each time step (which i don't think is the case if i used that formula) i was getting a touch confused.