PageRenderTime 100ms CodeModel.GetById 91ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/PhysicsEngines/Bullet/BulletJoint.cs

#
C# | 130 lines | 93 code | 15 blank | 22 comment | 6 complexity | d2c2a4120df0565eee39b9b6517a6e37 MD5 | raw file
  1using BulletXNA.BulletDynamics;
  2using Delta.PhysicsEngines.Enums;
  3using Delta.Utilities.Datatypes;
  4using Delta.Utilities.Helpers;
  5using BulletVector = Microsoft.Xna.Framework.Vector3;
  6using BulletMatrix = Microsoft.Xna.Framework.Matrix;
  7
  8namespace Delta.PhysicsEngines.Bullet
  9{
 10	/// <summary>
 11	/// Bullet implementation of PhysicsJoint.
 12	/// </summary>
 13	internal class BulletJoint : PhysicsJoint
 14	{
 15		#region Constraint (Public)
 16		/// <summary>
 17		/// Gets Bullet Constraint.
 18		/// </summary>
 19		public TypedConstraint Constraint
 20		{
 21			get
 22			{
 23				return bulletConstraint;
 24			}
 25		}
 26		#endregion
 27
 28		#region Private
 29
 30		#region bulletPhysics (Private)
 31		private BulletPhysics bulletPhysics;
 32		#endregion
 33
 34		#region bulletConstraint (Private)
 35		/// <summary>
 36		/// Bullet joint.
 37		/// </summary>
 38		private TypedConstraint bulletConstraint;
 39		#endregion
 40
 41		#endregion
 42
 43		#region Constructors
 44		/// <summary>
 45		/// Initializes a new instance of the <see cref="BulletJoint"/> class.
 46		/// </summary>
 47		/// <param name="physicsManager">The physics manager.</param>
 48		/// <param name="jointType">Type of the joint.</param>
 49		/// <param name="bodyA">The body A.</param>
 50		/// <param name="bodyB">The body B.</param>
 51		/// <param name="args">The args.</param>
 52		public BulletJoint(
 53			BulletPhysics bulletPhysics,
 54			JointType jointType,
 55			PhysicsBody bodyA,
 56			PhysicsBody bodyB, object[] args)
 57			: base(jointType, bodyA, bodyB, args)
 58		{
 59			this.bulletPhysics = bulletPhysics;
 60
 61			CreateJoint();
 62		}
 63		#endregion
 64
 65		#region Methods (Private)
 66
 67		#region CreateJoint
 68		/// <summary>
 69		/// Creates bullet joint.
 70		/// </summary>
 71		private void CreateJoint()
 72		{
 73			RigidBody rigidBodyA = (BodyA as BulletBody).bulletBody;
 74			RigidBody rigidBodyB = BodyB != null
 75			                       	? (BodyB as BulletBody).bulletBody
 76			                       	: null;
 77			switch (JointType)
 78			{
 79				case JointType.PointOnPoint:
 80					var localAnchor = BulletDatatypesMapping.Convert(
 81						ArrayHelper.SafeGet<PropertyType, Vector>(
 82							Properties, PropertyType.Anchor1));
 83					// Do we create single body point on point ? 
 84					if (rigidBodyB != null)
 85					{
 86						BulletVector pivotInA = BulletDatatypesMapping.Convert(
 87							BodyA.BoundingBox.Center);
 88
 89						var temp = BulletVector.Transform(pivotInA,
 90							rigidBodyA.GetCenterOfMassTransform());
 91						BulletVector pivotInB = BulletVector.Transform(temp,
 92							BulletMatrix.Invert(rigidBodyB.GetCenterOfMassTransform()));
 93
 94						bulletConstraint = new Point2PointConstraint(
 95							rigidBodyA, rigidBodyB, ref pivotInA, ref pivotInB);
 96					}
 97					else
 98					{
 99						bulletConstraint = new Point2PointConstraint(rigidBodyA, ref localAnchor);
100					}
101					break;
102				case JointType.Hinge:
103					var position = BulletDatatypesMapping.Convert(
104						ArrayHelper.SafeGet<PropertyType, Vector>(
105							Properties, PropertyType.Position));
106
107					var hingeAxis = BulletDatatypesMapping.Convert(
108						ArrayHelper.SafeGet<PropertyType, Vector>(
109							Properties, PropertyType.HingeAxis));
110
111					var identity = BulletMatrix.Identity;
112					// Bullet has even Hinge2 constraing, make usage in future.
113					if (rigidBodyB != null)
114					{
115						bulletConstraint = new HingeConstraint(rigidBodyA, ref identity);
116					}
117					else
118					{
119						bulletConstraint = new HingeConstraint(rigidBodyA, rigidBodyB,
120							ref identity, ref identity);
121					}
122
123					break;
124			}
125		}
126		#endregion
127
128		#endregion
129	}
130}