Asteroids: First Playable Release

It has been a while since I posted about cloning the arcade classic Asteroids

So I though I would post up my work in progress clone. It is about 80% finished at this point and is an actual playable arcade clone.

So what made it to this first release?

  • Player movement.
  • Player lives and death.
  • Asteroids, movement and splitting.
  • Scores.
  • Sound effect for most actions.
  • Multi language support via Google Translate
  • Flying Saucers

So what is yet to come?

  • Online high scores
  • Player explosion animation
  • Small flying saucers shooting needs to target the player
  • Play testing and tweaking

Since the list of things not in the game does not stop it from being playable I have decided to do a release now and see if I can get any feedback on it. I will release the source code to Asteroids after I have finished up the remaining items on the to do list.

So for now you can have fun and give Asteroids v0.11 a bash.
Play Asteroids



Related Posts:
It begins
Welcome to the launch of Cloning The Classics a website dedicated to the creation of...
Asteroids Movement Demo
A demo applet showing how to program Asteroids style movement. ...
Play Asteroids
To play, enable JavaScript from the Options or Preferences menu. How...
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...

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...

First clone picked: Asteroids

Asteroids an arcade classic, published in 1979 by Atari. Sporting some simple black and white vector graphics, old school sound effects and some simple physics I think Asteroids will be a good game to start with. Not too easy and not too hard.

I have been researching Java frameworks to help with the cloning of Asteroids. Since my main focus is on cloning classic arcade games, it makes sense to use a framework that will take care of the details concerning rendering graphics, collecting player input, playing sounds and managing game assets. My research has led me to this great Java applet library called Pulpcore. It handles all of the basics needs for a 2d rendered game implemented as a Java applet. In short Pulpcore makes a great boiler plate for my arcade game clones.

Even tho I am going to use a third party library for the graphical rendering, sound and input all the clones will run in a similar manner and it make sense to reuse as much of that code as possible. This leaves me to design a basic clone framework built on top of Pulpcore to handle loading the right assets, displaying generic menus, high score entry and display screen. This then frees me to focus on cloning the game play for each arcade game and not rewriting the same high score table five times.

I have started to flesh out the main classes and interactions in the framework by creating some quick prototypes and using UML to model the ideas generated. I recommend StarUML for drawing UML diagrams. It is free, open source and works great for me.

I will post some more details on the framework design as I complete it. If you are thirsty for more Asteroids information you can read the Asteroids Overview and the Asteroids Specification



Related Posts:
Asteroids: First Playable Release
It has been a while since I posted about cloning the arcade classic Asteroids So...
Specification
Resolution I have been unable to source the resolution of the original Asteroids...
Player Movement In Classic Asteroids Arcade Game
As noted in my previous post First clone picked: Asteroids the first classic arcade...
Asteroids
Asteroids is a classic arcade game developed and published by Atari in 1979 and is...
Asteroids Movement Demo
A demo applet showing how to program Asteroids style movement. ...

It begins

Welcome to the launch of Cloning The Classics a website dedicated to the creation of faithful clones of classics arcade games as web playable Java Applets.

Each of the classic arcade games that I clone will feature the following.

An overview of the game. A gameplay and features specification describing what will be cloned. A web playable arcade game implemented as a Java Applet and finally the source code to the clone will be released as an open source project so that other game developers can learn from my cloning attempts.

I will strive to make each arcade game clone look, sound and play as close to the original as able. Since these clones are being built from scratch an exact replica will not possible but they will remain faithful to the original game. If exactness is what you are after then you should probably check out an arcade game machine emulator like MAME

Well lets get this show on the road and clone some classic arcade games!



Related Posts:
Specification
Resolution I have been unable to source the resolution of the original Asteroids...