Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jon723

Help with bounding box collision detection!!!!!

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

Hello, I'm trying to work out the serious kinks in my bounding box collision detection. What I'm trying to do is update the box as the object moves and see if the two bounding boxes are overlapping. My funtion keeps return true even though the two objects aren't overlapping. Can someone help me resolve this problem? Here is my code:
void updateBox(Player *p)
{
	*p->max = *p->position + *p->extent;
	*p->min = *p->position - *p->extent;
}
	

// Bounding Box Collision test

bool boxCollision(Player *p1, Player *p2)
{
	updateBox(p1);
	updateBox(p2);

	if( (p1->max->x >= p2->min->x && p1->min->x <= p2->max->x) &&
		(p1->max->y >= p2->min->y && p1->min->y <= p2->max->y) &&
		(p1->max->z >= p2->min->z && p1->min->z <= p2->max->z) )
	{
		return true;
	}

	return false;
}

[edited by - Jon723 on May 8, 2004 2:25:17 PM] [edited by - Jon723 on May 8, 2004 2:26:31 PM] [edited by - Jon723 on May 8, 2004 2:40:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
if your using windows check out this function that is part of the windows api


RECT rcDst;

if( IntersectRect(&rcDst, &Object1.GetBBox(), &Object2.GetBBox()) != 0 )
{
// Collision
}
else
{
// No collision
}


you could even wrap it up like this


bool boxCollision(CONST RECT *lprcSrc1, CONST RECT *lprcSrc2)
{
RECT rcDst;

if( IntersectRect(&rcDst, lprcSrc1, lprcSrc2) != 0 )
{
// Collision

return true;
}

// No collision

return false;
}



if( boxCollision(&p1.GetBBox(), &p2.GetBBox()) )
{
// Collision
}

Share this post


Link to post
Share on other sites
This is AABB testing right? (It looks that way)
All you need to test is if any axis min of box1 is greater than the same axis max of box2, like so:

bool inline AABBAABBIntersection(AABB_s boxA, AABB_s boxB)
{
if(boxA.Mins.x > boxB.Maxs.x)
return false;
if(boxA.Maxs.x < boxB.Mins.x)
return false;
if(boxA.Mins.y > boxB.Maxs.y)
return false;
if(boxA.Maxs.y < boxB.Mins.y)
return false;
if(boxA.Mins.z > boxB.Maxs.z)
return false;
if(boxA.Maxs.z < boxB.Mins.z)
return false;
return true;
}

Wtf is going on, half the code keeps being cut off?



[edited by - X-0ut on May 8, 2004 4:40:51 PM]

[edited by - X-0ut on May 8, 2004 4:42:19 PM]

Share this post


Link to post
Share on other sites
X-out: that''s almost exactly the code that he already posted.

The code seems reasonably. I would run to the point where there''s a problem, and then put a breakpoint in the test, and check what each comparision returns. You might want to break it out into three separate booleans to make it easier to debug (in optimized builds, the compiler will fold it all anyway).

To keep in the spirit of re-writing the same code a third time in the same thread, here''s what it would look like for easier debugging:


bool aabb_intersect( aabb * a, aabb * b ) {
bool x = (a->min.x <= b->max.x) && (a->max.x >= b->min.x);
bool y = (a->min.y <= b->max.y) && (a->max.y >= b->min.y);
bool z = (a->min.z <= b->max.z) && (a->max.z >= b->min.z);
return x && y && z;
}


If the function returns intersection when you think it shouldn''t, chances are you''re giving it bad data, rather than the test being wrong. The only way to find this out is to stop in the debugger and examine the data.

Share this post


Link to post
Share on other sites
I''m pretty sure the problem lies in that when I constructed the bounding boxes for my objects I did the according to the vertices in the model (which is how they''re supposed to be constructed). When I''m running the test the box coordinates are in world space which is makes the test fail when it shouldn''t. How do I get things back into the space of the model??

Share this post


Link to post
Share on other sites
Actually he was doing the opposite, testing if an AABB is completely inside another AABB, which will fail in partial overlap.
That is why I said test if an AABB is fully outside another AABB.

Share this post


Link to post
Share on other sites
quote:
Original post by X-0ut
Wtf is going on, half the code keeps being cut off?


You probably neglected a space between a word and your <''s and >''s prompting your browser to try and interpret (and ignore) some of your code as tags.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
Testing if two boxes are ourside of one another still fails if they aren't overlapping. Is what I have with the updating of the box correct by applying the extent of the obx to the current position??

[edited by - Jon723 on May 8, 2004 9:28:26 PM]

Share this post


Link to post
Share on other sites

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