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