aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/ModifiedBulletX/Dynamics/ConstraintSolver/HingeConstraint.cs
blob: 3e35550bbc412881d56315536f47e73b3715f21a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
  Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
  Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

using System;
using System.Collections.Generic;
using System.Text;
using MonoXnaCompactMaths;

namespace XnaDevRu.BulletX.Dynamics
{
	/// <summary>
	/// hinge constraint between two rigidbodies each with a pivotpoint that descibes the axis location in local space
	/// axis defines the orientation of the hinge axis
	/// </summary>
	public class HingeConstraint : TypedConstraint
	{
		private JacobianEntry[] _jac = new JacobianEntry[3]; //3 orthogonal linear constraints
		private JacobianEntry[] _jacAng = new JacobianEntry[3]; //2 orthogonal angular constraints + 1 for limit/motor

		private Vector3 _pivotInA;
		private Vector3 _pivotInB;
		private Vector3 _axisInA;
		private Vector3 _axisInB;

		private bool _angularOnly;

		private float _motorTargetVelocity;
		private float _maxMotorImpulse;
		private bool _enableAngularMotor;

		public HingeConstraint(RigidBody rbA, RigidBody rbB, Vector3 pivotInA, Vector3 pivotInB, Vector3 axisInA, Vector3 axisInB)
			: base(rbA, rbB)
		{
			_pivotInA = pivotInA;
			_pivotInB = pivotInB;
			_axisInA = axisInA;
			_axisInB = -axisInB;
			_angularOnly = false;
		}

		public HingeConstraint(RigidBody rbA, Vector3 pivotInA, Vector3 axisInA)
			: base(rbA)
		{
			_pivotInA = pivotInA;
			_pivotInB = MathHelper.MatrixToVector(rbA.CenterOfMassTransform, pivotInA);
			_axisInA = axisInA;
			//fixed axis in worldspace
			_axisInB = MathHelper.TransformNormal(-axisInA, rbA.CenterOfMassTransform);
			_angularOnly = false;
		}

		public HingeConstraint() { }

		public bool AngularOnly { set { _angularOnly = value; } }

		public override void BuildJacobian()
		{
			AppliedImpulse = 0f;

			Vector3 normal = new Vector3();

			if (!_angularOnly)
			{
				for (int i = 0; i < 3; i++)
				{
					MathHelper.SetElement(ref normal, i, 1);
					_jac[i] = new JacobianEntry(
							MatrixOperations.Transpose(RigidBodyA.CenterOfMassTransform),
							MatrixOperations.Transpose(RigidBodyB.CenterOfMassTransform),
							MathHelper.Transform(_pivotInA, RigidBodyA.CenterOfMassTransform) - RigidBodyA.CenterOfMassPosition,
							MathHelper.Transform(_pivotInB, RigidBodyB.CenterOfMassTransform) - RigidBodyB.CenterOfMassPosition,
							normal,
							RigidBodyA.InvInertiaDiagLocal,
							RigidBodyA.InverseMass,
							RigidBodyB.InvInertiaDiagLocal,
							RigidBodyB.InverseMass);
					MathHelper.SetElement(ref normal, i, 0);
				}
			}

			//calculate two perpendicular jointAxis, orthogonal to hingeAxis
			//these two jointAxis require equal angular velocities for both bodies
			//this is unused for now, it's a todo
			Vector3 jointAxisALocal = new Vector3();
			Vector3 jointAxisBLocal = new Vector3();
			MathHelper.PlaneSpace1(_axisInA, ref jointAxisALocal, ref jointAxisBLocal);

			Vector3 jointAxisA = MathHelper.TransformNormal(jointAxisALocal, RigidBodyA.CenterOfMassTransform);
			Vector3 jointAxisB = MathHelper.TransformNormal(jointAxisBLocal, RigidBodyA.CenterOfMassTransform);
			Vector3 hingeAxisWorld = MathHelper.TransformNormal(_axisInA, RigidBodyA.CenterOfMassTransform);

			_jacAng[0] = new JacobianEntry(jointAxisA,
				MatrixOperations.Transpose(RigidBodyA.CenterOfMassTransform),
				MatrixOperations.Transpose(RigidBodyB.CenterOfMassTransform),
				RigidBodyA.InvInertiaDiagLocal,
				RigidBodyB.InvInertiaDiagLocal);

			_jacAng[1] = new JacobianEntry(jointAxisB,
				MatrixOperations.Transpose(RigidBodyA.CenterOfMassTransform),
				MatrixOperations.Transpose(RigidBodyB.CenterOfMassTransform),
				RigidBodyA.InvInertiaDiagLocal,
				RigidBodyB.InvInertiaDiagLocal);

			_jacAng[2] = new JacobianEntry(hingeAxisWorld,
				MatrixOperations.Transpose(RigidBodyA.CenterOfMassTransform),
				MatrixOperations.Transpose(RigidBodyB.CenterOfMassTransform),
				RigidBodyA.InvInertiaDiagLocal,
				RigidBodyB.InvInertiaDiagLocal);
		}

		public override void SolveConstraint(float timeStep)
		{
			Vector3 pivotAInW = MathHelper.Transform(_pivotInA, RigidBodyA.CenterOfMassTransform);
			Vector3 pivotBInW = MathHelper.Transform(_pivotInB, RigidBodyB.CenterOfMassTransform);

			Vector3 normal = new Vector3(0, 0, 0);
			float tau = 0.3f;
			float damping = 1f;

			//linear part
			if (!_angularOnly)
			{
				for (int i = 0; i < 3; i++)
				{
					if (i == 0)
						normal = new Vector3(1, 0, 0);
					else if (i == 1)
						normal = new Vector3(0, 1, 0);
					else
						normal = new Vector3(0, 0, 1);

					float jacDiagABInv = 1f / _jac[i].Diagonal;

					Vector3 rel_pos1 = pivotAInW - RigidBodyA.CenterOfMassPosition;
					Vector3 rel_pos2 = pivotBInW - RigidBodyB.CenterOfMassPosition;

					Vector3 vel1 = RigidBodyA.GetVelocityInLocalPoint(rel_pos1);
					Vector3 vel2 = RigidBodyB.GetVelocityInLocalPoint(rel_pos2);
					Vector3 vel = vel1 - vel2;
					float rel_vel;
					rel_vel = Vector3.Dot(normal, vel);
					//positional error (zeroth order error)
					float depth = -Vector3.Dot(pivotAInW - pivotBInW, normal); //this is the error projected on the normal
					float impulse = depth * tau / timeStep * jacDiagABInv - damping * rel_vel * jacDiagABInv * damping;
					AppliedImpulse += impulse;
					Vector3 impulse_vector = normal * impulse;
					RigidBodyA.ApplyImpulse(impulse_vector, pivotAInW - RigidBodyA.CenterOfMassPosition);
					RigidBodyB.ApplyImpulse(-impulse_vector, pivotBInW - RigidBodyB.CenterOfMassPosition);
				}
			}
			//solve angular part
			// get axes in world space
			Vector3 axisA = MathHelper.TransformNormal(_axisInA, RigidBodyA.CenterOfMassTransform);
			Vector3 axisB = MathHelper.TransformNormal(_axisInB, RigidBodyB.CenterOfMassTransform);

			Vector3 angVelA = RigidBodyA.AngularVelocity;
			Vector3 angVelB = RigidBodyB.AngularVelocity;
			Vector3 angVelAroundHingeAxisA = axisA * Vector3.Dot(axisA, angVelA);
			Vector3 angVelAroundHingeAxisB = axisB * Vector3.Dot(axisB, angVelB);

			Vector3 angAOrthog = angVelA - angVelAroundHingeAxisA;
			Vector3 angBOrthog = angVelB - angVelAroundHingeAxisB;
			Vector3 velrelOrthog = angAOrthog - angBOrthog;

			//solve angular velocity correction
			float relaxation = 1f;
			float len = velrelOrthog.Length();
			if (len > 0.00001f)
			{
				Vector3 normal2 = Vector3.Normalize(velrelOrthog);
				float denom = RigidBodyA.ComputeAngularImpulseDenominator(normal2) +
					RigidBodyB.ComputeAngularImpulseDenominator(normal2);
				// scale for mass and relaxation
				velrelOrthog *= (1f / denom) * 0.9f;
			}

			//solve angular positional correction
			Vector3 angularError = -Vector3.Cross(axisA, axisB) * (1f / timeStep);
			float len2 = angularError.Length();
			if (len2 > 0.00001f)
			{
				Vector3 normal2 = Vector3.Normalize(angularError);
				float denom2 = RigidBodyA.ComputeAngularImpulseDenominator(normal2) +
						RigidBodyB.ComputeAngularImpulseDenominator(normal2);
				angularError *= (1f / denom2) * relaxation;
			}

			RigidBodyA.ApplyTorqueImpulse(-velrelOrthog + angularError);
			RigidBodyB.ApplyTorqueImpulse(velrelOrthog - angularError);

			//apply motor
			if (_enableAngularMotor)
			{
				//todo: add limits too
				Vector3 angularLimit = Vector3.Zero;

				Vector3 velrel = angVelAroundHingeAxisA - angVelAroundHingeAxisB;
				float projRelVel = Vector3.Dot(velrel, axisA);

				float desiredMotorVel = _motorTargetVelocity;
				float motorRelvel = desiredMotorVel - projRelVel;

				float denom3 = RigidBodyA.ComputeAngularImpulseDenominator(axisA) +
						RigidBodyB.ComputeAngularImpulseDenominator(axisA);

				float unclippedMotorImpulse = (1f / denom3) * motorRelvel;
				//todo: should clip against accumulated impulse
				float clippedMotorImpulse = unclippedMotorImpulse > _maxMotorImpulse ? _maxMotorImpulse : unclippedMotorImpulse;
				clippedMotorImpulse = clippedMotorImpulse < -_maxMotorImpulse ? -_maxMotorImpulse : clippedMotorImpulse;
				Vector3 motorImp = clippedMotorImpulse * axisA;

				RigidBodyA.ApplyTorqueImpulse(motorImp + angularLimit);
				RigidBodyB.ApplyTorqueImpulse(-motorImp - angularLimit);
			}
		}

		public void EnableAngularMotor(bool enableMotor, float targetVelocity, float maxMotorImpulse)
		{
			_enableAngularMotor = enableMotor;
			_motorTargetVelocity = targetVelocity;
			_maxMotorImpulse = maxMotorImpulse;
		}

		public void UpdateRHS(float timeStep)
		{
		}
	}
}