Part 5 – Player missiles and collisions

In this episode we look at launching some missiles from the player tank and checking for collisions with the Invaders.

Also available on Youtube for those that like a more visual approach,

OK,Let’s go!

The full source code is available here, to use it (or just view it) just click expand and copy and paste to your Arduino Development software.

New constant
Line 23
shows a new constant

#define INVADER_HEIGHT 8.

Which as it says just defines the height of the invaders. All the Space invaders have the same height so this makes things super easy for us.

The following lines define some properties of player missiles. The graphics height and width and also the speed that it travels at, the higher the number the faster. This is basically the number of pixels moved per game “click”. A game “click” is just one cycle though all the physics code (remember the physics code is the code that implements all the movement/game logic/ collision detection etc.

Line 38 adds a new status for game objects such as Invaders which allows us to flag if they have been destroyed and thus shouldn’t be displayed etc.

#define DESTROYED 2

We also define the very simple missile graphics which I won’t discuss further as we have covered this in some detail in earlier episodes.

Line 171 creates a global object for the one missile that the player can fire at a time:

GameObjectStruct Missile;

This is a game restriction/mechanic of the original arcade machine, it only allowed one player missile on screen at any one time.

The physics function has been updated to include two new aspects, the control of any fired missile and the collisions it may have:

Firing the missile
The firing is a player action, so quite logically this is controlled in the PlayerControl function, here are the changes:

The first line is making the decision of If Player has pressed fire AND there isn’t an active missile , if this condition is satisfied then we set up the firing of the missile. Line 208 sets the missiles X position to the middle of the players tank (where the barrel is). Line 209 sets the missiles Y to the players Y, and so the missile is ready to launch from the players tank barrel. We then mark the status of the missile to Active. This will  be used by several routines, including this one as just noted at the start of the paragraph. If it isn’t active for example then the display will not plot it to screen and if active then you are not allowed to fire any more missiles.

MissileControl()

This is a very simple piece of code which basically just changes the missiles position according to the speed constant mentioned earlier. It also checks if it’s gone off the top of the screen( Y less than 0). If so it marks the missiles status as destroyed and then another can be fired by the player.

CheckCollisions()
This will be expanded in later episodes and for now it simply calls another function which is….

MissileAndAlienCollisions()

This goes through all the Invaders and for those that are still Active it checks if the missile and this Invader are in collision, to do that it uses this line;

if(Collision(Missile,MISSILE_WIDTH,MISSILE_HEIGHT,Alien[across][down].Ord,AlienWidth[down],INVADER_HEIGHT))

This is just calling a function called Collision which takes 6 parameters (arguments). The first three are for the first of the objects that may be in collision, the next three are for the second object that may be colliding with the first object. If they are colliding it returns true else false. If true then the above routine marks this particular Invader as destroyed and marks the missile as destroyed also. At this point I could have coded the routine to end as the missile can only be in collision with one object at a time, but I didn’t. The game runs fast enough and it’s not an issue but it could be an area of improvement if you wished to do this.

Collision
The collision function is a single line of code that returns true if two objects are in collision or false otherwise. It takes just two coordinate structures for the objects in question. It also takes the width and height of the two objects in question. With this information it can be worked out if these two objects are overlapping (in collision).  Note :Usually we would have a structure that included the coordinates and the width/depth of the object all in one, however this elegance has been compromised in order to save memory on the memory challenged processor used on our Arduinos. To put this into perspective even some of those early 1980’s home computers had more available memory than we have at our disposal with the Atmel processor we are using. However this processor was never designed to be the equivalent of a general purpose desktop computer!

The code above is relatively simple (being only 1 line) but sometimes the code can be hard to imagine what it’s doing in your head. So below I have created a hands example of this exact code. There are 4 conditions that must be met for a collision to have taken place, when any one of these conditions are true it highlights in red. When they are all true you will see the all conditions in red and the only time this happens is when the two objects overlap. Underneath the code I’ve also shown that actual numbers for each of the four conditions. Have a play with the controls to move object 1 whilst looking at the code and the coordinates as they change.




Displaying the missile
We have some very small additional code to our display routine which shouldn’t need any explanation.

Initialising the missile
The missile is set up prior to a game starting in the InitPlayer routine.

All we do is simply mark the missiles status as currently destroyed.

… And that’s it for this episode, if you run the code you will see we can fire a missile and destroy an Invader. Admittedly it just disappears for now and when you’ve cleared them all nothing happens. In  the next episode we’ll look at adding the explosion animation and some scoring.

Enjoy and Learn 🙂