Archived

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

Xingo

Box Collsion

Recommended Posts

I am having troble driving box collsiona dn even so how to know which Side is hit so I could move up,down,left or right.... Its for 3d but only moving in a 2d axis [edited by - Xingo on January 24, 2004 3:50:12 AM]

Share this post


Link to post
Share on other sites
you mean, for axis aligned boxes?

I use an overlap test, and takes the part of the overlap which has the minimum penetration.


//---------------------------------------------------------------------------

// calculate the amount of overlap between two intervals

//---------------------------------------------------------------------------

bool IntervalOverlap(float min0, float max0, float min1, float max1, float& d)
{
//-------------------------------------------------------

// the intersection amount on the left and right side of

// the interval.

//-------------------------------------------------------

float d0 = max1 - min0;
float d1 = max0 - min1;

//-------------------------------------------------------

// the intervals are disjoint, so they don''t intersect

//-------------------------------------------------------

if (d0 < 0.0f || d1 < 0.0f)
return false;

//-------------------------------------------------------

// selct the interval with the minimum of overlap

//-------------------------------------------------------

if (d0 < d1)
d = d0;
else
d = -d1;

return true;
}

//---------------------------------------------------------------------------

// AABBox vs. AABBox intersection test.

// using boxes [Min0, Max0] and [Min1, Max1], compute if they

// intersect, if they do, return the direction and amount

// [N] required to push first box away from second box.

//---------------------------------------------------------------------------

bool Intersect(const Vector& Min0, const Vector& Max0, const Vector& Min1, const Vector& Max1, Vector& N)
{
//---------------------------------------------------------------------------

// test intersection along x axis

//---------------------------------------------------------------------------

if (!IntervalOverlap(Min0.x, Max0.x, Min1.x, Max1.x, N.x))
return false;

//---------------------------------------------------------------------------

// test intersection along y axis

//---------------------------------------------------------------------------

if (!IntervalOverlap(Min0.y, Max0.y, Min1.y, Max1.y, N.y))
return false;

//---------------------------------------------------------------------------

// test intersection along z axis

//---------------------------------------------------------------------------

if (!IntervalOverlap(Min0.z, Max0.z, Min1.z, Max1.z, N.z))
return false;

//---------------------------------------------------------------------------

// select the axis with the minimum of separation as the collision axis. Clear other axes to 0.0f

//---------------------------------------------------------------------------

float mindist = (float) fabs(N.x);

if ((float) fabs(N.y) < mindist)
{
mindist = (float) fabs(N.y);
N.x = 0.0f;
}
else
{
N.y = 0.0f;
}
if ((float) fabs(N.z) < mindist)
{
N.x = N.y = 0.0f;
}
else
{
N.z = 0.0f;
}

return true;
}



you can see it in action here

Share this post


Link to post
Share on other sites