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

2D Engine-Collision Detection?

This topic is 5438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 no collision
return false;

Share this post

Link to post
Share on other sites