07. Collision


The most obvious missing component of our game is currently collision – the MainShip can move through Rocks, Bullets pass through Rocks, and Rocks pass through each other. This tutorial will cover adding collision to all objects.

Same approach as Beefball

This tutorial will follow the same approach as Beefball:

  1. Add a Circle object to an Entity
  2. Adjust the circle radius to match the desired collision size
  3. Make the Circle object public so collision can be performed in the GameScreen
  4. Add collision loops in the GameScreen to perform the actual collision
  5. Respond to collisions if necessary – such as destroying Bullets when they collide with Rocks.

The major difference between Rock Blaster and Beefball is that RockBlaster includes Entities which contain Sprites. This means that the Entity’s Sprite is responsible for the visible representation of the Entity – the Circle (or any other shape used in this situation) is used purely for collision and should eventually be made invisible.

Using GlueView

GlueView can be very helpful when working on functionality such as adding collision objects to match the size of existing game art. GlueView will automatically update in response to changed variables, so you can immediately verify whether a Circle’s Radius is correct by keeping GlueView open while editing in Glue.

Adding collision to MainShip

To add collision to the MainShip:

  1. Expand the MainShip Entity in Glue
  2. Right-click on the Objects item
  3. Select “Add Object”
  4. Set the SourceType to “FlatRedBallType”
  5. Set the SourceClassType to “Circle”
  6. Set “HasPublicProperty” to True
  7. Scroll up in the Variable grid and set the Radius to 10

Adding collision to Bullet

Next, add a Collision object to Bullet:

  1. Expand the Bullet Entity in Glue
  2. Right-click on the Objects item
  3. Select “Add Object”
  4. Set the SourceType to “FlatRedBallType”
  5. Set the SourceClassType to “Circle”
  6. Set “HasPublicProperty” to True
  7. Set Radius to 3 in the Variables tab
  8. Set Y to 5
  9. Set X to 0.5 (to compensate for the bullet Sprite not being centered in the image)

Adding collision to Rock

To add collision to Rock:

  1. Expand the Rock Entity in Glue
  2. Right-click on the Objects item
  3. Select “Add Object”
  4. Set the SourceType to “FlatRedBallType”
  5. Set the SourceClassType to “Circle”
  6. Set “HasPublicProperty” to True
  7. Set the Radius to 6 in the Variables tab

Performing Collision

Now that all of our objects have a Collision object we can perform collision in our GameScreen.

First add the following call to CustomActivity in GameScreen.cs:

Next implement the CollisionActivity function in GameScreen.cs:

Next, we’ll implement the specific functions. Let’s start with BulletVsRockCollisionActivity:

Reverse loops are important!Note that the lists in BulletVsRockCollisionActivity are reverse loops. The reason for this is because when an Entity is destroyed, it removes itself from any PositionedObjectLists that it belongs to. For more information on the two-way relationship between Entities and PositionedObjectLists, see this article.

Next let’s do MainShipVsRockCollisionActivity next:

And finally, let’s do RockVsRockCollisionActivity. The purpose of this will be simply to bounce asteroids off of each other when they collide:

Should the mass and elasticity variables be in Glue?
If you’re asking this question then congratulations on thinking like a Glue developer! The answer is – it depends. Let’s look at a number of scenarios where you might not want to have the variables in Glue

  • The variables have little impact on the design of the game, and will likely never be changed (this is the current situation). In this case, you may not need to add the variables to Glue. Of course, this is a difficult call to make – the direction that a game takes as its developed can surprise even the most experienced game designers. However, we decided to just put the variables right in code to focus on the purpose of this section – the collision logic. Furthermore, since the scope of this tutorial is rather small, I knew up front that these values would never change.
  • The variables will be changed dynamically. Even though these tutorials don’t cover this, it would be possible to determine the mass of the rocks according to their sizes. A later tutorial will cover breaking the rocks up into smaller pieces. A game like this may adjust the collision mass according to the rock size.


That was a lot of code we just wrote, but it is actually fairly simple considering – most of the code was function headers and the structure for loops. Now if you run the game you should see circles on all of your Entities, and collisions should be functioning properly.

Since we added a Destroy call in MainShipVsRockCollisionActivity the, the MainShip will be removed from the game when it collides with a rock. In a later tutorial we will add health so that the player can survive multiple collisions.

<- 06. Rock Entity08. Rock States ->