Sign in to follow this  

Calculate the angle of intersection and depth

Recommended Posts

anchelito    122
hello everyone, i successfully implemented the separating axis theorem for my collision-detection. now i just need to know how to calculate the angle in which the one object hit the other and of course the depth to know how much both objects intersect. here is what i have so far: a working obb-collision-detection. it is a bit messed up between DX and our own vector-format, because i am still using DirectX for the matrix-calculations till i write my own matrix-class:
	bool OBBCollision::checkForCollision(const Tron::BoundingBox &boxA, const Tron::BoundingBox &boxB){
		// rotation-matrizes for A and B
		D3DXMatrixRotationYawPitchRoll(&matRotA, boxA.getDirection().y, boxA.getDirection().x, boxA.getDirection().z);
		D3DXMatrixRotationYawPitchRoll(&matRotB, boxB.getDirection().y, boxB.getDirection().x, boxB.getDirection().z);
		D3DXMATRIX matRotA_inverse;
		D3DXMatrixInverse(&matRotA_inverse, NULL, &matRotA);

		// the rotation of B in relation to A
		D3DXMATRIX rotAtoB = matRotB*matRotA_inverse;

		CVector3D diff(boxB.getPosition() - boxA.getPosition());
		D3DXVECTOR3 dxDiff;

		// the translation of B in relation to A
		D3DXVECTOR3 transAtoB;
		D3DXVec3TransformCoord(&transAtoB, &dxDiff, &matRotA_inverse);
		// we need the individual axis
		CVector3D axisX(rotAtoB._11,rotAtoB._21,rotAtoB._31);
		CVector3D axisY(rotAtoB._12,rotAtoB._22,rotAtoB._32);
		CVector3D axisZ(rotAtoB._13,rotAtoB._23,rotAtoB._33);

		CVector3D axisXabs(std::abs(rotAtoB._11),std::abs(rotAtoB._21),std::abs(rotAtoB._31));
		CVector3D axisYabs(std::abs(rotAtoB._12),std::abs(rotAtoB._22),std::abs(rotAtoB._32));
		CVector3D axisZabs(std::abs(rotAtoB._13),std::abs(rotAtoB._23),std::abs(rotAtoB._33));
		// the tests need the half-sizes
		CVector3D halfSizeA(boxA.getSize());
		halfSizeA.x /=2.0f; halfSizeA.y /=2.0f; halfSizeA.z /=2.0f;
		CVector3D halfSizeB(boxB.getSize());
		halfSizeB.x /=2.0f; halfSizeB.y /=2.0f; halfSizeB.z /=2.0f;
		//15 tests
		//1 (Ra)x
		if(fabs(transAtoB.x) > halfSizeA.x + halfSizeB.x * axisXabs.x + halfSizeB.y * axisXabs.y + halfSizeB.z * axisXabs.z)
			return false;
		//2 (Ra)y
		if(fabs(transAtoB.y) > halfSizeA.y + halfSizeB.x * axisYabs.x + halfSizeB.y * axisYabs.y + halfSizeB.z * axisYabs.z)
			return false;
		//3 (Ra)z
		if(fabs(transAtoB.z) > halfSizeA.z + halfSizeB.x * axisZabs.x + halfSizeB.y * axisZabs.y + halfSizeB.z * axisZabs.z)
			return false;
		//4 (Rb)x
		if(fabs(transAtoB.x*axisX.x + transAtoB.y*axisY.x+transAtoB.z*axisZ.x) >
			(halfSizeB.x+halfSizeA.x*axisXabs.x + halfSizeA.y * axisYabs.x + halfSizeA.z*axisZabs.x))
			return false;
		//5 (Rb)y
		if(fabs(transAtoB.x*axisX.y+transAtoB.y*axisY.y+transAtoB.z*axisZ.y) >
			(halfSizeB.y+halfSizeA.x*axisXabs.y + halfSizeA.y * axisYabs.y + halfSizeA.z*axisZabs.y))
			return false;
		//6 (Rb)z
		if(fabs(transAtoB.x*axisX.z+transAtoB.y*axisY.z+transAtoB.z*axisZ.z) >
			(halfSizeB.z+halfSizeA.x*axisXabs.z + halfSizeA.y * axisYabs.z + halfSizeA.z*axisZabs.z))
			return false;
		//7 (Ra)x X (Rb)x
		if(fabs(transAtoB.z*axisY.x-transAtoB.y*axisZ.x) > halfSizeA.y*axisZabs.x + 
			halfSizeA.z*axisYabs.x + halfSizeB.y*axisXabs.z + halfSizeB.z*axisXabs.y)
			return false;
		//8 (Ra)x X (Rb)y
		if(fabs(transAtoB.z*axisY.y-transAtoB.y*axisZ.y) > halfSizeA.y*axisZabs.y + 
			halfSizeA.z*axisYabs.y + halfSizeB.x*axisXabs.z + halfSizeB.z*axisXabs.x)
			return false;
		//9 (Ra)x X (Rb)z
		if(fabs(transAtoB.z*axisY.z-transAtoB.y*axisZ.z) > halfSizeA.y*axisZabs.z + 
			halfSizeA.z*axisYabs.z + halfSizeB.x*axisXabs.y + halfSizeB.y*axisXabs.x)
			return false;
		//10 (Ra)y X (Rb)x
		if(fabs(transAtoB.x*axisZ.x-transAtoB.z*axisX.x) > halfSizeA.x*axisZabs.x + 
			halfSizeA.z*axisXabs.x + halfSizeB.y*axisYabs.z + halfSizeB.z*axisYabs.y)
			return false;
		//11 (Ra)y X (Rb)y
		if(fabs(transAtoB.x*axisZ.y-transAtoB.z*axisX.y) > halfSizeA.x*axisZabs.y + 
			halfSizeA.z*axisXabs.y + halfSizeB.x*axisYabs.z + halfSizeB.z*axisYabs.x)
			return false;
		//12 (Ra)y X (Rb)z
		if(fabs(transAtoB.x*axisZ.z-transAtoB.z*axisX.z) > halfSizeA.x*axisZabs.z + 
			halfSizeA.z*axisXabs.z + halfSizeB.x*axisYabs.y + halfSizeB.y*axisYabs.x)
			return false;
		//13 (Ra)z X (Rb)x
		if(fabs(transAtoB.y*axisX.x-transAtoB.x*axisY.x) > halfSizeA.x*axisYabs.x + 
			halfSizeA.y*axisXabs.x + halfSizeB.y*axisZabs.z + halfSizeB.z*axisZabs.y)
			return false;
		//14 (Ra)z X (Rb)y
		if(fabs(transAtoB.y*axisX.y-transAtoB.x*axisY.y) > halfSizeA.x*axisYabs.y + 
			halfSizeA.y*axisXabs.y + halfSizeB.x*axisZabs.z + halfSizeB.z*axisZabs.x)
			return false;
		//15 (Ra)z X (Rb)z
		if(fabs(transAtoB.y*axisX.z-transAtoB.x*axisY.z) > halfSizeA.x*axisYabs.z + 
			halfSizeA.y*axisXabs.z + halfSizeB.x*axisZabs.y + halfSizeB.y*axisZabs.x)
			return false;
		return true;

but how (and from which information) do i calculate the angle now??? thanks for any little tip!!! best regards! anchelito [edit] am i right to think, that i need the length of each overlapping? (the overlap of rA and rB in this pic)

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