IPhysicsMesh CreatePhysicsMesh(MyModel model) { IPhysicsMesh physicsMesh = new MyPhysicsMesh(); physicsMesh.SetAABB(model.BoundingBox.Min, model.BoundingBox.Max); for (int v = 0; v < model.GetVerticesCount(); v++) { Vector3 vertex = model.GetVertex(v); Vector3 normal = model.GetVertexNormal(v); Vector3 tangent = model.GetVertexTangent(v); if (model.TexCoords == null) model.LoadTexCoordData(); Vector2 texCoord = model.TexCoords[v].ToVector2(); physicsMesh.AddVertex(vertex, normal, tangent, texCoord); } for (int i = 0; i < model.Indices16.Length; i++) { physicsMesh.AddIndex(model.Indices16[i]); } for (int i = 0; i < model.GetMeshList().Count; i++) { var mesh = model.GetMeshList()[i]; physicsMesh.AddSectionData(mesh.IndexStart, mesh.TriCount, mesh.Material.Name); } return physicsMesh; }
HkShape CreateShape(MyModel model, HkShapeType shapeType) { switch(shapeType) { case HkShapeType.Box: Vector3 halfExtents = (model.BoundingBox.Max - model.BoundingBox.Min) / 2; return new HkBoxShape(Vector3.Max(halfExtents - 0.1f, new Vector3(0.05f)), 0.02f); break; case HkShapeType.Sphere: return new HkSphereShape(model.BoundingSphere.Radius); break; case HkShapeType.ConvexVertices: m_tmpVerts.Clear(); for (int i = 0; i < model.GetVerticesCount(); i++) { m_tmpVerts.Add(model.GetVertex(i)); } return new HkConvexVerticesShape(m_tmpVerts.GetInternalArray(), m_tmpVerts.Count, true, 0.1f); break; } throw new InvalidOperationException("This shape is not supported"); }