2D Game Dev – Part 12.3 – Pixel Perfect Collision Detection

This post is a portion of Part 12 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.

We are finally on to the good stuff. The final algorithm we are going to look at is the pixel perfect collision detection. With this method, we create “masks” for our images, and then based on the distances between images, we form a comparison area. Once formed, we search each mask for bits in the comparison area that match. If they match, we have collision. Simple right?

Full source can be found here.

Posted in 2D Game Dev, Allegro, C++, Code, Game Dev, Part 12, Tutorial
6 Comments » for 2D Game Dev – Part 12.3 – Pixel Perfect Collision Detection
  1. GrygrFlzr says:

    It took 11.5 seconds for me to load on a 1.4 GHz PC, so the result will vary for every computer. Just a little something to keep in mind for everyone. =)

  2. Luiji Maryo says:

    Mask_New() would probably be *much* faster if you ran al_lock_bitmap(bmp) before the for-loop and al_unlock_bitmap(bmp) afterwards.

  3. TheBard says:

    For ball2.image i set a realy big png with transparent center.
    (I want it would work like a map background with trasnparent center.)
    But somehow i believe when i set the mask: ball2.mask = Mask_New(ball2.image);
    it aligns it to middle of the image.
    So the collision and mask draw is not currently aligned with the image x y.
    al_draw_bitmap(ball2.image, 0, 0, 0);
    Mask_Draw(ball2.mask, ball2.x, ball2.y);
    it looks like this: http://s16.postimage.org/8ggrk2bqt/asd.jpg
    ;D
    So I want to know where to change in the PPCD.cpp so it would align mask normaly by x and y.

  4. Mario says:

    I tried to compile this on Ubuntu 11.10 and give me this error

    /tmp/cccJve2m.o: In function `Mask_Create(int, int)’:
    PPCD.cpp:(.text+0×195): undefined reference to `operator new(unsigned int)’
    PPCD.cpp:(.text+0x1bb): undefined reference to `operator new[](unsigned int)’
    /tmp/cccJve2m.o: In function `Mask_Delete(mask*)’:
    PPCD.cpp:(.text+0x2ce): undefined reference to `operator delete[](void*)’
    PPCD.cpp:(.text+0x2df): undefined reference to `operator delete(void*)’
    collect2: ld devolvió el estado de salida 1

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>