Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying my hand at creating a simple 2d physics engine right now, and I'm running into some problems figuring out how to incorporate momentum into movement of a spaceship.

If I am moving in a given direction at a certain velocity, I am able to currently update the position of my ship easily (Position += Direction * Velocity). However, if the ship rotates at all, and I recalculate the direction (based on the new angle the ship is facing), and accelerate in that direction, how can I take momentum into account to alter the "line" that the ship travels? Currently the ship changes direction instantaneously and continues at its current velocity in that new direction when I press the thrust button. I want it to be a more gradual turning motion so as to give the impression that the ship itself has some mass.

If there is already a nice post on this topic I apologize, but nothing came up in my searches. Let me know if any more information is needed, but I'm hoping someone can easily tell me how I can throw mass * velocity into my game loop update.

share|improve this question
    
If you are looking into more like how to program the motion, you may want to look at the free game Transcendence, another 2d space shooter that uses that. Maybe you can look at its scripts or ask the developers how they did it –  Mastersteviekun Nov 29 '11 at 6:49

2 Answers 2

up vote 6 down vote accepted

It looks like you have Position and Direction as vectors and Velocity as a scalar?

If so just change Velocity into a vector as well and then do something like this:

Force = Direction * Power
Acceleration = Force / Mass
Velocity += Acceleration * ElapsedTime
Position += Velocity * ElapsedTime

Direction being a unit length vector giving the heading of your ship. Power being the amount of thrust you want to add (Zero when no acceleration is needed) ElapsedTime is the time between your game Update calls to keep movement smooth even when your framerate is changing

If you want to know more about it you can look up Euler Integration

share|improve this answer
1  
Yep and the next step up is to model Newtons Law F = m*a. So instead of using a "Power" constant to get acceleration you use Acceleration = Force / mass. So when you apply thrust, you apply it as a force vector. Doing this will allow you to apply other forces to your spaceship easily by adding all the force vectors together and calculating the corresponding acceleration. Also means you can play with Mass as a separate variable. –  TerryB Nov 29 '11 at 5:16
    
Terry is correct, I forgot to incorporate that in my original post. –  Mr Bell Nov 29 '11 at 5:19
    
Thanks Mr. Bell/Terry. Both equations listed above helped me programatically do this. I was having a lot of trouble understanding how this would work trying to use floats all over the place. After switching everything but mass to vectors (thanks to leftium above), I was able to finally get things working. –  Josh Petite Nov 30 '11 at 2:04

Instead of manipulating the velocity directly, add another variable: acceleration, that gradually, smoothly alters the velocity:

From classical physics (Newton's laws of motion):

a =  F/m  // a Force will result in a smaller acceleration on objects with more mass
v += a    // acceleration is the rate of change in velocity
p += v    // velocity is the rate of change in position    

where:

a = acceleration, v = velocity, p = position
F = force, m = mass

(Note m is the only scalar value; F, a, v, and p are all 2D or 3D vectors)

Also, there are actually two directions:

  1. The direction the ship is facing. Force from engines is applied along this vector.
  2. The direction the ship is actually moving (due to momentum). This is the velocity vector v.

I answered a related question on StackOverflow: 2D Spaceship movement math. There is some sample code in that answer.

Also you can check out my asteroids demo, which implements the algorithm described above.

share|improve this answer
    
Note that in a tick-based simulation using that method is going to give you incorrect results. The base formulas are correct in the continuous world of physics, but you'll need to do numerical integration to get you accurate results. More details here: gafferongames.com/game-physics/integration-basics –  Tetrad Nov 29 '11 at 16:04
    
Finally got this working. Thanks very much everyone. It took awhile to figure out how to parse this up input event wise along with my update (as well as incorporating the usage of time). –  Josh Petite Nov 30 '11 at 2:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.