/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
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
- using FarseerPhysics.Dynamics.Joints;
- using FarseerPhysics.Factories;
- using Microsoft.Xna.Framework;
- using JointType = Delta.PhysicsEngines.Enums.JointType;
-
- namespace Delta.PhysicsEngines.Farseer
- {
- /// <summary>
- /// Farseer joint implementation.
- /// </summary>
- internal class FarseerJoint : PhysicsJoint
- {
- #region Joint (Public)
- /// <summary>
- /// Gets farseer joint object.
- /// </summary>
- public Joint Joint
- {
- get;
- private set;
- }
- #endregion
-
- #region Private
-
- #region physicsManager (Private)
- private readonly FarseerPhysics physicsManager;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Creates a new instance of the <see cref="FarseerJoint"/> class.
- /// </summary>
- /// <param name="physicsManager">The physics manager.</param>
- /// <param name="jointType">Type of the joint.</param>
- /// <param name="bodyA">The body A.</param>
- /// <param name="bodyB">The body B.</param>
- /// <param name="args">The args.</param>
- public FarseerJoint(
- FarseerPhysics physicsManager,
- JointType jointType,
- PhysicsBody bodyA,
- PhysicsBody bodyB, object[] args)
- : base(jointType, bodyA, bodyB, args)
- {
- this.physicsManager = physicsManager;
-
- CreateJoint();
- }
- #endregion
-
- #region Methods (Private)
-
- #region CreateJoint
- /// <summary>
- /// Create joint from properties.
- /// </summary>
- private void CreateJoint()
- {
- var farseerBodyA = (BodyA as FarseerBody).Body;
- var farseerBodyB = (BodyB as FarseerBody).Body;
-
- Vector tempVector;
- switch (JointType)
- {
- case JointType.Angle:
- AngleJoint angleJoint = JointFactory.CreateAngleJoint(
- physicsManager.farseerPhysicsWorld,
- farseerBodyA,
- farseerBodyB);
-
- angleJoint.MaxImpulse = 3;
- angleJoint.TargetAngle =
- ArrayHelper.SafeGet<PropertyType, float>(
- base.Properties, PropertyType.TargetAngle);
-
- Joint = angleJoint;
- break;
- case JointType.Prismatic:
- PrismaticJoint prismaticJoint = JointFactory.CreatePrismaticJoint(
- farseerBodyA, farseerBodyB, Vector2.Zero, Vector2.Zero);
- Joint = prismaticJoint;
- break;
-
- case JointType.PointPointDistance:
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Anchor1);
- Vector2 anchor1 = FarseerDatatypesMapping.Convert(ref tempVector);
-
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Anchor2);
- Vector2 anchor2 = FarseerDatatypesMapping.Convert(ref tempVector);
-
- var distanceJoint = JointFactory.CreateDistanceJoint(
- physicsManager.farseerPhysicsWorld, farseerBodyA,
- farseerBodyB, anchor1, anchor2);
-
- // Calculate length
- var d = distanceJoint.BodyB.GetWorldPoint(distanceJoint.LocalAnchorB) -
- distanceJoint.BodyA.GetWorldPoint(distanceJoint.LocalAnchorA);
- distanceJoint.Length = d.Length();
- break;
- }
-
- if (Joint != null)
- {
- Joint.CollideConnected = true;
- }
- }
- #endregion
-
- #endregion
- }
- }