This topic is now archived and is closed to further replies.


collision detection

Recommended Posts

yoda5    122
Does anyone know some good code for pixel to pixel collisions. I have used straight tile based and bounding boxes and got those up and running, but I am having trouble with finding a good example of pixel to pixel. I have all tiles that are 64 wide and 64 long, and my basic object struct is just float x; float y; char tile; bool active; If anyone could point me to some good examples or has some suggestions that would be great.

Share this post

Link to post
Share on other sites
bastardos    100

(Assuming you''re coding on an x86)

The ideal solution could be to use two bitmasks, and do some shuffles and ands, I guess.

That is, create bitmasks for the two bitmaps, such that whenever the pixel is non-transparent (or otherwise a "hit"), you put a ''1'' in there, otherwise a ''0''. Pseudo-code (actually, pure C++):

int BITMASK [(64 * 64) >> 5];

for (int yloop = 0; yloop < 64; yloop++) {
for (int xloop = 0; xloop < 64; xloop++) {
if (TILESARRAY [tile->tile][xloop][yloop] != ALPHA_COLOR) BITMASK [(xloop + (yloop << 6)) >> 5] |= (1 << xloop & 31);


Then, you first do the trivial intersection calculations. After you''ve done that (and they still look like they could intersect) you call the per-pixel routine. First, you calculate the intersection.

Then, you look up the values (with a 2-dimensional for loop) from the bitmasks, using (x >> 5) as the indexes and shuffling the bit mask values (remember, they''re dword, not bit aligned) right as many times as needed (value >> (x & 31). Then you and them.


Blargh, it''s 8:49 on a monday morning and I''m barely half awake. Maybe I should just shut up.


[sPiKie] mmorpg isnt hard in vb

Share this post

Link to post
Share on other sites