#### Archived

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

# 2D Engine-Collision Detection?

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

## 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 on other sites
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 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.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002140
×