8
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Nov 29, 2011 at 6:49

2 Answers 2

7
\$\begingroup\$

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

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – TerryB
    Commented Nov 29, 2011 at 5:16
  • \$\begingroup\$ Terry is correct, I forgot to incorporate that in my original post. \$\endgroup\$
    – Mr Bell
    Commented Nov 29, 2011 at 5:19
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 30, 2011 at 2:04
8
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$
    – Tetrad
    Commented Nov 29, 2011 at 16:04
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Nov 30, 2011 at 2:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .