Jump to content
  • Advertisement
Sign in to follow this  
Volvic

Bounding box collision detection for a 2d game(Solved)

This topic is 4326 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

Hi guys its my 1st time trying to implement some collision code for my game, basically i have 2 sprites and i want it so that when they collide i can give a responce. by the way i'm using c++ DX9 here is how i have made my bounding boxes
float EnemyHeight,EnemyWidth,PlayerHeight,PlayerWidth = 32;
D3DXVECTOR2 vEnemyPosition;
D3DXVECTOR2 vPlayerPosition;

	RECT EnemyBoundingBox;
	EnemyBoundingBox.top = vEnemyPosition.y - (EnemyHeight/2);
	EnemyBoundingBox.bottom = vEnemyPosition.y + (EnemyHeight/2);
	EnemyBoundingBox.left = vEnemyPosition.x - (EnemyWidth/2);
	EnemyBoundingBox.right = vEnemyPosition.x + (EnemyWidth/2);

	RECT PlayerBoundingBox;
	PlayerBoundingBox.top = vPlayerPosition.y - (PlayerHeight/2);
	PlayerBoundingBox.bottom = vPlayerPosition.y + (PlayerHeight/2);
	PlayerBoundingBox.left = vPlayerPosition.x - (PlayerWidth/2);
	PlayerBoundingBox.right = vPlayerPosition.x + (PlayerWidth/2);




so can someone tell me what i do now to check for colisions? [Edited by - Volvic on December 11, 2006 4:02:08 AM]

Share this post


Link to post
Share on other sites
Advertisement
check to see if the rectangles are outside each other.



// returns true if the two rects intersect.
bool Collision(RECT a, RECT b)
{
if (a.right < b.left) return false;
if (a.bottom < b.top) return false;
if (b.right < a.left) return false;
if (b.bottom < a.top) return false;

return true;
}




I think that's right. You should test it out though, I just wrote it off the top of my head...

Share this post


Link to post
Share on other sites
I think i have got the right code for it now


// =========
// Test collisions between player and enemy
// =========
void player_TestEnemyCollision()
{
//=========== Player information
D3DXVECTOR2 vPlayerPosition;
g_Player.GetTranslation( &vPlayerPosition );
D3DXVECTOR2 vEnemyPosition;
g_GhostPinky.GetTranslation(&vEnemyPosition);

RECT EnemyBoundingBox;
EnemyBoundingBox.top = vEnemyPosition.y - (EnemyHeight/2);
EnemyBoundingBox.bottom = EnemyBoundingBox.top + EnemyHeight;
EnemyBoundingBox.left = vEnemyPosition.x - (EnemyWidth/2);
EnemyBoundingBox.right = EnemyBoundingBox.left + EnemyWidth;

RECT PlayerBoundingBox;
PlayerBoundingBox.top = vPlayerPosition.y - (PlayerHeight/2);
PlayerBoundingBox.bottom = PlayerBoundingBox.top + PlayerHeight;
PlayerBoundingBox.left = vPlayerPosition.x - (PlayerHeight/2);
PlayerBoundingBox.right = PlayerBoundingBox.left + PlayerWidth;

Collision(EnemyBoundingBox,PlayerBoundingBox);
if (Collision(EnemyBoundingBox,PlayerBoundingBox)==true)
{
//Do a response here too the collision
}
}
// =========
// returns true if the two rect's intersect.
// =========
bool Collision(RECT a, RECT b)
{
if(a.bottom < b.top)return false;
if(a.top > b.bottom)return false;
if(a.right < b.left)return false;
if(a.left > b.right)return false;
return true;
}




Share this post


Link to post
Share on other sites
kanato post is correct, Volvic check your signs and a's and b's in your new code.

edit: thanks now you have changed it and make me look an old fool :(

Share this post


Link to post
Share on other sites
Sign in to follow this  

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