Jump to content
  • Advertisement

Archived

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

yoda5

collision detection

This topic is 5287 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

(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.

if (VALUE_SELECTED_FROM_BITMASK1 & VALUE_SELECTED_FROM_BITMASK2) return YES_THEY_DO_INTERSECT;

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

.bas

[sPiKie] mmorpg isnt hard in vb

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!