Public Group

# 2D collision detection problem with struct's

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

## Recommended Posts

I know this question has been asked but it seams that I cant search for it in the furms I just get some error. And I know there is articles on this site but I cant figure out how they relate for what I am using. any way using struct struct Bullet2 { int Screenx; // were to put the sprite on the screen int Screeny; int Bullets; bool Alive; int width ,hight; }; And struct House { int houseX; int houseY; int width; int hight; }; how would I write a function for simple bounding boxes? Will this work? if (housex + house.width == Bulletx + bullet.width && housey + house.hight == Bullety + bullet.hight) { animate } else contiue

##### Share on other sites
Oh, I believe you'll want to be checking if it's within the bounds, not necessarily equal. The if statement you have at the end there is testing to see if the house's position plus its width EQUALS the bullet's position plus its width... essentially, checking if the exact pixels of the right side of the bullet is at the exact same location as the pixels of the right side of the house... and similarly for the Y position check you have there.

What you'll want to check is something more like:

* Is (the left wall of the house <= right side of bullet) AND
(the right wall of the house >= left side of bullet)
And a similar check for top/bottom. That way you'll know if the bullet is at or "in" the house because of its current path.

##### Share on other sites
struct Bullet2{int x; // were to put the sprite on the screenint y;int Bullets;bool Alive;int width;int height;};struct House{int houseX;int houseY;int width;int height;};bool CollisionTest(Bullet2 bullet, House house){	if(bullet.x + bullet.width >= house.x)		if(bullet.x <= house.x + house.width)			if(bullet.y + bullet.height >= house.y)				if(bullet.y <= house.y + house.height)					return true;	return false;}

Bounding box would work.

##### Share on other sites
How do I erase the sprites after collision is detected?

##### Share on other sites
Ah... what I'd do is take the section of background at the exact place where the object is being drawn, and draw it over that, and then cease drawing said objects again.

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

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

• Total Topics
631766
• Total Posts
3002218
×