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

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

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

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633705
• Total Posts
3013463
×