/PhysicsEngines/Jitter/JitterJoint.cs
C# | 355 lines | 264 code | 50 blank | 41 comment | 19 complexity | 3757ba4ab528c43f6c14f1eb9bb3856a MD5 | raw file
Possible License(s): Apache-2.0
- using Delta.PhysicsEngines.Enums;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
- using Jitter.Dynamics;
- using Jitter.Dynamics.Constraints;
- using Jitter.Dynamics.Joints;
- using Jitter.LinearMath;
- using SingleBodyConstraints = Jitter.Dynamics.Constraints.SingleBody;
-
- namespace Delta.PhysicsEngines.Jitter
- {
- /// <summary>
- /// JItter joint implementation
- /// </summary>
- internal class JitterJoint : PhysicsJoint
- {
- #region Constraint (Public)
- /// <summary>
- /// Gets jitter Constraint
- /// </summary>
- public Constraint Constraint
- {
- get;
- private set;
- }
- #endregion
-
- #region Joint (Public)
- /// <summary>
- /// Gets jitter Joint.
- /// </summary>
- public Joint Joint
- {
- get;
- private set;
- }
- #endregion
-
- #region Softness (Public)
- /// <summary>
- /// Defines how big the applied impulses can get.
- /// </summary>
- /// <value>
- /// The softness.
- /// </value>
- public override float Softness
- {
- get
- {
- return base.Softness;
- }
- set
- {
- if (Constraint is FixedAngle)
- {
- (Constraint as FixedAngle).Softness = value;
- }
-
- if (Constraint is PointOnLine)
- {
- (Constraint as PointOnLine).Softness = value;
- }
-
- if (Constraint is PointOnPoint)
- {
- (Constraint as PointOnPoint).Softness = value;
- }
-
- if (
- Constraint is global::Jitter.Dynamics.Constraints.SingleBody.PointOnPoint)
- {
- (Constraint as global::Jitter.Dynamics.Constraints.SingleBody.PointOnPoint)
- .Softness = value;
- }
-
- base.Softness = value;
- }
- }
- #endregion
-
- #region Anchor1 (Public)
- /// <summary>
- /// The anchor 1 point in the world.
- /// </summary>
- /// <value>
- /// The anchor1.
- /// </value>
- public override Vector Anchor1
- {
- get
- {
- return base.Anchor1;
- }
- set
- {
- if (Constraint is PointPointDistance)
- {
- (Constraint as PointPointDistance).LocalAnchor1 =
- JitterDatatypesMapping.Convert(ref value);
- }
- if (
- Constraint is global::Jitter.Dynamics.Constraints.SingleBody.PointOnPoint)
- {
- (Constraint as global::Jitter.Dynamics.Constraints.SingleBody.PointOnPoint)
- .Anchor =
- JitterDatatypesMapping.Convert(ref value);
- }
- base.Anchor1 = value;
- }
- }
- #endregion
-
- #region Anchor2 (Public)
- /// <summary>
- /// The anchor 2 point in the world.
- /// </summary>
- /// <value>
- /// The anchor2.
- /// </value>
- public override Vector Anchor2
- {
- get
- {
- return base.Anchor2;
- }
- set
- {
- if (Constraint is PointPointDistance)
- {
- (Constraint as PointPointDistance).LocalAnchor2 =
- JitterDatatypesMapping.Convert(ref value);
- }
- base.Anchor2 = value;
- }
- }
- #endregion
-
- #region Private
-
- #region physicsManager (Private)
- private readonly JitterPhysics physicsManager;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="JitterJoint"/> 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 JitterJoint(
- JitterPhysics 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>
- /// Creates jitter joint.
- /// </summary>
- private void CreateJoint()
- {
- RigidBody rigidBodyA = (BodyA as JitterBody).Body;
- RigidBody rigidBodyB = BodyB != null
- ? (BodyB as JitterBody).Body
- : null;
-
- Vector tempVector;
- switch (JointType)
- {
- case JointType.FixedAngle:
-
- #region FixedAngle
- // Do we create single body joint?
- if (rigidBodyB != null)
- {
- Constraint = new FixedAngle(rigidBodyA, rigidBodyB);
- }
- else
- {
- Constraint =
- new global::Jitter.Dynamics.Constraints.SingleBody.FixedAngle(rigidBodyA);
- }
- #endregion
-
- break;
-
- case JointType.PointOnLine:
-
- #region PointOnLine
- JVector lineStartPointBody1;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.LineStartPointBody);
- JitterDatatypesMapping.Convert(ref tempVector,
- out lineStartPointBody1);
-
- JVector pointBody2;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.PointBody);
- JitterDatatypesMapping.Convert(ref tempVector,
- out pointBody2);
-
- // Do we create single body joint?
- if (rigidBodyB != null)
- {
- Constraint = new PointOnLine(rigidBodyA, rigidBodyB,
- lineStartPointBody1, pointBody2);
- }
- {
- Log.Warning("You're trying to create PointOnLine with second " +
- "body at null.Maybe you should create SingleBodyPointOnLine.");
- }
- #endregion
-
- break;
- case JointType.PointOnPoint:
-
- #region PointOnPoint
- JVector localAnchor;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Anchor1);
- JitterDatatypesMapping.Convert(ref tempVector, out localAnchor);
-
- // Do we create single body point on point ?
- if (rigidBodyB != null)
- {
- Constraint = new PointOnPoint(rigidBodyA, rigidBodyB, localAnchor);
- }
- else
- {
- Constraint =
- new global::Jitter.Dynamics.Constraints.SingleBody.PointOnPoint(
- rigidBodyA,
- localAnchor);
- }
- #endregion
-
- break;
- case JointType.PointPointDistance:
-
- #region PointPointDistance
- JVector anchor1;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Anchor1);
- JitterDatatypesMapping.Convert(ref tempVector, out anchor1);
-
- JVector anchor2;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Anchor2);
- JitterDatatypesMapping.Convert(ref tempVector, out anchor2);
-
- Constraint = new PointPointDistance(rigidBodyA, rigidBodyB,
- anchor1, anchor2);
- #endregion
-
- break;
-
- case JointType.Hinge:
-
- #region Hinge
- JVector position;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Position);
- JitterDatatypesMapping.Convert(ref tempVector, out position);
-
- JVector hingeAxis;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.HingeAxis);
- JitterDatatypesMapping.Convert(ref tempVector, out hingeAxis);
-
- Joint = new HingeJoint(physicsManager.jitterWorld, rigidBodyA,
- rigidBodyB, position, hingeAxis);
- #endregion
-
- break;
-
- case JointType.Prismatic:
-
- #region Prismatic
- Joint = new PrismaticJoint(
- physicsManager.jitterWorld,
- rigidBodyA,
- rigidBodyB,
- ArrayHelper.SafeGet<PropertyType, float>(
- Properties, PropertyType.MinimumDistance),
- ArrayHelper.SafeGet<PropertyType, float>(
- Properties, PropertyType.MaximumDistance)
- );
-
- float minimumSoftness =
- ArrayHelper.SafeGet<PropertyType, float>(
- Properties, PropertyType.MinimumSoftness);
-
- float maximumSoftness =
- ArrayHelper.SafeGet<PropertyType, float>(
- Properties, PropertyType.MaximumSoftness);
-
- (Joint as PrismaticJoint).MaximumDistanceConstraint.Softness =
- maximumSoftness;
- (Joint as PrismaticJoint).MinimumDistanceConstraint.Softness =
- minimumSoftness;
- #endregion
-
- break;
-
- case JointType.SingleBodyPointOnLine:
-
- #region SingleBodyPointOnLine
- JVector anchor;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Anchor1);
- JitterDatatypesMapping.Convert(ref tempVector, out anchor);
-
- JVector lineDirection;
- tempVector = ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.LineDirection);
- JitterDatatypesMapping.Convert(ref tempVector, out lineDirection);
-
- Constraint =
- new global::Jitter.Dynamics.Constraints.SingleBody.PointOnLine(rigidBodyA,
- anchor, lineDirection);
- #endregion
-
- break;
- }
-
- if (Constraint != null)
- {
- physicsManager.jitterWorld.AddConstraint(Constraint);
- }
-
- if (Joint != null)
- {
- Joint.Activate();
- }
- }
- #endregion
-
- #endregion
- }
- }