   OBB collision
  Hi I have been sitting with the problem of collision detection with OBB all day. Now I have found a solution all by myself. And it is so little code and so simple I cant but wonder what is wrong with it. I have seen alot of examples using complex matrix multiplications.Anyways heres the code:public bool Intersects(BoundingBox box){   float distance = 0.0f;   Vector3 relativePosition = mPosition - box.mPosition;   for (int i = 0; i < 3; i++)   { distance += projection(mFaces, relativePosition).Length(); distance += projection(box.mFaces, relativePosition).Length();   }   if (relativePosition.Length() > distance) return false;   return true;}This detects collision between a box and a box. Are vector projections slow?
  mybe it is too sample...though,i am not sure it is wrong.here is my code.hope to help you.i am tiger.i am sorry for my english.GTBoolean obb_overlap( //A const GTVector3f& a, //extents const GTVector3f& Pa, //position const GTVector3f* A, //orthonormal basis //B const GTVector3f& b, //extents const GTVector3f& Pb, //position const GTVector3f* B //orthonormal basis ){//translation, in parent frameGTVector3f v = Pb - Pa;//translation, in A's frameGTVector3f T( v.Dot(A[0]), v.Dot(A[1]), v.Dot(A[2]) );//B's basis with respect to A's local frameGTFloat R[3][3];GTFloat ra, rb, t;long i, k;//calculate rotation matrixfor( i=0 ; i<3 ; i++ )for( k=0 ; k<3 ; k++ )R[k] = A.Dot(B[k]); /*ALGORITHM: Use the separating axis test for all 15 potential separating axes. If a separating axis could not be found, the two boxes overlap. *///A's basis vectorsfor( i=0 ; i<3 ; i++ ) {ra = a;rb = b[0]*gtfabs(R[0]) + b[1]*gtfabs(R[1]) + b[2]*gtfabs(R[2]);t = gtfabs( T );if( t > ra + rb ) return false;}//B's basis vectorsfor( k=0 ; k<3 ; k++ )  {ra = a[0]*gtfabs(R[0][k]) + a[1]*gtfabs(R[1][k]) + a[2]*gtfabs(R[2][k]);rb = b[k];t = gtfabs( T[0]*R[0][k] + T[1]*R[1][k] + T[2]*R[2][k] );if( t > ra + rb )return false;}//9 cross products//L = A0 x B0ra = a[1]*gtfabs(R[2][0]) + a[2]*gtfabs(R[1][0]);rb = b[1]*gtfabs(R[0][2]) + b[2]*gtfabs(R[0][1]);t = gtfabs( T[2]*R[1][0] - T[1]*R[2][0] );if( t > ra + rb )return false;//L = A0 x B1ra = a[1]*gtfabs(R[2][1]) + a[2]*gtfabs(R[1][1]);rb = b[0]*gtfabs(R[0][2]) + b[2]*gtfabs(R[0][0]);t = gtfabs( T[2]*R[1][1] - T[1]*R[2][1] );if( t > ra + rb )return false;//L = A0 x B2ra = a[1]*gtfabs(R[2][2]) + a[2]*gtfabs(R[1][2]);rb = b[0]*gtfabs(R[0][1]) + b[1]*gtfabs(R[0][0]);t = gtfabs( T[2]*R[1][2] - T[1]*R[2][2] );if( t > ra + rb )return false;//L = A1 x B0ra = a[0]*gtfabs(R[2][0]) + a[2]*gtfabs(R[0][0]);rb = b[1]*gtfabs(R[1][2]) + b[2]*gtfabs(R[1][1]);t = gtfabs( T[0]*R[2][0] - T[2]*R[0][0] );if( t > ra + rb )return false;//L = A1 x B1ra =a[0]*gtfabs(R[2][1]) + a[2]*gtfabs(R[0][1]);rb =b[0]*gtfabs(R[1][2]) + b[2]*gtfabs(R[1][0]);t = gtfabs( T[0]*R[2][1] - T[2]*R[0][1] );if( t > ra + rb )return false;//L = A1 x B2ra =a[0]*gtfabs(R[2][2]) + a[2]*gtfabs(R[0][2]);rb =b[0]*gtfabs(R[1][1]) + b[1]*gtfabs(R[1][0]);t = gtfabs( T[0]*R[2][2] - T[2]*R[0][2] );if( t > ra + rb )return false;//L = A2 x B0ra = a[0]*gtfabs(R[1][0]) + a[1]*gtfabs(R[0][0]);rb = b[1]*gtfabs(R[2][2]) + b[2]*gtfabs(R[2][1]);t = gtfabs( T[1]*R[0][0] - T[0]*R[1][0] );if( t > ra + rb )return false;//L = A2 x B1ra = a[0]*gtfabs(R[1][1]) + a[1]*gtfabs(R[0][1]);rb = b[0] *gtfabs(R[2][2]) + b[2]*gtfabs(R[2][0]);t = gtfabs( T[1]*R[0][1] - T[0]*R[1][1] );if( t > ra + rb )return false;//L = A2 x B2ra = a[0]*gtfabs(R[1][2]) + a[1]*gtfabs(R[0][2]);rb = b[0]*gtfabs(R[2][1]) + b[1]*gtfabs(R[2][0]);t = gtfabs( T[1]*R[0][2] - T[0]*R[1][2] );if( t > ra + rb )return false;/*no separating axis found,the two boxes overlap */return true;}GTBoolean ModelOBBOverlap(const TModelOBB &me , const TModelOBB &other ){return obb_overlap( me.m_vSize, me.m_vPosition, me.m_vBaise, other.m_vSize, other.m_vPosition, other.m_vBaise);};Quote:Original post by Net-NinjaHi I have been sitting with the problem of collision detection with OBB all day. Now I have found a solution all by myself. And it is so little code and so simple I cant but wonder what is wrong with it. I have seen alot of examples using complex matrix multiplications.Anyways heres the code:public bool Intersects(BoundingBox box){   float distance = 0.0f;   Vector3 relativePosition = mPosition - box.mPosition;   for (int i = 0; i < 3; i++)   { distance += projection(mFaces, relativePosition).Length(); distance += projection(box.mFaces, relativePosition).Length();   }   if (relativePosition.Length() > distance) return false;   return true;}This detects collision between a box and a box. Are vector projections slow?You didn't post all of the code, so it's difficult to evaluate your method. What does projection() do?(Also, use [ source ] tags when posting code.)   ;  I'm guessing, but you are likely checking only the face/face collisions, but what if, as it was pointed out to me earlier, you have one of these collisions?   _____  /\    \    ____ /  \    \ /|    |/    \    \    / |____|\\    \  | /    / \\____\ |/    / .------C    /    / .----C----.| |\  /    / /|    || | \/____/ | |    | | |    | .----..------.    |/____/    edge/edgevertex/vertex Where:  C = point of collisionGeneral rule: If you think you've found a simpler way to do something that has been done to death, you are wrong.   