PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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