/// <summary> /// Visualizes the BoundingSpheres of a ModelGeo. This method should be properly be moved to the ModelGeo class /// </summary> /// <param name="m"></param> /// <param name="c"></param> public static void ShowBoundingSpheres(ModelGeo m, Color c) { BoundingSphere bs; for (int i = 0; i < m.BoundingSpheres.Count; i++) { bs = m.GetBoundingSphere(i); AddSphere(Matrix.CreateScale(bs.Radius) * Matrix.CreateTranslation(bs.Center), c, false); } }
/// <summary> /// Returns true/false based on whether the segment defined by a and b intersects /// the model m's bounding spheres. /// meshInd report the specific mesh being intersected /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="m"></param> /// <returns></returns> public static bool IntersectSegmentBSphere(Vector3 a, Vector3 b, ModelGeo m, out int meshInd) { //BoundingSphere bs; float t = 0; Vector3 q = Vector3.Zero; meshInd = -1; Vector3 norm = Vector3.Normalize(b - a); for (int i = 0; i < m.BoundingSpheres.Count; i++) { if (IntersectRaySphere(a, norm, m.GetBoundingSphere(i), ref t, ref q) && t <= (a - b).Length()) { meshInd = i; CollisionData.NewEvent(i, -1, norm, -norm); //return true; } } //*/ return CollisionData.Event; // false; }
/// <summary> /// Collision between bounding spheres for c1 and c2 /// Andre Berthiaume, 2008 /// </summary> /// <param name="c1"></param> /// <param name="c2"></param> /// <returns>True if a collision is detected, false otherwise.</returns> private static bool CollisionSphereSphere(ModelGeo c1, ModelGeo c2) { BoundingSphere c1BoundingSphere, c2BoundingSphere; for (int i = 0; i < c1.BoundingSpheres.Count; i++) { // Check whether the bounding spheres intersect. c1BoundingSphere = c1.GetBoundingSphere(i); //Monitor.AddMessage("c1: " + c1BoundingSphere.Center); for (int j = 0; j < c2.BoundingSpheres.Count; j++) { c2BoundingSphere = c2.GetBoundingSphere(j); //Monitor.AddMessage("c2: " + c2BoundingSphere.Center); if (c1BoundingSphere.Intersects(c2BoundingSphere)) { //Console.WriteLine("Sphere Sphere col"); n1 = c2BoundingSphere.Center - c1BoundingSphere.Center; n2 = -n1; CollisionData.NewEvent(i, j, n1, n2); //return true; } // Comparison Counter (pedagogical value only) mComparisonCounter++; } } return CollisionData.Event; }
/// <summary> /// Returns wether a models's bounding spheres (of meshes) collides with a terrain /// </summary> /// <param name="c1"></param> /// <param name="t"></param> /// <returns></returns> private static bool CollisionSphereTerrain(ModelGeo c1, Terrain t) { BoundingSphere c1BoundingSphere; //for (int i = 0; i < c1.mModel.Meshes.Count; i++) for (int i = 0; i < c1.BoundingSpheres.Count; i++) { //c1BoundingSphere = c1.mModel.Meshes[i].BoundingSphere; //c1BoundingSphere.Center = Vector3.Transform(c1BoundingSphere.Center, c1.mModel.Bones[i].Transform * c1.mMatWorld); //c1BoundingSphere.Radius *= c1.mMaxScale; c1BoundingSphere = c1.GetBoundingSphere(i); if (c1BoundingSphere.Center.Y - t.GetHeight(c1BoundingSphere.Center) <= c1BoundingSphere.Radius) { n2 = t.GetNormal(c1BoundingSphere.Center); n1 = -n2; CollisionData.NewEvent(i, -1, n1, n2); //return true; } } //Console.WriteLine(c2.mModel.Meshes[0].MeshParts[0]. ); //return false; return CollisionData.Event; }
/// <summary> /// Bounding sphere-Mesh collision test /// Andre Berhtiaume, 2008 /// </summary> /// <param name="m1"></param> /// <param name="m2"></param> /// <returns>True if a collision is detected, false otherwise.</returns> private static bool CollisionMeshBoundingSphere(ModelGeo m1, ModelGeo m2) { for (int i = 0; i < m2.BoundingSpheres.Count; i++) { if (CollisionMeshBoundingSphere(m1, i, m2.GetBoundingSphere(i))) { return true; } } return false; }