PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/PhysicsEngines/Bullet/BulletJoint.cs

#
C# | 130 lines | 93 code | 15 blank | 22 comment | 6 complexity | d2c2a4120df0565eee39b9b6517a6e37 MD5 | raw file
Possible License(s): Apache-2.0
  1. using BulletXNA.BulletDynamics;
  2. using Delta.PhysicsEngines.Enums;
  3. using Delta.Utilities.Datatypes;
  4. using Delta.Utilities.Helpers;
  5. using BulletVector = Microsoft.Xna.Framework.Vector3;
  6. using BulletMatrix = Microsoft.Xna.Framework.Matrix;
  7. namespace Delta.PhysicsEngines.Bullet
  8. {
  9. /// <summary>
  10. /// Bullet implementation of PhysicsJoint.
  11. /// </summary>
  12. internal class BulletJoint : PhysicsJoint
  13. {
  14. #region Constraint (Public)
  15. /// <summary>
  16. /// Gets Bullet Constraint.
  17. /// </summary>
  18. public TypedConstraint Constraint
  19. {
  20. get
  21. {
  22. return bulletConstraint;
  23. }
  24. }
  25. #endregion
  26. #region Private
  27. #region bulletPhysics (Private)
  28. private BulletPhysics bulletPhysics;
  29. #endregion
  30. #region bulletConstraint (Private)
  31. /// <summary>
  32. /// Bullet joint.
  33. /// </summary>
  34. private TypedConstraint bulletConstraint;
  35. #endregion
  36. #endregion
  37. #region Constructors
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="BulletJoint"/> class.
  40. /// </summary>
  41. /// <param name="physicsManager">The physics manager.</param>
  42. /// <param name="jointType">Type of the joint.</param>
  43. /// <param name="bodyA">The body A.</param>
  44. /// <param name="bodyB">The body B.</param>
  45. /// <param name="args">The args.</param>
  46. public BulletJoint(
  47. BulletPhysics bulletPhysics,
  48. JointType jointType,
  49. PhysicsBody bodyA,
  50. PhysicsBody bodyB, object[] args)
  51. : base(jointType, bodyA, bodyB, args)
  52. {
  53. this.bulletPhysics = bulletPhysics;
  54. CreateJoint();
  55. }
  56. #endregion
  57. #region Methods (Private)
  58. #region CreateJoint
  59. /// <summary>
  60. /// Creates bullet joint.
  61. /// </summary>
  62. private void CreateJoint()
  63. {
  64. RigidBody rigidBodyA = (BodyA as BulletBody).bulletBody;
  65. RigidBody rigidBodyB = BodyB != null
  66. ? (BodyB as BulletBody).bulletBody
  67. : null;
  68. switch (JointType)
  69. {
  70. case JointType.PointOnPoint:
  71. var localAnchor = BulletDatatypesMapping.Convert(
  72. ArrayHelper.SafeGet<PropertyType, Vector>(
  73. Properties, PropertyType.Anchor1));
  74. // Do we create single body point on point ?
  75. if (rigidBodyB != null)
  76. {
  77. BulletVector pivotInA = BulletDatatypesMapping.Convert(
  78. BodyA.BoundingBox.Center);
  79. var temp = BulletVector.Transform(pivotInA,
  80. rigidBodyA.GetCenterOfMassTransform());
  81. BulletVector pivotInB = BulletVector.Transform(temp,
  82. BulletMatrix.Invert(rigidBodyB.GetCenterOfMassTransform()));
  83. bulletConstraint = new Point2PointConstraint(
  84. rigidBodyA, rigidBodyB, ref pivotInA, ref pivotInB);
  85. }
  86. else
  87. {
  88. bulletConstraint = new Point2PointConstraint(rigidBodyA, ref localAnchor);
  89. }
  90. break;
  91. case JointType.Hinge:
  92. var position = BulletDatatypesMapping.Convert(
  93. ArrayHelper.SafeGet<PropertyType, Vector>(
  94. Properties, PropertyType.Position));
  95. var hingeAxis = BulletDatatypesMapping.Convert(
  96. ArrayHelper.SafeGet<PropertyType, Vector>(
  97. Properties, PropertyType.HingeAxis));
  98. var identity = BulletMatrix.Identity;
  99. // Bullet has even Hinge2 constraing, make usage in future.
  100. if (rigidBodyB != null)
  101. {
  102. bulletConstraint = new HingeConstraint(rigidBodyA, ref identity);
  103. }
  104. else
  105. {
  106. bulletConstraint = new HingeConstraint(rigidBodyA, rigidBodyB,
  107. ref identity, ref identity);
  108. }
  109. break;
  110. }
  111. }
  112. #endregion
  113. #endregion
  114. }
  115. }