2D Game Dev – Part 5.5: Our First Game – Collision Detection

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.

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.

Posted in 2D Game Dev, Allegro, C++, Code, Game Dev, Part 5, Tutorial
13 Comments » for 2D Game Dev – Part 5.5: Our First Game – Collision Detection
  1. Graham says:

    Thanks Mike!

    Would it be possible for you to make a tutorial on an autonomous enemy tracking algorithm?

    • Mike Geig says:

      Could you be more specific? Do you mean a system that knows where game objects are, or like “heat seeking missiles”?

  2. Graham says:

    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.

    • Mike Geig says:

      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.

  3. Steven says:

    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.

  4. chatomania says:

    Thank you very much, you are an amazing teacher.

  5. Luke says:

    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.

    • Mike says:

      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.

  6. Luke says:

    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.

  7. Varun says:

    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!

    • Mike says:

      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.

      • Varun says:

        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 ?

  8. sparksplur says:

    i love your tutorials and learning this, u are a awesome teacher i cant wait to see more

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Error: Twitter did not respond. Please wait a few minutes and refresh this page.