Player Movement In Classic Asteroids Arcade Game

As noted in my previous post First clone picked: Asteroids the first classic arcade game I am going to clone will be Asteroids.

In Asteroids the player controls a triangle shaped space ship. The basic controls are rotate left, rotate right and accelerate in the direction the spaceship is currently pointed.
The spaceship will then continue to travel in the direction it is pointed at as long as the engines are still on. Once the engines are turned off the spaceship will slowly come to a halt.
If the player pilots the spaceship off the edge of the screen it will reappear on the other side.

So on paper this all sounds pretty easy. The question is how do we program this behavior?
There are several properties we will need to keep track of to enable us to make our spaceship move as described above. They are as follows.

Thrust
We the player activates the spaceships engines it generates thrust. This variable controls how fast the spaceship will accelerate when the engines are activated.

Maximum Thrust
If the amount of thrust is not capped the players spaceship would continue to accelerate as long as the engines are active. This variable will allow us to place a cap on the maximum speed the player can move at.

Rotation speed
The player can rotate their spaceship, this variable places a cap on how fast they can rotate left or right.

Friction
In Asteroids there is a friction component to the players movement. If the engines are turned off the player will drift for a short distance before coming to rest. This variable controls how fast the player slows down if they do not have the engines running.

X and Y Velocity
These variables control how fast and what direction the players spaceship will move in the current game update. They are calculated from the rotation variable, thrust variable, maximum thrusts variable and the friction variable.

So the basic flow of calculating the players movements are
If the engines are on:
Calculate the current x and y velocities.
Cap the x and y velocities if they are greater than the allowed maximum thrust
Else
Apply friction to the spaceships current x and y velocities.

Move the spaceship by the current x and y velocities.

If the spaceship has left the bounds of the screen:
Wrap the spaceship to the other side.

Calculating the current x and y velocities
Calculating the current velocities can be achieved by constructing a right angled triangle and using
Pythagorean theorem to solve for a hypotenuse with a length of 1 unit. To make this a bit easier to explain I have whipped up a quick diagram in paintbrush.

Pythagorean Theorem

The red triangle is our spaceship.
The pink arc is our current rotation.
The black line with the arrow is our hypotenuse with a length of 1 unit.
The blue line is the length we ant to solve for our x velocity.
The green line is the length we want to solve for our y velocity.

Pythagorean theorem for a right angled triangle states that the sine of the angle of our spaceships rotation will equal the length of the blue line divided by the hypotenuse.
Since our hypotenuse is 1 we can simplify it in this case to the length of the blue line is equal to the sine of the spaceships rotation.

So to calculate our x velocity for 1 unit we simply do x velocity = sine(rotation)

For the y velocity it is a very similar deal except instead of the sine we use the cosine.
Again our hypotenuse is 1 and we can simplify the equation down to y velocity = cosine(rotation)

We have now calculated the x and y velocity for 1 unit of travel. Since our spaceship is moving on a screen 1 unit of travel is 1 pixel. Moving by 1 pixel every game loop if not that fast. So we simply multiply them by the thrust value. Which will give us the following.

x velocity = sine(rotation) * thrust
y velocity = cosine(rotation) * thrust

So we can now work out the velocity for the thrust for the spaceships current rotation. However we are missing one more part before we can use these velocities. They have not taken into account what velocity the spaceship is all ready moving at. Imagine that you fly up the screen and turn the engines off. The spaceship will gently continue to coast up the screen. You could then rotate the ship to point down and fire the engines. It would instantly take off down the screen instead of slowing down and then start to accelerate down the screen.
Luckily this is very easy to take into account. We simply add the new velocities to the old velocities.

Old x velocity += sine(rotation) * thrust
Old y velocity -= cosine(rotation) * thrust

Notice that I have subtracted the new y velocity from the old velocity. This is because computer screens have the (0,0) location in the top left hand side and not the bottom left hand side. So to go up you need to use subtraction and to go down you need to use addition.

The next step is to cap the maximum speed of the players spaceship.
This is step is easy.
If x velocity > maximum thrust:
x velocity = maximum thrust

If y velocity > maximum thrust:
y velocity = maximum thrust

If x velocity < -maximum thrust:
x velocity = -maximum thrust

If y velocity < -maximum thrust:
y velocity = -maximum thrust

Applying friction to a drifting spaceship is also very easy too.
x velocity = x velocity * friction
y velocity = y velocity * friction

Finally updating the players position and then wrapping the players spaceship around the screen is a simple task of checking if they have crossed a screen boundary and if so setting them to the other side.

x = (x + xVelocity);
y = (y + yVelocity);

if(x < 0)
{
x = getWidth();
}

if(x > getWidth())
{
x = 0;
}

if(y < 0)
{
y = getHeight();
}

if(y > getHeight())
{
y = 0;
}

Finally if none of the above made any sense you can check out a basic demo of the ideas in action below. You will need Java installed to play it. The left and right arrow key rotate the space ship and the up key turns the engines on.
The full source code is also provided. You will need to download the Pulpcore API to build it. I used version 0.11.3 for this example. Asteroids Movement Source Code

Play the Asteroids Movement Demo



Related Posts:
Asteroids: First Playable Release
It has been a while since I posted about cloning the arcade classic Asteroids So...
Asteroids Movement Demo
A demo applet showing how to program Asteroids style movement. ...
Asteroids
Asteroids is a classic arcade game developed and published by Atari in 1979 and is...
First clone picked: Asteroids
Asteroids an arcade classic, published in 1979 by Atari. Sporting some simple black...
It begins
Welcome to the launch of Cloning The Classics a website dedicated to the creation of...