Pixel Image Collision

BlitzMax Forums/BlitzMax Programming/Pixel Image Collision

BLaBZ(Posted 2010) [#1]
Is there a way to test for a collision between a pixal and an image?


_JIM(Posted 2010) [#2]
The easiest way would be to read a pixel from it and decide. Also, I use it on a pixmap rather than an image:

Function Collided%(px#, py#, pix:TPixmap, imgx#, imgy#)
Local _x# = px - imgx 'bring coords in pixmap space
Local _y# = py - imgy

if (_x < 0) Return False
if (_x > pix.width) Return False
if (_y < 0) Return False
if (_y > pix.height) Return False

Return ((pix.GetPixel(_x, _y) Shr 24) < $0F)
End Function



Now to explain that last line of code:

GetPixel returns a pixel in the form of a 32 bit integer, with 8 bits for each of Red, Green, Blue and Alpha components.

Shr shifts the whole value right 24 bits. That strips the RGB and leave the Alpha. Then I just measure it against a low value (might as well be 0) to give it a small threshold. Some images don't have precisely 0 alpha and that might register a collision everywhere on the image.

Limitations of this particular code:

- Does not take into account the scale/rotation of the image
- requires the pixmap to be in PF_RGBA format. I always convert them to this format upon loading. This speeds up the rest of my code because it's targeted at this format.

The other approch, a bit slower, would be to use CollideImages or CollideImages2 with your image and a 1x1 image for your "pixel".


BLaBZ(Posted 2010) [#3]
Awesome thanks a lot Jim!

I've been using the 1x1 image technique and it works! I just thought there might be a "better" way to do it.

I'm glad I asked because it gives me something new to learn, Literals :)