Archived

This topic is now archived and is closed to further replies.

mits

2D Engine-Collision Detection?

Recommended Posts

Any easygoing ideas for collision detection in a 2D Engine which uses ID3DXSprite to draw tiles?Or a good link for this piece of work? Thanks in advance.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
bool SpriteCollision(CSprite* Sprite1, CSprite* Sprite2)
{

// Are these a valid sprites
if( !Sprite1 || !Sprite2 )
return false;

// Get the radi of each rect
int nWidth1 = (Sprite1->m_nWidth>>1) - (Sprite1->m_nWidth>>6);
int nHeight1 = (Sprite1->m_nHeight>>1) - (Sprite1->m_nHeight>>6);

int nWidth2 = (Sprite2->m_nWidth>>1) - (Sprite2->m_nWidth>>6);
int nHeight2 = (Sprite2->m_nHeight>>1) - (Sprite2->m_nHeight>>6);

// Compute the center of each rect
int cx1 = Sprite1->m_X + nWidth1;
int cy1 = Sprite1->m_Y + nHeight1;

int cx2 = Sprite2->m_X + nWidth2;
int cy2 = Sprite2->m_Y + nHeight2;

// Compute deltas
int dx = abs(cx2 - cx1);
int dy = abs(cy2 - cy1);

// Test if rects overlap
if( dx < (nWidth1+nWidth2) && dy < (nHeight1+nHeight2) )
return true;
else
// Else no collision
return false;
}

Share this post


Link to post
Share on other sites
More accurate than rectangles are line segments and per pixel collision detection.

btw, collision detection is usually independent of your renderer so it shouldn''t matter if you use ID3DXSprite or any other method.

digital radiation

Share this post


Link to post
Share on other sites