This post is a portion of Part 5 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 5.0 – Our First Game
- 5.1 – Our First Game: The Player
- 5.2 – Our First Game: Timing and Movement
- 5.3 – Our First Game: The Projectiles
- 5.4 – Our First Game: The Enemies
- 5.5 – Our First Game: Collision Detection
- 5.6 – Our First Game: Game Logic
- 5.7 – Our First Game: Conclusion
In this video, we look at making our game interactive by adding collision detection. We don’t do anything too extreme though, just basic bounding box magic.
Full source can be found here.
Notice that the source code is slightly different from the video? Here is why.

Thanks Mike!
Would it be possible for you to make a tutorial on an autonomous enemy tracking algorithm?
Could you be more specific? Do you mean a system that knows where game objects are, or like “heat seeking missiles”?
Yes – almost like “heat seeking missiles.” Although I am referring more to enemies tracking the player. For example, comets autonomously tracking the ship as they move, instead of just flying straight ahead.
Something like that is actually very easy to achieve (depending on how you want it to work). Basically, every update cycle, the comets could adjust their bearing to the current position of the player. Of course, you would probably want to make it sloppy (so the player has a chance) which can be done by adjusting the bearing only every 5 cycles or so.
If you wish to have external forces acting upon the projectile (comet, missile, …) you can give the object momentum. It may be heading down, but when the player’s ship is to the left, apply a force toward the player. The momentum will be strong enough that the projectile will “lean” toward the player without actually homing in on them directly. It gives the projectile a natural curve without any jerky course alterations.
Thank you very much, you are an amazing teacher.
Im just wondering if there is a better way to do this in Allegro 5? I know in Allegro 4 there was a way to grab Pixels from a bitmap and if your in an area with that certain pixel. It trigger a collision statement. Wich I had set up.
You can certainly do that, but I would not consider it better. Get pixel operations are costly as far as efficiency goes. Also it is worth noting, that in this example we aren’t using bitmaps. Therefore, we would have to draw to the screen and then pull back from the screen. Bounding box collision is much more efficient.
Thankyou. I was thinking of doing it that way for my Pong game that I thought I might make. I will have to work on Bounding Box then.
I tend to get Comets too close to each other after every little while(not fixed), two comets tend to be drawn over each other in a way that they have 30-40% shared circle!
I hope i am able to convey it correctly!
It should be that the comets are initialized randomly. You will have some spawning on each other, but that shouldn’t happen all of the time.
It does, each time i run the game, after every few seconds, i do get some spawning! The game might look mediocre because of it. So, some rand() mod should do it ?
i love your tutorials and learning this, u are a awesome teacher i cant wait to see more