/// <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> /// Returns wether a models's meshes collides with a terrain /// </summary> /// <param name="m1"></param> /// <param name="t"></param> /// <returns></returns> private static bool CollisionMeshTerrain(ModelGeo m1, Terrain t) { int meshIndex = -1; int polyIndex = -1; int meshPartIndex = -1; for (int i = 0; i < m1.mModel.Meshes.Count; i++) { ModelMesh mesh = m1.mModel.Meshes[i]; for (int j = 0; j < mesh.MeshParts.Count; j++) { ModelMeshPart part = mesh.MeshParts[j]; for (int x = 0; x < part.PrimitiveCount; x++) { Matrix mat = m1.mMatBones[mesh.ParentBone.Index] * m1.mMatWorld; Vector3 a = Vector3.Transform(m1.GetVertex(i, j, x * 3 + 0), mat); Vector3 b = Vector3.Transform(m1.GetVertex(i, j, x * 3 + 1), mat); Vector3 c = Vector3.Transform(m1.GetVertex(i, j, x * 3 + 2), mat); if (a.Y < t.GetHeight(a) || b.Y < t.GetHeight(b) || c.Y < t.GetHeight(c)) { meshIndex = i; meshPartIndex = j; polyIndex = x; n1 = Vector3.Cross((a - b), (c - b)); n2 = t.GetNormal(a); CollisionData.NewEvent(i, -1, n1, n2); //return true; } } } } //Console.WriteLine(fClosestPoly); //return meshIndex != -1 && polyIndex != -1 && meshPartIndex != -1; return CollisionData.Event; }