Skip to main content
GameDev.net gamedev.net
🔒 Locked

Motion equation with gravity

Started by doctorsixstring Aug 4, 2008 at 10:33 PM 4 replies 2.7k views
Original Post
doctorsixstring
doctorsixstring
Given the motion equation (d = v*t + 0.5*a*t2), I can find a vehicle's position after a given passage of time with a given starting velocity and constant acceleration. Solving this equation for two dimension allows me to calculate spaceship movement in a Gravity Wars-style game. That same formula can also be used by missiles that the ships will shoot at each other. Now I want to throw in some gravity wells (planets, black holes, etc.). How can I calculate the final position of a missile or ship at a given time, with an initial velocity, acceleration, and one or more sources of gravity? Is there a formula for calculating the trajectory of a vehicle with those parameters? I know I could simply apply gravity to the ship's position each game tick, but that would yield different results depending on the length of each tick. I would really like a solution that yields identical results regardless of the change in time. Any thoughts? - Mike
scratt
scratt
When I do this I sum all the gravity vectors each frame and then apply that to the target object.

To solve the timing issue you refer to I simply scale the composite gravity effect against the length of the interval between frames.

I am using this method for a large number of dynamic objects and it's pretty efficient. If you wanted to get really clever you could use some AltiVec or something on well organized sets of values.
Feel free to 'rate me down', especially when I prove you wrong, because it will make you feel better for a second....
Hodgman
Hodgman
Quote:
Original post by doctorsixstring
Given the motion equation (d = v*t + 0.5*a*t2), I can find a vehicle's position after a given passage of time with a given starting velocity and constant acceleration.
As you've stated, keep in mind this formula only works when acceleration remains constant. Obviously a game with constant acceleration would be pretty boring, so this formula isn't exactly correct - it's still correct for the duration of one time-step, because in our simulations we update the input forces (i.e. update the acceleration) only once per frame. So for the duration of a frame we can say that the acceleration remains constant.
Quote:
Now I want to throw in some gravity wells (planets, black holes, etc.). How can I calculate the final position of a missile or ship at a given time, with an initial velocity, acceleration, and one or more sources of gravity?
The above forumula remains the same, you just also want to add in the formula f=m*a (or acceleration = force / mass). Gravity is just another force being applied to your objects, so it is going to affect the acceleration which is fed into your first formula.
Quote:
I know I could simply apply gravity to the ship's position each game tick, but that would yield different results depending on the length of each tick. I would really like a solution that yields identical results regardless of the change in time.
Welcome to the world of numerical integration ;)
Games (almost?) always use approximate methods for integration, such as Euler's method or verlet integration. Usually these approximations are stabilised by enforcing a fixed time-step.

For example, always use a delta-time value of 10ms. If 35ms have passed since the last update, then you run the integrator 3 times, and add 5 onto a 'leftover' variable which will be added onto next frames delta value.

Alternatively, my Verlet link above describes a "time corrected" method that remains stable even when the time-step varies.
doctorsixstring
doctorsixstring
Thanks for the replies, guys.

I'll go ahead and start working on using fixed-length time steps, and running one or more updates per frame, depending on frame length. I'll apply gravity, ship thrust, and other external forces as a summed acceleration vector at the start of each frame.

Since we've established that it would be extremely difficult to create a single formula, I won't be able to use it to solve for vehicle thrust with a desired final position.

So in order to calculate power and angle of launched missiles, it sounds like I'll need to use brute force to calculate a solution. I will calculate each frame's update logic ahead of time and see where the missile will end up. If it misses, I'll keep re-running the trajectory calculation for a slightly changed power/angle until the missile hits. This seems pretty computationally expensive, but I don't see any other way.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.