• 07/24/13 10:07 PM
    Sign in to follow this  

    Pixel perfect collision detection in DirectX

    Graphics and GPU Programming

    stbn
    • Posted By stbn
    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: D3DLOCKED_RECT rectS1; HRESULT hResult = myTexture->LockRect(0, &rectS1, NULL, NULL); Then, we will grab the pixel data by reading de pBits component of our locked rectangle (Again, we need to do this for every texture we are working with): D3DCOLOR* pixelsS1 = (D3DCOLOR*)rectS1.pBits; Now is when the fun part comes in. We have to check the intersected rectangle pixels to see if the pixels collide. Sadly, we just can read our texture's data (not the screen data) so we will have to tranlate screen coordinates to each texture's coordinates, and then read the texture's pixels. for (int rx = dest.left; rx < dest.right; rx++) { for (int ry = dest.top; ry < dest.bottom; ry ++) { // Translate screen coordinates into texture coordinates int s1x = rx - sprite1.x; int s1y = ry - sprite1.y; int s2x = rx - sprite2.x; int s2y = ry - sprite2.y; // a and b will hold the alpha values of the pixel we are staring at BYTE a = (pixelsS1[s1y * TEXTURE1_WIDTH + s1x] & 0xFF000000) >> 24; BYTE b = (pixelsS2[s2y * TEXTURE2_WIDTH + s2x] & 0xFF000000) >> 24; // if both pixels are opaque a collision has taken place if (a == 255 && b == 255) { //Remember to unlock your textures texture1->UnlockRect(0); texture2->UnlockRect(0); return 1; } } }

    Full code

    In case you want to read how the full code would be, here it is: // Returns 1 if a collision is detected and 0 if the collision did not happen int PixelPerfectCollision(SPRITE sprite1, SPRITE sprite2) { // Creation of the bounding rectangles from the SPRITE values // Remember that coordinates start in the upper left corner of the screen RECT rect1; rect1.left = (long)sprite1.x; rect1.top = (long)sprite1.y; rect1.right = (long)sprite1.x + sprite1.width; rect1.bottom = (long)sprite1.y + sprite1.height; RECT rect2; rect2.left = (long)sprite2.x; rect2.top = (long)sprite2.y; rect2.right = (long)sprite2.x + sprite2.width; rect2.bottom = (long)sprite2.y + sprite2.height; // Intersection of the bounding rectangles // Up to here the code is just bounding box collision detection RECT dest; if (IntersectRect(&dest, &rect1, &rect2)) { // Loking of the textures // In this case the SPRITE object holds the texture to draw // We will access it and invoke the LockRect method of LPDIRECT3DTEXTURE9 // The pixels will be saved in each D3DLOCKED_RECT object D3DLOCKED_RECT rectS1; HRESULT hResult = sprite1.texture->LockRect(0, &rectS1, NULL, NULL); if(FAILED(hResult)) { MessageBox(0,"Failed","Info",0); return 0; } D3DLOCKED_RECT rectS2; hResult = sprite2.texture>LockRect(0, &rectS2, NULL, NULL); if(FAILED(hResult)) { MessageBox(0,"Failed","Info",0); return 0; } // Get the pointer to the color values // From now on, we will read that pointer as an array D3DCOLOR* pixelsS1 = (D3DCOLOR*)rectS1.pBits; D3DCOLOR* pixelsS2 = (D3DCOLOR*)rectS2.pBits; // We will check the area of the intersected rect (dest) // In this rectangle, we have to check that in the same spot: // A pixel from each texture collide for (int rx = dest.left; rx < dest.right; rx++) { for (int ry = dest.top; ry < dest.bottom; ry ++) { // Translation from the "dest" rect to sprite1 coordinates int s1x = rx - sprite1.x; int s1y = ry - sprite1.y; // Translation from the "dest" rect to sprite2 coordinates int s2x = rx - sprite2.x; int s2y = ry - sprite2.y; // Check the alpha value of each texture pixel // The alpha value is the leftmost byte BYTE a = (pixelsS1[s1y * 128 + s1x] & 0xFF000000) >> 24; BYTE b = (pixelsS2[s2y * 480 + s2x] & 0xFF000000) >> 24; // If both pixels are opaque, we found a collision // We have to unlock the textures and return if (a == 255 && b == 255) { sprite1.texture->UnlockRect(0); sprite2.texture->UnlockRect(0); return 1; } } } // If we reached here, it means that we did not find a collision sprite1.texture->UnlockRect(0); sprite2.texture->UnlockRect(0); return 0; } return 0; }

    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!


      Report Article
    Sign in to follow this  


    User Feedback

    Create an account or sign in to leave a review

    You need to be a member in order to leave a review

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now


    Joyal

    Report ·

      

    Share this review


    Link to review
    Ectara

    Report ·

      

    Share this review


    Link to review
    ballmar

    Report ·

      

    Share this review


    Link to review
    stbn

    Report ·

      

    Share this review


    Link to review