/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
- using BulletXNA.BulletCollision;
- using Delta.Rendering.Models;
- using Delta.Utilities.Datatypes;
- using BulletVector3 = Microsoft.Xna.Framework.Vector3;
- using BulletMatrix = Microsoft.Xna.Framework.Matrix;
-
- namespace Delta.PhysicsEngines.Bullet
- {
- /// <summary>
- /// Helpers class used into the library.
- /// </summary>
- public static class Helpers
- {
- #region CreateFrom (Static)
- /// <summary>
- /// Helper method used for creation of Bullet TriangleMeshShape by
- /// populating data with vertices and indices gathered from out Mesh.
- /// </summary>
- /// <param name="mesh">The mesh to get data from.</param>
- /// <param name="localSpaceMatrix">The local space matrix.</param>
- /// <param name="invertTriangles">
- /// if set to <c>true</c> we invert winding order of triangles.
- /// </param>
- /// <returns></returns>
- public static TriangleMeshShape CreateFrom(Mesh mesh,
- Matrix localSpaceMatrix, bool invertTriangles)
- {
- // extract the data from the physics mesh
- TriangleMesh triangleMesh = new TriangleMesh(true, false);
-
- // apply the local transform of the mesh so we get correct coordinates
- Matrix TransMat = localSpaceMatrix;
-
- ushort[] indices = mesh.Geometry.Data.Indices;
-
- for (int i = 0; i < mesh.Geometry.Data.NumberOfUsedVertices; i += 3)
- {
- Vector vertex0 = Vector.Transform(
- mesh.Geometry.Data.GetPosition(i), TransMat);
-
- Vector vertex1 = Vector.Transform(
- mesh.Geometry.Data.GetPosition(i + 1), TransMat);
-
- Vector vertex2 = Vector.Transform(
- mesh.Geometry.Data.GetPosition(i + 2), TransMat);
-
- triangleMesh.AddTriangle(
- BulletDatatypesMapping.Convert(vertex0),
- BulletDatatypesMapping.Convert(vertex1),
- BulletDatatypesMapping.Convert(vertex2));
-
- // add Indices too
- if (invertTriangles)
- {
- triangleMesh.AddIndex(indices[i + 2]);
- triangleMesh.AddIndex(indices[i + 1]);
- triangleMesh.AddIndex(indices[i]);
- }
- else
- {
- triangleMesh.AddIndex(indices[i]);
- triangleMesh.AddIndex(indices[i + 1]);
- triangleMesh.AddIndex(indices[i + 2]);
- }
- }
-
- // Now create shape and return
- return new TriangleMeshShape(triangleMesh);
- }
- #endregion
-
- #region GetCenter (Static)
- /// <summary>
- ///
- /// </summary>
- /// <param name="bbox"></param>
- /// <returns></returns>
- public static BulletVector3 GetCenter(BoundingBox bbox)
- {
- return new BulletVector3(
- (bbox.Max.X + bbox.Min.X) * 0.5f,
- (bbox.Max.Y + bbox.Min.Y) * 0.5f,
- (bbox.Max.Z + bbox.Min.Z) * 0.5f);
- }
- #endregion
- }
- }