Jump to content
  • Advertisement
Sign in to follow this  
amigagamedev

Pixel Perfect Collision Detection

This topic is 4846 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

I need to do a Perfect Pixel Collision Detection Using Directx 9 Direct3d Sprite class for a 2d game. I'm using Visual Basic .net Anyone can help me? Sample code? Thank you

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Using the bounds of the sprites, calculate how much they overlap - you should have a very small box that defines the overlaps. For this box, test each pixel in the each of the sprites, if there's a pixel in a specific position for each of the sprite, you have a collision, you might want to use a simple alpha mask for the pixel testing. Have a look in the articles section, I can think of at least two that you'd find useful.

Share this post


Link to post
Share on other sites
You could store additional "collision maps" of your sprites in system memory (do not use them as textures!). They could be just simple black-and-white images with black indicating "hard" pixel and white empty space. Notice that you have to rotate and move them so they're always at the same position as the sprite. Then you could just test collisions between these.

Share this post


Link to post
Share on other sites
What I do is something like AP said before me... You calculate how much the boxes overlap and then for each pixel in that "overlap box" you see if theres a collision. Now, for the alpha mask I would advise that the enemy masks should only be rotated if they're close enough to the player, to have in fact the possibility of collision, since that kind of operation tends to be costy... You could do that by defining sectors in the screen, quadtrees or simple bounding circle collisions.
After you checked for collisions and rotated the sprite you could have a flag that indicated that the mask was already rotated, if you would need to use it again for some purpose, like enemy-enemy collision. This also assumes you check for collisions AFTER you have updated the enemy, since you should always put the flag off in the updating process....

Share this post


Link to post
Share on other sites
You might want to consider bounding polygon collision, as it would be quicker processor wise and about 90% as accurate if compared to pixel perfect.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!