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

Started by
2 comments, last by CmpDev 17 years, 5 months ago
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]
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...
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;}
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 :(

This topic is closed to new replies.

Advertisement