/PhysicsEngines/Bullet/Helpers.cs
C# | 87 lines | 55 code | 10 blank | 22 comment | 2 complexity | aa63db8dd7c27213458c2efc71b854ff MD5 | raw file
Possible License(s): Apache-2.0
1using BulletXNA.BulletCollision; 2using Delta.Rendering.Models; 3using Delta.Utilities.Datatypes; 4using BulletVector3 = Microsoft.Xna.Framework.Vector3; 5using BulletMatrix = Microsoft.Xna.Framework.Matrix; 6 7namespace Delta.PhysicsEngines.Bullet 8{ 9 /// <summary> 10 /// Helpers class used into the library. 11 /// </summary> 12 public static class Helpers 13 { 14 #region CreateFrom (Static) 15 /// <summary> 16 /// Helper method used for creation of Bullet TriangleMeshShape by 17 /// populating data with vertices and indices gathered from out Mesh. 18 /// </summary> 19 /// <param name="mesh">The mesh to get data from.</param> 20 /// <param name="localSpaceMatrix">The local space matrix.</param> 21 /// <param name="invertTriangles"> 22 /// if set to <c>true</c> we invert winding order of triangles. 23 /// </param> 24 /// <returns></returns> 25 public static TriangleMeshShape CreateFrom(Mesh mesh, 26 Matrix localSpaceMatrix, bool invertTriangles) 27 { 28 // extract the data from the physics mesh 29 TriangleMesh triangleMesh = new TriangleMesh(true, false); 30 31 // apply the local transform of the mesh so we get correct coordinates 32 Matrix TransMat = localSpaceMatrix; 33 34 ushort[] indices = mesh.Geometry.Data.Indices; 35 36 for (int i = 0; i < mesh.Geometry.Data.NumberOfUsedVertices; i += 3) 37 { 38 Vector vertex0 = Vector.Transform( 39 mesh.Geometry.Data.GetPosition(i), TransMat); 40 41 Vector vertex1 = Vector.Transform( 42 mesh.Geometry.Data.GetPosition(i + 1), TransMat); 43 44 Vector vertex2 = Vector.Transform( 45 mesh.Geometry.Data.GetPosition(i + 2), TransMat); 46 47 triangleMesh.AddTriangle( 48 BulletDatatypesMapping.Convert(vertex0), 49 BulletDatatypesMapping.Convert(vertex1), 50 BulletDatatypesMapping.Convert(vertex2)); 51 52 // add Indices too 53 if (invertTriangles) 54 { 55 triangleMesh.AddIndex(indices[i + 2]); 56 triangleMesh.AddIndex(indices[i + 1]); 57 triangleMesh.AddIndex(indices[i]); 58 } 59 else 60 { 61 triangleMesh.AddIndex(indices[i]); 62 triangleMesh.AddIndex(indices[i + 1]); 63 triangleMesh.AddIndex(indices[i + 2]); 64 } 65 } 66 67 // Now create shape and return 68 return new TriangleMeshShape(triangleMesh); 69 } 70 #endregion 71 72 #region GetCenter (Static) 73 /// <summary> 74 /// 75 /// </summary> 76 /// <param name="bbox"></param> 77 /// <returns></returns> 78 public static BulletVector3 GetCenter(BoundingBox bbox) 79 { 80 return new BulletVector3( 81 (bbox.Max.X + bbox.Min.X) * 0.5f, 82 (bbox.Max.Y + bbox.Min.Y) * 0.5f, 83 (bbox.Max.Z + bbox.Min.Z) * 0.5f); 84 } 85 #endregion 86 } 87}