Jump to content
  • Advertisement


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.

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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!