2D collision detection problem with struct's

Started by
3 comments, last by VThornheart 19 years, 4 months ago
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
Advertisement
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.
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)
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.
Rob Loach [Website] [Projects] [Contact]
How do I erase the sprites after collision is detected?
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.
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)

This topic is closed to new replies.

Advertisement