/// <summary> /// Colisiona un BoundingSphere en movimiento contra el BoundingBox. /// Si hay colision devuelve el instante t de colision mas proximo y el punto q de colision mas cercano /// </summary> /// <param name="sphere">BoundingSphere</param> /// <param name="movementVector">movimiento del BoundingSphere</param> /// <param name="t">Menor instante de colision</param> /// <param name="q">Punto mas cercano de colision</param> /// <param name="n">Normal de la cara colisionada</param> /// <returns>True si hay colision</returns> public override bool intersectMovingSphere(TgcBoundingSphere sphere, Vector3 movementVector, TgcBoundingSphere movementSphere, out float t, out Vector3 q, out Vector3 n) { t = -1f; q = Vector3.Empty; n = Vector3.Empty; // Compute the AABB resulting from expanding b by sphere radius r var e = Aabb.toStruct(); e.min.X -= sphere.Radius; e.min.Y -= sphere.Radius; e.min.Z -= sphere.Radius; e.max.X += sphere.Radius; e.max.Y += sphere.Radius; e.max.Z += sphere.Radius; // Intersect ray against expanded AABB e. Exit with no intersection if ray // misses e, else get intersection point p and time t as result Vector3 p; var ray = new TgcRay.RayStruct(); ray.origin = sphere.Center; ray.direction = movementVector; if (!intersectRayAABB(ray, e, out t, out p) || t > 1.0f) { return(false); } // Compute which min and max faces of b the intersection point p lies // outside of. Note, u and v cannot have the same bits set and // they must have at least one bit set among them var i = 0; var sign = new int[3]; if (p.X < Aabb.PMin.X) { sign[0] = -1; i++; } if (p.X > Aabb.PMax.X) { sign[0] = 1; i++; } if (p.Y < Aabb.PMin.Y) { sign[1] = -1; i++; } if (p.Y > Aabb.PMax.Y) { sign[1] = 1; i++; } if (p.Z < Aabb.PMin.Z) { sign[2] = -1; i++; } if (p.Z > Aabb.PMax.Z) { sign[2] = 1; i++; } //Face if (i == 1) { n = new Vector3(sign[0], sign[1], sign[2]); q = sphere.Center + t * movementVector - sphere.Radius * n; return(true); } // Define line segment [c, c+d] specified by the sphere movement var seg = new Segment(sphere.Center, sphere.Center + movementVector); //Box extent and center var extent = Aabb.calculateAxisRadius(); var center = Aabb.PMin + extent; //Edge if (i == 2) { //Generar los dos puntos extremos del Edge float[] extentDir = { sign[0], sign[1], sign[2] }; var zeroIndex = sign[0] == 0 ? 0 : (sign[1] == 0 ? 1 : 2); extentDir[zeroIndex] = 1; var capsuleA = center + new Vector3(extent.X * extentDir[0], extent.Y * extentDir[1], extent.Z * extentDir[2]); extentDir[zeroIndex] = -1; var capsuleB = center + new Vector3(extent.X * extentDir[0], extent.Y * extentDir[1], extent.Z * extentDir[2]); //Colision contra el Edge hecho Capsula if (intersectSegmentCapsule(seg, new Capsule(capsuleA, capsuleB, sphere.Radius), out t)) { n = new Vector3(sign[0], sign[1], sign[2]); n.Normalize(); q = sphere.Center + t * movementVector - sphere.Radius * n; return(true); } } //Vertex if (i == 3) { var tmin = float.MaxValue; var capsuleA = center + new Vector3(extent.X * sign[0], extent.Y * sign[1], extent.Z * sign[2]); Vector3 capsuleB; capsuleB = center + new Vector3(extent.X * -sign[0], extent.Y * sign[1], extent.Z * sign[2]); if (intersectSegmentCapsule(seg, new Capsule(capsuleA, capsuleB, sphere.Radius), out t)) { tmin = TgcCollisionUtils.min(t, tmin); } capsuleB = center + new Vector3(extent.X * sign[0], extent.Y * -sign[1], extent.Z * sign[2]); if (intersectSegmentCapsule(seg, new Capsule(capsuleA, capsuleB, sphere.Radius), out t)) { tmin = TgcCollisionUtils.min(t, tmin); } capsuleB = center + new Vector3(extent.X * sign[0], extent.Y * sign[1], extent.Z * -sign[2]); if (intersectSegmentCapsule(seg, new Capsule(capsuleA, capsuleB, sphere.Radius), out t)) { tmin = TgcCollisionUtils.min(t, tmin); } if (tmin == float.MaxValue) { return(false); // No intersection } t = tmin; n = new Vector3(sign[0], sign[1], sign[2]); n.Normalize(); q = sphere.Center + t * movementVector - sphere.Radius * n; return(true); // Intersection at time t == tmin } return(false); }