Pixel perfect collision implementation for allegro 5
nova99

I'm making a simple allegro game and I need to detect the collision between two bitmaps. I tried to use the bounding box method, but, since my two sprites aren't squares nor rectangles, it is pretty unaccurate. Searching around for a solution, I found the so-called pixel perfect method and it is implementable in Allegro 4 with the PMASK library but not in Allegro 5. Searching around I found this library which does work with Allegro 5, but it's not fully optimised so the collision detection is not precise. Does someone know how to use this system with Allegro 5 or where I can find a completely working library?

m c

you can do it yourself by first checking bounding box then double checking by normalizing the coordinates and using getpixel to see if the normalized coordinate of both bitmaps is non-transparent, for each pixel on one of your bitmaps (or you could be more clever, and only for the pixels in the overlapping sub-region of both bitmaps).

something like

1int y; 2for(y=0;y<al_get_bitmap_height(bmp1);++y) 3{ 4int x; 5for(x=0;x<al_get_bitmap_width(bmp1);++x) 6{ 7 8int screen_x; 9int screen_y; 10ALLEGRO_COLOR pixel; 11 12screen_x = bmp1_pos_x + x; 13screen_y = bmp1_pos_y + y; 14pixel = al_get_pixel(bmp1,screen_x - bmp1_pos_x, screen_y - bmp1_pos_y); 15//if transparent then continue to the next loop, i.e. "continue;" statement 16pixel = al_get_pixel(bmp2, screen_x - bmp2_pos_x, screen_y - bmp2_pos_y); 17//if non transparent, we have a collision 18 19} 20}

i used a4 where getpixel returns bitmap_mask_color(bmp) for transparent and -1 if the coordinates are outside of the bitmap, but for a5 not sure on that part

nova99

thanks a lot it works perfectly