• ### Announcements

#### Archived

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

# Corner Sucks in my Sphere

## Recommended Posts

For the majority, this code works flawlessy for detecting sphere against bounding box collision. Only problem is if the moving mesh with the bounding sphere slides to any corner of the bounding box of the mesh being tested against, will sometimes suck the mesh in, and only be reversible by exiting out the corner. Any one have a solution or advice, would be greatly apprieciated!!!
bool const cModel::CheckBoxIntersect( D3DXVECTOR3 const& vLoc, D3DXVECTOR3 const* const& vBoundBox, char const cDirection )
{
D3DXVECTOR2 vDelta( m_vLoc.x + (cDirection == '1' ? m_vVel.x : -m_vVel.x), m_vLoc.z + (cDirection == '1' ? m_vVel.y : -m_vVel.y) );
D3DXVECTOR2 vDiff( vDelta.x - vLoc.x, vDelta.y - vLoc.z );	// relative position of sphere centre to the box centre

D3DXVECTOR2 vExt( vBoundBox[1].x - vBoundBox[0].x, vBoundBox[1].z - vBoundBox[0].z ); // size of the box along X, Z direction.

// see if sphere coords are within the box coords

float fDx = vExt.x - fabs(vDiff.x) + m_fFrustum;	// distance of sphere centre to one of the X-Face of the box

float fDz = vExt.y - fabs(vDiff.y) + m_fFrustum;	// distance of sphere centre to one of the Z-Face of the box

bool bOutx  =  (fDx < 0.0f);		// sphere centre between the two X-Faces of the box

bool bOutz  =  (fDz < 0.0f);		// sphere centre between the two Z-Faces of the box

bool bIn    = !(bOutx|bOutz);		// sphere centre inside all the faces of the box

if (bIn)
{
float fMinDist = fDx;

if ( fDz < fMinDist )
m_vVel.y = 0.0f;
else
m_vVel.x = 0.0f;

return true;
}
return false;
}

[edited by - Carandiru on April 21, 2004 3:48:29 AM]

##### Share on other sites
what''s m_fFrustum?

##### Share on other sites
so, your sphere is really a box

And this code looks familliar

I guess you''re trying to cancel the velocity against the colliding face.

I don;t know why you have a cDirection, since your velocity is already signed. otherwise, it looks kind of all right. Besides, your code does not handle corners, only face collisions. if you have more info, or demo and code...

##### Share on other sites
The above code is a sphere ( the object residing in *this class ) against a bounding box. All detection against sides of the bounding box works great. Using the radius of the bounding sphere (m_fFrustum), tested against the vBoundBox Extents the velocity of the object movement for *this instance will make the object "slide" by nulling the proper velocity vector dependent on what side was hit. I'm looking for an addition to handle the case for when the object is sliding against the bounding box object, the corner does not make the object translate in the direction that the sliding would normally make it.
eg.)
o------->^ +----------+| |          || |          || |          || |          |o +----------+/* Hits the corner, sliding upwards, then automagically starts sliding to the right... The object has to be rotated @ a fairlyhigh angle for this to happen, as instead of continuing upwards,it is @ a high enough angle to be caught in the detection forthe top part of the sphere->bounding box test. This case will happen for every corner @ a high angle ofincidence.*/If someone has come across a similar or exactly the sameproblem, I would greatly apprieciatte your advice!!

[edited by - Carandiru on April 23, 2004 1:01:31 AM]

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978622

• 9
• 14
• 12
• 10
• 12