/*! \brief Finds the Ray intersection parameter. * \param aabb Aligned box * \param vorigin A vec3f with the origin of the ray * \param vdir A vec3f with the direction of the ray */ public bool CollideRay(ref IndexedVector3 vorigin, ref IndexedVector3 vdir) { IndexedVector3 extents, center; GetCenterExtend(out center, out extents); float Dx = vorigin.X - center.X; if (BoxCollision.BT_GREATER(Dx, extents.X) && Dx * vdir.X >= 0.0f) { return(false); } float Dy = vorigin.Y - center.Y; if (BoxCollision.BT_GREATER(Dy, extents.Y) && Dy * vdir.Y >= 0.0f) { return(false); } float Dz = vorigin.Z - center.Z; if (BoxCollision.BT_GREATER(Dz, extents.Z) && Dz * vdir.Z >= 0.0f) { return(false); } float f = vdir.Y * Dz - vdir.Z * Dy; if (Math.Abs(f) > extents.Y * Math.Abs(vdir.Z) + extents.Z * Math.Abs(vdir.Y)) { return(false); } f = vdir.Z * Dx - vdir.X * Dz; if (Math.Abs(f) > extents.X * Math.Abs(vdir.Z) + extents.Z * Math.Abs(vdir.X)) { return(false); } f = vdir.X * Dy - vdir.Y * Dx; if (Math.Abs(f) > extents.X * Math.Abs(vdir.Y) + extents.Y * Math.Abs(vdir.X)) { return(false); } return(true); }
//! transcache is the transformation cache from box to this AABB public bool OverlappingTransCache(ref AABB box, ref BT_BOX_BOX_TRANSFORM_CACHE transcache, bool fulltest) { //Taken from OPCODE IndexedVector3 ea, eb; //extends IndexedVector3 ca, cb; //extends GetCenterExtend(out ca, out ea); box.GetCenterExtend(out cb, out eb); IndexedVector3 T = new IndexedVector3(0, 0, 0); float t, t2; int i; // Class I : A's basis vectors for (i = 0; i < 3; i++) { T[i] = transcache.m_R1to0[i].Dot(ref cb) + transcache.m_T1to0[i] - ca[i]; t = transcache.m_AR[i].Dot(ref eb) + ea[i]; if (BoxCollision.BT_GREATER(T[i], t)) { return(false); } } // Class II : B's basis vectors for (i = 0; i < 3; i++) { t = Mat3DotCol(ref transcache.m_R1to0, ref T, i); t2 = Mat3DotCol(ref transcache.m_AR, ref ea, i) + eb[i]; if (BoxCollision.BT_GREATER(t, t2)) { return(false); } } // Class III : 9 cross products if (fulltest) { // check to see if these need to be restored back or are read-only float[,] m_R1to0 = MathUtil.BasisMatrixToFloatArray(ref transcache.m_R1to0); float[,] m_AR = MathUtil.BasisMatrixToFloatArray(ref transcache.m_AR); int j, m, n, o, p, q, r; for (i = 0; i < 3; i++) { m = (i + 1) % 3; n = (i + 2) % 3; o = i == 0 ? 1 : 0; p = i == 2 ? 1 : 2; for (j = 0; j < 3; j++) { q = j == 2 ? 1 : 2; r = j == 0 ? 1 : 0; t = T[n] * m_R1to0[m, j] - T[m] * m_R1to0[n, j]; t2 = ea[o] * m_AR[p, j] + ea[p] * m_AR[o, j] + eb[r] * m_AR[i, q] + eb[q] * m_AR[i, r]; if (BoxCollision.BT_GREATER(t, t2)) { return(false); } } } } return(true); }