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

/PhysicsEngines/Farseer/FarseerJoint.cs

#
C# | 118 lines | 83 code | 17 blank | 18 comment | 3 complexity | 980c56eec0439e3c6805d17db1b73fdb MD5 | raw file
Possible License(s): Apache-2.0
  1. using Delta.Utilities.Datatypes;
  2. using Delta.Utilities.Helpers;
  3. using FarseerPhysics.Dynamics.Joints;
  4. using FarseerPhysics.Factories;
  5. using Microsoft.Xna.Framework;
  6. using JointType = Delta.PhysicsEngines.Enums.JointType;
  7. namespace Delta.PhysicsEngines.Farseer
  8. {
  9. /// <summary>
  10. /// Farseer joint implementation.
  11. /// </summary>
  12. internal class FarseerJoint : PhysicsJoint
  13. {
  14. #region Joint (Public)
  15. /// <summary>
  16. /// Gets farseer joint object.
  17. /// </summary>
  18. public Joint Joint
  19. {
  20. get;
  21. private set;
  22. }
  23. #endregion
  24. #region Private
  25. #region physicsManager (Private)
  26. private readonly FarseerPhysics physicsManager;
  27. #endregion
  28. #endregion
  29. #region Constructors
  30. /// <summary>
  31. /// Creates a new instance of the <see cref="FarseerJoint"/> class.
  32. /// </summary>
  33. /// <param name="physicsManager">The physics manager.</param>
  34. /// <param name="jointType">Type of the joint.</param>
  35. /// <param name="bodyA">The body A.</param>
  36. /// <param name="bodyB">The body B.</param>
  37. /// <param name="args">The args.</param>
  38. public FarseerJoint(
  39. FarseerPhysics physicsManager,
  40. JointType jointType,
  41. PhysicsBody bodyA,
  42. PhysicsBody bodyB, object[] args)
  43. : base(jointType, bodyA, bodyB, args)
  44. {
  45. this.physicsManager = physicsManager;
  46. CreateJoint();
  47. }
  48. #endregion
  49. #region Methods (Private)
  50. #region CreateJoint
  51. /// <summary>
  52. /// Create joint from properties.
  53. /// </summary>
  54. private void CreateJoint()
  55. {
  56. var farseerBodyA = (BodyA as FarseerBody).Body;
  57. var farseerBodyB = (BodyB as FarseerBody).Body;
  58. Vector tempVector;
  59. switch (JointType)
  60. {
  61. case JointType.Angle:
  62. AngleJoint angleJoint = JointFactory.CreateAngleJoint(
  63. physicsManager.farseerPhysicsWorld,
  64. farseerBodyA,
  65. farseerBodyB);
  66. angleJoint.MaxImpulse = 3;
  67. angleJoint.TargetAngle =
  68. ArrayHelper.SafeGet<PropertyType, float>(
  69. base.Properties, PropertyType.TargetAngle);
  70. Joint = angleJoint;
  71. break;
  72. case JointType.Prismatic:
  73. PrismaticJoint prismaticJoint = JointFactory.CreatePrismaticJoint(
  74. farseerBodyA, farseerBodyB, Vector2.Zero, Vector2.Zero);
  75. Joint = prismaticJoint;
  76. break;
  77. case JointType.PointPointDistance:
  78. tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
  79. Properties, PropertyType.Anchor1);
  80. Vector2 anchor1 = FarseerDatatypesMapping.Convert(ref tempVector);
  81. tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
  82. Properties, PropertyType.Anchor2);
  83. Vector2 anchor2 = FarseerDatatypesMapping.Convert(ref tempVector);
  84. var distanceJoint = JointFactory.CreateDistanceJoint(
  85. physicsManager.farseerPhysicsWorld, farseerBodyA,
  86. farseerBodyB, anchor1, anchor2);
  87. // Calculate length
  88. var d = distanceJoint.BodyB.GetWorldPoint(distanceJoint.LocalAnchorB) -
  89. distanceJoint.BodyA.GetWorldPoint(distanceJoint.LocalAnchorA);
  90. distanceJoint.Length = d.Length();
  91. break;
  92. }
  93. if (Joint != null)
  94. {
  95. Joint.CollideConnected = true;
  96. }
  97. }
  98. #endregion
  99. #endregion
  100. }
  101. }