The basic collision detection system everyone starts with is bounding box collision detection, but this method can be a bit inaccurate; however we will take advantage of it in this article.

The basic idea is to check first, if bounding box collision detection has occurred, and then check pixel by pixel if the collision trully took place:

check bounding box collisions
if they exist, take a look pixel by pixel

Bounding box collisions

Lets see how we can implement bounding box collisions:

Microsoft provides us with an useful struct called RECT. It represents an axis aligned rectangle, and we will use it to test collisions.

What we should do, is save the bounding rectangle of our SPRITE class, and when we want to look for collisions between to sprites, the only thing we have to do is intersect the 2 RECT scructs the sprites hold.

The code would look something like this:

RECT dest;
IntersectRect(&dest, &sprite1.rect, &sprite2.rect);

Pixel perfect check

Once we know that a collision has happened, it's time to check pixel by pixel!

We will be working directly with the LPDIRECT3DTEXTURE9 variables in order to read their pixels.

First, we will lock them, so that we can read them from the GPU, we have to lock every texture we want to read pixels from:

Conclusion

I hope to have helped you figuring out how you can implement pixel based collisions in DirectX.

But... what to do now? This is just a "simple" example. Based on this example you can develop code that takes into account sprite scaling and rotation. It is often said that locking textures is not very efficient, because it reduces GPU acceleration. A simple approach to solve this would be to store texture pixels in a boolean array and read that array instead of reading the texture's ones.

Another thing to do now is implementing bouncing based on pixels, there are lots of possibilities!

Great article. But really, bounding boxes (aka rectangles) should be sufficient for most collision detection for sprites, especially for the beginner game developer. The only reason I could see for wanting pixel-level collision detection is for simulations. Pixel-level anything will slow down processing time.

Great article. But really, bounding boxes (aka rectangles) should be sufficient for most collision detection for sprites, especially for the beginner game developer. The only reason I could see for wanting pixel-level collision detection is for simulations. Pixel-level anything will slow down processing time.

You are right, pixel perfect collisions are very time consuming, but I just wanted to try it in DirectX, I found out that this is not a popular topic around the internet, so I decided to write my own article. You never know who may need help with this!

Did you notice that most of your comments make good function names, you could make the code readable (means easier to understand, less likely to contain bugs, easier to unit test) by just moving the gory details out of the way.

Why I am complaining? When you put code on the web for beginner programmers you influence their coding style.

Did you notice that most of your comments make good function names, you could make the code readable (means easier to understand, less likely to contain bugs, easier to unit test) by just moving the gory details out of the way.Why I am complaining? When you put code on the web for beginner programmers you influence their coding style.

Well, first of all... who says I'm not a begginner? (Because I am).Many people tend to be bogged down when they have to follow function calls in order to know what they really do.In this case, splitting the code would result in tiny functions (nothing more than a loop or an if statement).Moreover, this is just an introductory article. If you want to do real pixel perfect collision detection, you will have much more code, and then you will eventually realize that you have to create sub functions.I just felt that writing the article this way would be very easy to read and understand.

There's no reason you couldn't use a CPU side texture instead of polling the GPU for it. The GPU requires a deep pipeline of commands to be executed and the lock will stall and empty the pipeline. If you duplicate the texture on the CPU, you will double your use of texture memory, but eliminate the stall.

There's no reason you couldn't use a CPU side texture instead of polling the GPU for it. The GPU requires a deep pipeline of commands to be executed and the lock will stall and empty the pipeline. If you duplicate the texture on the CPU, you will double your use of texture memory, but eliminate the stall.

This is what you should be doing, honestly. Back before we had real alpha-blending, one way to do transparency was with a bitmask. Each "pixel" in the bitmask was comprised of all-zeros where it was transparent, and all-ones where it was opaque. You would read the destination pixel (from the framebuffer), the source pixel (from the image), and the alpha mask pixel; then, you would get the final color by logically-ORing the destination with the logical-ANDing of the source and alpha pixels -- that is dest = dest || (src && alpha).

You can actually optimize pixel-perfect collision using a similar technique -- if you create collision masks where each bit is a pixel that collides (say, any pixel that's not fully transparent, or any pixel that's more than some level of alpha, like 50%), and you have one collision mask for each of the things that can collide (the player and the ground, the player and an enemy, etc) then you can translate those two into the same coordinate space and logically-AND them together. The resulting mask will have a bit set wherever the two masks overlap, if no bits are set anywhere there is no collision. You can check multiple bits at a time by looking at byte, short, word, dword, or larger chunks at a time -- if the entire chunk is 0, there's no collision inside, if the chunk is the maximum value for its size, then it collides everywhere, if its non-zero/non-max then you have to look at it more closely.

Also, masks are more flexible because they can be pixel-perfect if you want, but if you don't you can create the collision-mask as an asset and decide for yourself exactly which pixels are collidable -- for example, you may not want the "fringes" of an object to collide -- or say the player sprite wears a cape. It would be silly to register the cape for collision in the same way as the player body.