Advertisement

Pixel perfect collision

Started by May 29, 2001 05:41 AM
0 comments, last by Junghans 23 years, 8 months ago
Hello, can someone explain me EXACTLY how to do a pixel perfect collision, cause I need it for my Space Invaders like... By the way I choose this solution for the 4 blocs down the screen(you see ? ) but I don''t know if it''s the good one... How to modify a bitmap on a directdrawsurfce during the game ?? (lock, unlock ??) Or Bit per bit operation to simulate the breaking of the blocs ? Sorry for my poor english junghans@free.fr
JunGhans
quote:
Original post by Junghans
can someone explain me EXACTLY how to do a pixel perfect collision, cause I need it for my Space Invaders like...



I implemented pixel-perfect collision detection for BMDXCtls, which can be found as part of the Scrolling Game Development Kit at http://gamedev.sf.net/. So I will let the code speak for itself (see the "Hit" function in BMDXCtls source code) and in this post give you an overview of what I did.

I start by checking to see if the rectangles for two sprites overlap. If they do not overlap I return false immediately. If they do overlap I calculate the size of the overlap and where the overlap occurs on each sprite. I locate these rectangles in my off-screen surface that contains all the sprite graphics. Then I loop through each pixel in the overlap area (after locking the sprite surface so that I can directly access the sprite pixels) and compare the pixels. When I find a pixel that is non-background in both sprites, I return true. If no such pixel is found, the loop finishes and returns false. I hope you can find the source code and understand it. If this is too difficult, I did also write a detailed document on the whole process (a long time ago) which I could send you (e-mail me if you want it).

quote:

By the way I choose this solution for the 4 blocs down the screen(you see ? ) but I don''t know if it''s the good one...
How to modify a bitmap on a directdrawsurfce during the game ??



For this I think it would work to use lock/unlock, or even IDirectDrawSurface7::GetDC (or IDirectDrawSurface5::GetDC depending on your DirectX version). GetDC may be slow, but it won''t be too bad because you only need to call GetDC when a collision occurs, and it''s easier to manipulate pixels with a device context. (Don''t forget ReleaseDC.) Note, GetDC will not work while the surface is still locked. Anyway, if I implemented this, I would store the 4 blocks as 4 separate graphics in an off-screen surface. I would never store anything directly on the display surface. So I would locate where the bullet hit the block, then find the corresponding pixel in the graphic in the off-screen surface. Then I would lock the surface (with Lock or GetDC) and remove the pixel (or a few pixels) and unlock the surface. Next time the display is drawn, this block would be drawn with the pixels missing (unless the missing pixels are transparent!).

To locate the pixel where the bullet hit the block, you may have to add some code to the collision detection code. Instead of simply returning true, maybe return a struct with X and Y. (?)


"All you need to do to learn circular logic is learn circular logic"

"All you need to do to learn circular logic is learn circular logic"

This topic is closed to new replies.

Advertisement