Sign in to follow this  

Separating Axis (Degenerate Axis, Problems after rotating or scaling, some DirectX)

Recommended Posts

Ok, I have the separating axis working perfect in some case, but I am not testing for degenerate axis. I will post my code and hope that somebody here can help me setup for 3 degenerates (atleast I think I remember there being 3). Also, I am posting my full code for the test because if my matrices have any rotations or scaling in them, the test doesn't detect perfectly. Sometimes, depending on the scale, it can be an extremely tiny bounds or a extra large bounds compared to the actual bounds. I assume to correct this I need to adjust my extents in relation to the scale. But with the rotation, while the boxes always detect in their vacinity a collision, depending on the rotation is can be more towards the center or the box or a little outside the box. I am using some DirectX functions to get just my rotation matrix and transform a vector. OK, here is the code:
		D3DXMatrixDecompose(&scaleNull, &qARot, &transNull, mTransform);
		D3DXMatrixRotationQuaternion(&aRot, &qARot);
		D3DXMatrixInverse(&aRot, NULL, &aRot);

		D3DXMatrixDecompose(&scaleNull, &qBRot, &transNull, nTransform);
		D3DXMatrixRotationQuaternion(&bRot, &qBRot);

		matB  = bRot*aRot;
		vPosB = (bCenter - aCenter);
		D3DXVec3TransformCoord(&vPosB, &vPosB, &aRot);

		XAxis = D3DXVECTOR3(matB._11,matB._21,matB._31);
		YAxis = D3DXVECTOR3(matB._12,matB._22,matB._32);
		ZAxis = D3DXVECTOR3(matB._13,matB._23,matB._33);

			//15 tests
			if(fabs(vPosB.x) > aExtents.x + bExtents.x * fabs(XAxis.x) + bExtents.y * fabs(XAxis.y) + bExtents.z * fabs(XAxis.z))
				return false;
			if(fabs(vPosB.y) > aExtents.y + bExtents.x * fabs(YAxis.x) + bExtents.y * fabs(YAxis.y) + bExtents.z * fabs(YAxis.z))
				return false;
			if(fabs(vPosB.z) > aExtents.z + bExtents.x * fabs(ZAxis.x) + bExtents.y * fabs(ZAxis.y) + bExtents.z * fabs(ZAxis.z))
				return false;

			if(fabs(vPosB.x*XAxis.x+vPosB.y*YAxis.x+vPosB.z*ZAxis.x) >
				(bExtents.x+aExtents.x*fabs(XAxis.x) + aExtents.y * fabs(YAxis.x) + aExtents.z*fabs(ZAxis.x)))
				return false;
			if(fabs(vPosB.x*XAxis.y+vPosB.y*YAxis.y+vPosB.z*ZAxis.y) >
				(bExtents.y+aExtents.x*fabs(XAxis.y) + aExtents.y * fabs(YAxis.y) + aExtents.z*fabs(ZAxis.y)))
				return false;
			if(fabs(vPosB.x*XAxis.z+vPosB.y*YAxis.z+vPosB.z*ZAxis.z) >
				(bExtents.z+aExtents.x*fabs(XAxis.z) + aExtents.y * fabs(YAxis.z) + aExtents.z*fabs(ZAxis.z)))
				return false;

			if(fabs(vPosB.z*YAxis.x-vPosB.y*ZAxis.x) > aExtents.y*fabs(ZAxis.x) + 
				aExtents.z*fabs(YAxis.x) + bExtents.y*fabs(XAxis.z) + bExtents.z*fabs(XAxis.y))
				return false;
			if(fabs(vPosB.z*YAxis.y-vPosB.y*ZAxis.y) > aExtents.y*fabs(ZAxis.y) + 
				aExtents.z*fabs(YAxis.y) + bExtents.x*fabs(XAxis.z) + bExtents.z*fabs(XAxis.x))
				return false;
			if(fabs(vPosB.z*YAxis.z-vPosB.y*ZAxis.z) > aExtents.y*fabs(ZAxis.z) + 
				aExtents.z*fabs(YAxis.z) + bExtents.x*fabs(XAxis.y) + bExtents.y*fabs(XAxis.x))
				return false;
			if(fabs(vPosB.x*ZAxis.x-vPosB.z*XAxis.x) > aExtents.x*fabs(ZAxis.x) + 
				aExtents.z*fabs(XAxis.x) + bExtents.y*fabs(YAxis.z) + bExtents.z*fabs(YAxis.y))
				return false;
			if(fabs(vPosB.x*ZAxis.y-vPosB.z*XAxis.y) > aExtents.x*fabs(ZAxis.y) + 
				aExtents.z*fabs(XAxis.y) + bExtents.x*fabs(YAxis.z) + bExtents.z*fabs(YAxis.x))
				return false;
			if(fabs(vPosB.x*ZAxis.z-vPosB.z*XAxis.z) > aExtents.x*fabs(ZAxis.z) + 
				aExtents.z*fabs(XAxis.z) + bExtents.x*fabs(YAxis.y) + bExtents.y*fabs(YAxis.x))
				return false;

			if(fabs(vPosB.y*XAxis.x-vPosB.x*YAxis.x) > aExtents.x*fabs(YAxis.x) + 
				aExtents.y*fabs(XAxis.x) + bExtents.y*fabs(ZAxis.z) + bExtents.z*fabs(ZAxis.y))
				return false;
			if(fabs(vPosB.y*XAxis.y-vPosB.x*YAxis.y) > aExtents.x*fabs(YAxis.y) + 
				aExtents.y*fabs(XAxis.y) + bExtents.x*fabs(ZAxis.z) + bExtents.z*fabs(ZAxis.x))
				return false;
			if(fabs(vPosB.y*XAxis.z-vPosB.x*YAxis.z) > aExtents.x*fabs(YAxis.z) + 
				aExtents.y*fabs(XAxis.z) + bExtents.x*fabs(ZAxis.y) + bExtents.y*fabs(ZAxis.x))
				return false;

Share this post

Link to post
Share on other sites
I seem to remember that degenerate axes will produce a 'r' value of 0 or near 0. If you check that the right side of your inequation is < tolerance (0.0000001f or whatever), then you should have a degenerate axis and ignore that test.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this