moai/src/uslscore/USIntersect.cpp
//----------------------------------------------------------------//
// Return:
//	 1:		Box is in front of the plane
//	 0:		Box intersects the plane
//	-1:		Box is behind the plane
Box is in front of the plane
0:             Box intersects the plane
1:             Box is behind the plane
Box is in front of the plane 0 : Box intersects the plane 1 : Box is behind the plane
s32 USSect::BoxToPlane ( const USBox& b, const USPlane3D& p ) {
	USVec3D spans = b.mMax;
	spans.Sub ( b.mMin );
	spans.Scale ( 0.5f );
	float sdX = spans.mX * p.mNorm.mX;
	if ( sdX < 0.0f ) sdX = -sdX;
	float sdY = spans.mY * p.mNorm.mY;
	if ( sdY < 0.0f ) sdY = -sdY;
	float sdZ = spans.mZ * p.mNorm.mZ;
	if ( sdZ < 0.0f ) sdZ = -sdZ;
	float r = sdX + sdY + sdZ;
	USVec3D c = b.mMin;
	c.Add ( spans );
	float d = USDist::VecToPlane ( c, p );
	if ( d > r ) return 1; 
	if ( d < -r ) return -1; 
	return 0; 
}
s32 USSect :: BoxToPlane ( const USBox & b , const USPlane3D & p ) { USVec3D spans = b . mMax ; spans . Sub ( b . mMin ) ; spans . Scale ( 0.5f ) ; float sdX = spans . mX * p . mNorm . mX ; if ( sdX < 0.0f ) sdX = - sdX ; float sdY = spans . mY * p . mNorm . mY ; if ( sdY < 0.0f ) sdY = - sdY ; float sdZ = spans . mZ * p . mNorm . mZ ; if ( sdZ < 0.0f ) sdZ = - sdZ ; float r = sdX + sdY + sdZ ; USVec3D c = b . mMin ; c . Add ( spans ) ; float d = USDist :: VecToPlane ( c , p ) ; if ( d > r ) return 1 ; if ( d < - r ) return -1 ; return 0 ; }
Return:
1:             Box is in front of the plane
0:             Box intersects the plane
1:             Box is behind the plane
Return : 1 : Box is in front of the plane 0 : Box intersects the plane 1 : Box is behind the plane
// Get the box spans // Get the span dots // Get the radius of the box (as projected onto the plane's normal) // Get the box center // The distance from the center of the box to the plane // Now test against the span // The box is in front of the plane // The box is behind the plane // The box intersects the plane