# Physics for space game [duplicate]

I am making a space game where you fly a ship.

I am trying to work out the physics for such a game and so far came up with:

var thrust; //between 1 & 100 how much the engines are pushing
var mass; // the mass of the spaceship
var speed; //the speed at which the spaceship is moving


Now big ships will have more mass to therefore take longer to speed up and longer to slow down once the thrust is off. Big ships will be slow because of the mass.

Small ships will speed up faster and slow down faster but also go faster.

How will I implement this into a game? I can do the keyevents, if up is pressed for 5 frames for example will I make the thrust raise to by 10 each frame(example) and then increase the speed of the ship based on some formula of the mass & thrust?

This is the game: Only UI and menus made but wan to get an idea of my physics before I implement the movement http://www.taffatech.com/DarkOrbit.html

-

## marked as duplicate by Byte56♦, Sean Middleditch, msell, Anko, Nicol BolasJun 3 '13 at 16:34

It's the same for implementing the physics of any other game. You'll likely even have an easier time as you may not implement gravity or friction. – Byte56 Jun 2 '13 at 21:00

First of all, you need to learn the basics of vectors. Just find a good tutorial. It will not take long. Try this.

For simulating a physical object:

Newton's first law of motion: "An object at rest remains at rest unless acted upon by a force. An object in motion remains in motion, and at a constant velocity, unless acted upon by a force."

What this means is that it takes no energy for an object to remain at its current velocity (linear and angular). It only takes energy to change the velocity. (Note: speed is the magnitude of velocity and is a scalar). This energy can come from thrusters, gravity, explosions, collisions, and even light falling on it.

Newton's second law of motion: "The acceleration of a body is directly proportional to, and in the same direction as, the net force acting on the body, and inversely proportional to its mass. Thus, F = ma, where F is the net force acting on the object, m is the mass of the object and a is the acceleration of the object."

This means that we can calculate the acceleration on a body if we know the total force acting upon it, and its mass.

Newton's third law of motion: "When one body exerts a force on a second body, the second body simultaneously exerts a force equal in magnitude and opposite in direction to that of the first body."

I won't deal with collisions here, but this means that in a collision both bodies receive the exact same collision force in the same position, but in opposite directions.

Once you can calculate acceleration, you can integrate it to find velocity, and integrate velocity to find position.

If your object looks like this:

struct PhysicalObject
{
vec3 _position;
vec3 _velocity;
vec3 _total_force;
float _mass;
}


Your update function should look something like this:

void update(float _DeltaTime)
{
this._velocity += this._total_force * (_DeltaTime / this._mass); // a = F/m
this._position += this._velocity * _DeltaTime; // simplectic Euler integration
this._total_force = vec3(0.0,0.0,0.0); // clear force accumulator
}


As you can see, if there is no external force, then the velocity will not change, but it is still added to position every frame, thus fulfilling first law. In space, there is no friction slowing the ship down so it would continue along its path forever (unless you want to go through a nebula or something).

This update function uses Euler integration, which is the simplest, but also the most inaccurate. Presumably this will be no problem, but if you need more accuracy, you can either decrease the timestep and do more update iterations per frame, or else look into a better integrator, such as Runge-Kutta.

You will also need analogous data and calculations to deal with rotational stuff, but I leave that as an exercise for the reader. It's a bit more complicated however. For example, the rotational equivalent for mass is a matrix rather than a scalar. :-/

-

So, there is no gravity? If you give some speed to object, it moves forever? What about this:

// values can be vectors
var speed = 1;
var mass = 1;
var position = 0;
function speedUp(d) { speed += d/mass; }  // change speed by d
function move() { position += speed; }

-
I'm very new to this, the only game I made before this was snake, and that was 4 days ago :P would position be the x y value of the ship? Also about rotating the ship how wold I calculate the new x/y values :/ – Wayne Jun 2 '13 at 21:02
You must learn some basic linear algebra and goniometry. – Ivan Kuckir Jun 2 '13 at 21:03