# Thread: 2d Collision using Directx

1. ## 2d Collision using Directx

Hey everyone,

I am currently implementing the darkGDK library for creating a basic 2D game.

USING:
Microsoft Visual C++ 2008 Express
DirectX SDK 2009 (August)
DarkGDK

I actually can't figure out how to get my game to run without using Debug->Start Debugging because when I successfully make the release build, I only get a black screen....

ANYHOW, on to the point.

The collision detection for DarkGDK is not really that great unless you want to use blocks as sprites. I would like to get help on using DirectX (Sprite Collision Masks i think) to create "Pixel Perfect" collisions.

SCREENSHOTS OF PROGRESS:
http://i39.tinypic.com/bj7fbo.png
http://i41.tinypic.com/2up9xr4.png

I do have full movement of character and working animations

and yes, the little guy is Link

2. I have a "pixel perfect" detection algo. It is written in allegro but the concepts should be simple to port to a direct x context.

Code:
```struct BoundingBox
{
int top;
int bottom;
int left;
int right;
};```
Code:
```#include <algorithm>
#include "Collision.h"

bool CCollisionDetection::DetectBoundingBox(const CSprite &spriteOne, const CSprite &spriteTwo)
{
//
//Grab the bounds of the sprites
//
const BoundingBox &boundOne = spriteOne.Bounds();
const BoundingBox &boundTwo = spriteTwo.Bounds();

//if the left is greater than the right no collison
if(boundOne.left > boundTwo.right){
return false;
}//if

//if the right is less than the left no collision
if(boundOne.right < boundTwo.left){
return false;
}//if

//if the bottom is less than the top no collision
if(boundOne.bottom < boundTwo.top){
return false;
}//if

//if the top is greater than the bottom no collision
if(boundOne.top > boundTwo.bottom){
return false;
}//if

//if all the checks failed that means they are colliding
return true;
}

bool CCollisionDetection::DetectPixelPerfect(const CSprite &spriteOne, const CSprite &spriteTwo)
{
//Cannot preform collision detection on a non active sprite
if(!spriteOne.IsActive() || !spriteTwo.IsActive()){
return false;
}//if

//Preform bounding box collision to save the waste of cpu on collision that are not close
if(!DetectBoundingBox(spriteOne, spriteTwo)){
return false;
}//if

//
//Grab the sprite's bounds
//
const BoundingBox& boundOne = spriteOne.Bounds();
const BoundingBox& boundTwo = spriteTwo.Bounds();

//Grab the sprite's images
const CBitmap& imageOne = spriteOne.Image();
const CBitmap& imageTwo = spriteTwo.Image();

//
//

//
//Grab the min and max y overlap
//
int minY = std::max(boundOne.top, boundTwo.top);
int maxY = std::min(boundOne.bottom, boundTwo.bottom);

//
//Grab the min and max x overlap
//
int minX = std::max(boundOne.left, boundTwo.left);
int maxX = std::min(boundOne.right, boundTwo.right);

for(int currentY = minY; currentY < maxY; currentY++){
//
//Grab the x adjustments for grabbing the proper place in the image.
//
int spriteOneYAdjustedPixel = currentY - boundOne.top;
int spriteTwoYAdjustedPixel = currentY - boundTwo.top;

for(int currentX = minX; currentX < maxX; currentX++){
//
//Grab the position x adjustments for grabbing the proper place in the image.
//
int spriteOneXAdjustedPixel = currentX - boundOne.left;
int spriteTwoXAdjustedPixel = currentX - boundTwo.left;

//Get the current pixel for the first sprite

//Get the current pixel for the second sprite

//Special Case: If both pixel at the current position are not the mask color of the
//image then they are overlapping. Return true to the caller.
return true;
}//if
}//for
}//for
return false;
}```

3. cool, thanks!

Looking through the code now....i've never used allegro but I might be able to convert over to directx. I have seen other examples and they were quite different.

see here

p.s. what is the code for you header file?

4. I have never really worked with direct x sprites or textures before.

But the key points were the logic. There are a couple of lines that should be replaced by direct x objects. The mask pixel color, the bounding box, etc.

But that two level loop is really where the magic happens.

5. yeah, i figured i'd need some kind of loop like that. I was gonna write mine from scratch and store the mask for the sprite in a 2d array. Then somehow, get the detection to work from there...but I know I have to lock the current animation frame before I detect a collision because the little guy is always moving

6. Here is the header file
Code:
```#ifndef collision_h
#define collision_h
#include "Sprite.h"

class CCollisionDetection
{
public:
static bool DetectBoundingBox(const CSprite &spriteOne, const CSprite &spriteTwo);
static bool DetectPixelPerfect(const CSprite &spriteOne, const CSprite &spriteTwo);
};

#endif//collision_h```

7. Thanks, my eyes are getting tired though and I should probably wait till morning before I can think clearly :\

But thanks for all your help, I'm still confused on all the different examples I've seen for "Pixel Perfect" collisions. Although I know there are going to be many different methods of doing this, I think I will try to go with the one that I understand the most :b

Thanks again, I'll PM you if I have any questions regarding your code