• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

stbn

Members
  • Content count

    0
  • Joined

  • Last visited

Community Reputation

300 Neutral

About stbn

  • Rank
    Newbie
  1. 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!
  2. [quote name='Guest' timestamp='1345057887'] 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. [/quote] 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.
  3. [quote name='Eastfist' timestamp='1344282204'] 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. [/quote] 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!
  4. There's a problem in the article submission system that stops me from updating the article. I will update it as soon as they fix it.