# How should I implement basic spaceship physics?

I am new to 2D game development.

I've made several games, but they've made almost no use of physics. Now I want to try making a game that uses some basic physics to look more realistic.

The game involves spaceships battling in space. I'm ignoring gravity. Each spaceship can move up, down, left, right or any of the 4 diagonals.

I want each spaceship to accelerate when moved (up to some threshold speed) and gradually slow to a stop otherwise.

I'm planning to give each spaceship a velocity and acceleration attribute.

The acceleration is set to some constant value. For example, 5.

When the spaceship starts moving, the velocity is set to some initial value. Then every period of time as long as it should be moving (for example every 30 milliseconds), the acceleration value is added to it.

When the user stops moving the ship, the reverse process happens: the acceleration value is decreased from velocity until velocity equals 0.

I have two questions regarding this:

1. Is this general type of physics realistic and/or common in games? Is this the right direction? Am I forgetting some factor or anything to be considered?

2. Regarding the specific implementation I'm planning: Is the right way to accelerate is add a constant value to the speed of the object? Or do I need to gradually increase the value added to the speed, thus increasing the acceleration?

Any physics "simulation" is going to deal with forces. Your approach may be basic, but it is definitely a step in the right direction to understanding game physics.

Now you may be able to add to this further. For example, if the player is turning around while moving at a high speed, how do you handle this? Do you prefer they continue along their original direction a bit until they correct to the new one, or should they just snap to the new direction at the same speed?

To answer your second question, let's first understand what acceleration means. For example, is your spaceship like a car where your acceleration is lower the higher your speed is, eventually capping at a certain maximum? Maybe this kind of change would break your gameplay, so these choices are entirely up to how you'd prefer your game to feel.

In real space, there is no friction. When you accelerate a body, it keeps moving in that direction and never stops, unless you accelerate it in the other direction. There are also no top-speeds. Given enough energy, you can accelerate a body to unlimited speeds.

There are games where such realistic physics are part of the gameplay, but the vast majority of games, especially those which focus on action instead of realism, handle this differently and add friction to space.

A simple and frequently used (but not physically correct) way to deal with friction is to have an acceleration value, a top-speed value and a deceleration value.

While the player presses the left/right key, each game-loop the acceleration-value is added to the current speed of its character. That means unless the character has already reached the top-speed.

When the player stops pressing the directional key, the deceleration value is applied in the opposite direction to slow the character down.

Often the acceleration is higher than the deceleration, so the player can break faster by pressing into the opposite direction than just stopping to press any button.

With the engines' propulsion as the only force acting on the spaceship, physics are hardly at play here. Your currently planned implementation are in accordance with 'realistic physics'. You could definitely design a spaceship that flies that way. To answer your second question, that too would be realistic. You could definitely design a spaceship that uses a non-constant force to reach its target velocity.

If you spaceship design is very specific, i.e. you know its mass, where the engines are, how much thrust they can produce and how much fuel they burn, how the controls work..., then you can run a physics simulation to determine exactly how that design is supposed to behave. Without these specifications, who is to say your ship handles incorrectly?

Your constant force-approach is simple to implement. Build it and play the game to see whether it needs tweaking.