/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
- using BulletXNA.BulletDynamics;
- using Delta.PhysicsEngines.Enums;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
- using BulletVector = Microsoft.Xna.Framework.Vector3;
- using BulletMatrix = Microsoft.Xna.Framework.Matrix;
-
- namespace Delta.PhysicsEngines.Bullet
- {
- /// <summary>
- /// Bullet implementation of PhysicsJoint.
- /// </summary>
- internal class BulletJoint : PhysicsJoint
- {
- #region Constraint (Public)
- /// <summary>
- /// Gets Bullet Constraint.
- /// </summary>
- public TypedConstraint Constraint
- {
- get
- {
- return bulletConstraint;
- }
- }
- #endregion
-
- #region Private
-
- #region bulletPhysics (Private)
- private BulletPhysics bulletPhysics;
- #endregion
-
- #region bulletConstraint (Private)
- /// <summary>
- /// Bullet joint.
- /// </summary>
- private TypedConstraint bulletConstraint;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="BulletJoint"/> 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 BulletJoint(
- BulletPhysics bulletPhysics,
- JointType jointType,
- PhysicsBody bodyA,
- PhysicsBody bodyB, object[] args)
- : base(jointType, bodyA, bodyB, args)
- {
- this.bulletPhysics = bulletPhysics;
-
- CreateJoint();
- }
- #endregion
-
- #region Methods (Private)
-
- #region CreateJoint
- /// <summary>
- /// Creates bullet joint.
- /// </summary>
- private void CreateJoint()
- {
- RigidBody rigidBodyA = (BodyA as BulletBody).bulletBody;
- RigidBody rigidBodyB = BodyB != null
- ? (BodyB as BulletBody).bulletBody
- : null;
- switch (JointType)
- {
- case JointType.PointOnPoint:
- var localAnchor = BulletDatatypesMapping.Convert(
- ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Anchor1));
- // Do we create single body point on point ?
- if (rigidBodyB != null)
- {
- BulletVector pivotInA = BulletDatatypesMapping.Convert(
- BodyA.BoundingBox.Center);
-
- var temp = BulletVector.Transform(pivotInA,
- rigidBodyA.GetCenterOfMassTransform());
- BulletVector pivotInB = BulletVector.Transform(temp,
- BulletMatrix.Invert(rigidBodyB.GetCenterOfMassTransform()));
-
- bulletConstraint = new Point2PointConstraint(
- rigidBodyA, rigidBodyB, ref pivotInA, ref pivotInB);
- }
- else
- {
- bulletConstraint = new Point2PointConstraint(rigidBodyA, ref localAnchor);
- }
- break;
- case JointType.Hinge:
- var position = BulletDatatypesMapping.Convert(
- ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.Position));
-
- var hingeAxis = BulletDatatypesMapping.Convert(
- ArrayHelper.SafeGet<PropertyType, Vector>(
- Properties, PropertyType.HingeAxis));
-
- var identity = BulletMatrix.Identity;
- // Bullet has even Hinge2 constraing, make usage in future.
- if (rigidBodyB != null)
- {
- bulletConstraint = new HingeConstraint(rigidBodyA, ref identity);
- }
- else
- {
- bulletConstraint = new HingeConstraint(rigidBodyA, rigidBodyB,
- ref identity, ref identity);
- }
-
- break;
- }
- }
- #endregion
-
- #endregion
- }
- }