Blog
Articles and information on C# and .NET development topics

Boulder Dash Part 2: Collision Detection

In our previous post we introduced the Firefly and Butterfly sprites and their movement rules around a random map. Now we're going to update the project to include collision detection and a new player sprite.

Refactoring AiTest

To start with though, we're going to do a little refactoring. The ButterflySprite and FireflySprite classes share pretty much the same movement code and will share exactly the same collision code, so we'll merge the common behaviour into a new abstract EnemySprite class which these will inherit from. We'll also add a protected constructor which will allow us to specify the differences between the inherited sprites and their movement rules.

With that done, we'll change FireflySprite and ButterflySprite to inherit from EnemySprite instead of Sprite, and update the constructors of these classes to call the protected constructor to supply the movement rule differences.

Finally, we'll remove the Move methods from the two sprite classes and instead let EnemySprite implement the movement code.

Although I won't go into this here, other refactoring we did was to move the Sprites collection from MainForm into Map. I also added an IsScenery method to the Map class which returns if a tile is considered scenery, for example a piece of solid earth, or a boulder which can't currently move.

A basic load map system was also added. You can still use the "Create Random Map" to generate a mostly empty canvas for the sprites to move around in (clicking with the left button will add a new firefly, with the right a butterfly) or you can load the predefined map.

Collision Detection

Now it's time to implement the actual collision detection. We'll do this by adding a new function to the base Sprite class that will check to see if a the location of any sprite matches a given location.

Note: This implementation assumes that only one sprite can occupy a tile at any one time, which is the case in Boulder Dash.

We're also using LINQ in this function for convenience. If you haven't yet upgraded to Visual Studio 2008/2010 you'll need to replace the call with a manual loop

I choose to implement the function as a bool to allow it to be easily used in an if statement, but providing an out parameter to return the matching sprite (or null otherwise).

With that done, it's time to update our movement code to also perform the collision detection. The two conditions in the Move method which check if a tile is part of the scenery will be modified to call out new method.

if (!this.Map.IsScenery(tile) && !this.IsCollision(tile.Location, out collision))

With this change, sprites on the map are now aware of each other and when they bump into each other they will automatically turn away.

Collision Actions

Our example project now has collision detection in place for the enemy sprites. Being enemies of the player nothing happens when they bump into each other. If they bump into the player on the other hand...

Time to add a new sprite. The PlayerSprite will be a non functioning sprite masquerading as a player character.

Now we have this, we'll update the Move method of our EnemySprite to take care of the action:

// if we collided with a sprite, lets execute the action
if (collision != null && this.GetCollisionAction(collision)== CollisionAction.Explode)
{
// kill both this sprite and the one we collided with
this.Map.Sprites.Remove(collision);
this.Map.Sprites.Remove(this);
}

Note that if the Player could move as well then it too would need collision detection. However, as we only have one class capable of movement we'll add the code just to that for now.

Also note that we had to adjust the original NextMove method in MainForm otherwise it would crash when looping through the sprite list and a removal occurred.

About The Author

The founder of Cyotek, Richard enjoys creating new blog content for the site. Much more though, he likes to develop programs, and can often found writing reams of code. A long term gamer, he has aspirations in one day creating an epic video game. Until that time, he is mostly content with adding new bugs to WebCopy and the other Cyotek products.

Leave a Comment

While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

Your name:

Your email address: (optional, will never be displayed)

Your URL: (optional)

Comments

Comment

Styling with Markdown is supported

Search

Donate

This content may be used free of charge, but as with all free content there are costs involved to develop and maintain.

If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.