summaryrefslogtreecommitdiffstats
path: root/src/jbullet/src/javabullet/dynamics/constraintsolver/ContactConstraint.java
blob: ea07897c3416c4453d6112945ce0a00a2a741896 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Java port of Bullet (c) 2008 Martin Dvorak <jezek2@advel.cz>
 *
 * Bullet Continuous Collision Detection and Physics Library
 * Copyright (c) 2003-2007 Erwin Coumans  http://continuousphysics.com/Bullet/
 *
 * 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.
 */

package javabullet.dynamics.constraintsolver;

import javabullet.BulletGlobals;
import javabullet.BulletPool;
import javabullet.BulletStack;
import javabullet.ObjectPool;
import javabullet.collision.narrowphase.ManifoldPoint;
import javabullet.dynamics.RigidBody;
import javax.vecmath.Matrix3f;
import javax.vecmath.Vector3f;

/**
 *
 * @author jezek2
 */
public class ContactConstraint {
	
	public static final ContactSolverFunc resolveSingleCollision = new ContactSolverFunc() {
		public float invoke(RigidBody body1, RigidBody body2, ManifoldPoint contactPoint, ContactSolverInfo info) {
			return resolveSingleCollision(body1, body2, contactPoint, info);
		}
	};

	public static final ContactSolverFunc resolveSingleFriction = new ContactSolverFunc() {
		public float invoke(RigidBody body1, RigidBody body2, ManifoldPoint contactPoint, ContactSolverInfo info) {
			return resolveSingleFriction(body1, body2, contactPoint, info);
		}
	};

	public static final ContactSolverFunc resolveSingleCollisionCombined = new ContactSolverFunc() {
		public float invoke(RigidBody body1, RigidBody body2, ManifoldPoint contactPoint, ContactSolverInfo info) {
			return resolveSingleCollisionCombined(body1, body2, contactPoint, info);
		}
	};
	
	/**
	 * Bilateral constraint between two dynamic objects.
	 */
	public static void resolveSingleBilateral(RigidBody body1, Vector3f pos1,
			RigidBody body2, Vector3f pos2,
			float distance, Vector3f normal, float[] impulse, float timeStep) {
		float normalLenSqr = normal.lengthSquared();
		assert (Math.abs(normalLenSqr) < 1.1f);
		if (normalLenSqr > 1.1f) {
			impulse[0] = 0f;
			return;
		}

		BulletStack stack = BulletStack.get();
		ObjectPool<JacobianEntry> jacobiansPool = BulletPool.get(JacobianEntry.class);
		
		stack.pushCommonMath();
		try {
			Vector3f rel_pos1 = stack.vectors.get();
			rel_pos1.sub(pos1, body1.getCenterOfMassPosition());

			Vector3f rel_pos2 = stack.vectors.get();
			rel_pos2.sub(pos2, body2.getCenterOfMassPosition());

			//this jacobian entry could be re-used for all iterations

			Vector3f vel1 = stack.vectors.get();
			vel1.set(body1.getVelocityInLocalPoint(rel_pos1));

			Vector3f vel2 = stack.vectors.get();
			vel2.set(body2.getVelocityInLocalPoint(rel_pos2));

			Vector3f vel = stack.vectors.get();
			vel.sub(vel1, vel2);

			Matrix3f mat1 = stack.matrices.get(body1.getCenterOfMassTransform().basis);
			mat1.transpose();

			Matrix3f mat2 = stack.matrices.get(body2.getCenterOfMassTransform().basis);
			mat2.transpose();

			JacobianEntry jac = jacobiansPool.get();
			jac.init(mat1, mat2,
					rel_pos1, rel_pos2, normal, body1.getInvInertiaDiagLocal(), body1.getInvMass(),
					body2.getInvInertiaDiagLocal(), body2.getInvMass());

			float jacDiagAB = jac.getDiagonal();
			float jacDiagABInv = 1f / jacDiagAB;

			Vector3f tmp1 = stack.vectors.get(body1.getAngularVelocity());
			mat1.transform(tmp1);

			Vector3f tmp2 = stack.vectors.get(body2.getAngularVelocity());
			mat2.transform(tmp2);

			float rel_vel = jac.getRelativeVelocity(
					body1.getLinearVelocity(),
					tmp1,
					body2.getLinearVelocity(),
					tmp2);
			
			jacobiansPool.release(jac);
			
			float a;
			a = jacDiagABInv;


			rel_vel = normal.dot(vel);

			// todo: move this into proper structure
			float contactDamping = 0.2f;

			//#ifdef ONLY_USE_LINEAR_MASS
			//	btScalar massTerm = btScalar(1.) / (body1.getInvMass() + body2.getInvMass());
			//	impulse = - contactDamping * rel_vel * massTerm;
			//#else	
			float velocityImpulse = -contactDamping * rel_vel * jacDiagABInv;
			impulse[0] = velocityImpulse;
			//#endif
		}
		finally {
			stack.popCommonMath();
		}
	}

	/**
	 * Response between two dynamic objects with friction.
	 */
	public static float resolveSingleCollision(
			RigidBody body1,
			RigidBody body2,
			ManifoldPoint contactPoint,
			ContactSolverInfo solverInfo) {

		BulletStack stack = BulletStack.get();
		
		stack.vectors.push();
		try {
			Vector3f pos1_ = contactPoint.getPositionWorldOnA();
			Vector3f pos2_ = contactPoint.getPositionWorldOnB();
			Vector3f normal = contactPoint.normalWorldOnB;

			// constant over all iterations
			Vector3f rel_pos1 = stack.vectors.get();
			rel_pos1.sub(pos1_, body1.getCenterOfMassPosition());

			Vector3f rel_pos2 = stack.vectors.get();
			rel_pos2.sub(pos2_, body2.getCenterOfMassPosition());

			Vector3f vel1 = stack.vectors.get(body1.getVelocityInLocalPoint(rel_pos1));
			Vector3f vel2 = stack.vectors.get(body2.getVelocityInLocalPoint(rel_pos2));
			Vector3f vel = stack.vectors.get();
			vel.sub(vel1, vel2);

			float rel_vel;
			rel_vel = normal.dot(vel);

			float Kfps = 1f / solverInfo.timeStep;

			// btScalar damping = solverInfo.m_damping ;
			float Kerp = solverInfo.erp;
			float Kcor = Kerp * Kfps;

			ConstraintPersistentData cpd = (ConstraintPersistentData) contactPoint.userPersistentData;
			assert (cpd != null);
			float distance = cpd.penetration;
			float positionalError = Kcor * -distance;
			float velocityError = cpd.restitution - rel_vel; // * damping;

			float penetrationImpulse = positionalError * cpd.jacDiagABInv;

			float velocityImpulse = velocityError * cpd.jacDiagABInv;

			float normalImpulse = penetrationImpulse + velocityImpulse;

			// See Erin Catto's GDC 2006 paper: Clamp the accumulated impulse
			float oldNormalImpulse = cpd.appliedImpulse;
			float sum = oldNormalImpulse + normalImpulse;
			cpd.appliedImpulse = 0f > sum ? 0f : sum;

			normalImpulse = cpd.appliedImpulse - oldNormalImpulse;

			//#ifdef USE_INTERNAL_APPLY_IMPULSE
			Vector3f tmp = stack.vectors.get();
			if (body1.getInvMass() != 0f) {
				tmp.scale(body1.getInvMass(), contactPoint.normalWorldOnB);
				body1.internalApplyImpulse(tmp, cpd.angularComponentA, normalImpulse);
			}
			if (body2.getInvMass() != 0f) {
				tmp.scale(body2.getInvMass(), contactPoint.normalWorldOnB);
				body2.internalApplyImpulse(tmp, cpd.angularComponentB, -normalImpulse);
			}
			//#else //USE_INTERNAL_APPLY_IMPULSE
			//	body1.applyImpulse(normal*(normalImpulse), rel_pos1);
			//	body2.applyImpulse(-normal*(normalImpulse), rel_pos2);
			//#endif //USE_INTERNAL_APPLY_IMPULSE

			return normalImpulse;
		}
		finally {
			stack.vectors.pop();
		}
	}
	
	public static float resolveSingleFriction(
			RigidBody body1,
			RigidBody body2,
			ManifoldPoint contactPoint,
			ContactSolverInfo solverInfo) {
		
		BulletStack stack = BulletStack.get();
		
		stack.vectors.push();
		try {
			Vector3f pos1 = contactPoint.getPositionWorldOnA();
			Vector3f pos2 = contactPoint.getPositionWorldOnB();

			Vector3f rel_pos1 = stack.vectors.get();
			rel_pos1.sub(pos1, body1.getCenterOfMassPosition());

			Vector3f rel_pos2 = stack.vectors.get();
			rel_pos2.sub(pos2, body2.getCenterOfMassPosition());

			ConstraintPersistentData cpd = (ConstraintPersistentData) contactPoint.userPersistentData;
			assert (cpd != null);

			float combinedFriction = cpd.friction;

			float limit = cpd.appliedImpulse * combinedFriction;

			if (cpd.appliedImpulse > 0f) //friction
			{
				//apply friction in the 2 tangential directions

				// 1st tangent
				Vector3f vel1 = stack.vectors.get();
				vel1.set(body1.getVelocityInLocalPoint(rel_pos1));

				Vector3f vel2 = stack.vectors.get();
				vel2.set(body2.getVelocityInLocalPoint(rel_pos2));

				Vector3f vel = stack.vectors.get();
				vel.sub(vel1, vel2);

				float j1, j2;

				{
					float vrel = cpd.frictionWorldTangential0.dot(vel);

					// calculate j that moves us to zero relative velocity
					j1 = -vrel * cpd.jacDiagABInvTangent0;
					float oldTangentImpulse = cpd.accumulatedTangentImpulse0;
					cpd.accumulatedTangentImpulse0 = oldTangentImpulse + j1;

					cpd.accumulatedTangentImpulse0 = Math.min(cpd.accumulatedTangentImpulse0, limit);
					cpd.accumulatedTangentImpulse0 = Math.max(cpd.accumulatedTangentImpulse0, -limit);
					j1 = cpd.accumulatedTangentImpulse0 - oldTangentImpulse;
				}
				{
					// 2nd tangent

					float vrel = cpd.frictionWorldTangential1.dot(vel);

					// calculate j that moves us to zero relative velocity
					j2 = -vrel * cpd.jacDiagABInvTangent1;
					float oldTangentImpulse = cpd.accumulatedTangentImpulse1;
					cpd.accumulatedTangentImpulse1 = oldTangentImpulse + j2;

					cpd.accumulatedTangentImpulse1 = Math.min(cpd.accumulatedTangentImpulse1, limit);
					cpd.accumulatedTangentImpulse1 = Math.max(cpd.accumulatedTangentImpulse1, -limit);
					j2 = cpd.accumulatedTangentImpulse1 - oldTangentImpulse;
				}

				//#ifdef USE_INTERNAL_APPLY_IMPULSE
				Vector3f tmp = stack.vectors.get();

				if (body1.getInvMass() != 0f) {
					tmp.scale(body1.getInvMass(), cpd.frictionWorldTangential0);
					body1.internalApplyImpulse(tmp, cpd.frictionAngularComponent0A, j1);

					tmp.scale(body1.getInvMass(), cpd.frictionWorldTangential1);
					body1.internalApplyImpulse(tmp, cpd.frictionAngularComponent1A, j2);
				}
				if (body2.getInvMass() != 0f) {
					tmp.scale(body2.getInvMass(), cpd.frictionWorldTangential0);
					body2.internalApplyImpulse(tmp, cpd.frictionAngularComponent0B, -j1);

					tmp.scale(body2.getInvMass(), cpd.frictionWorldTangential1);
					body2.internalApplyImpulse(tmp, cpd.frictionAngularComponent1B, -j2);
				}
				//#else //USE_INTERNAL_APPLY_IMPULSE
				//	body1.applyImpulse((j1 * cpd->m_frictionWorldTangential0)+(j2 * cpd->m_frictionWorldTangential1), rel_pos1);
				//	body2.applyImpulse((j1 * -cpd->m_frictionWorldTangential0)+(j2 * -cpd->m_frictionWorldTangential1), rel_pos2);
				//#endif //USE_INTERNAL_APPLY_IMPULSE
			}
			return cpd.appliedImpulse;
		}
		finally {
			stack.vectors.pop();
		}
	}
	
	/**
	 * velocity + friction<br>
	 * response between two dynamic objects with friction
	 */
	public static float resolveSingleCollisionCombined(
			RigidBody body1,
			RigidBody body2,
			ManifoldPoint contactPoint,
			ContactSolverInfo solverInfo) {
		
		BulletStack stack = BulletStack.get();
		
		stack.vectors.push();
		try {
			Vector3f pos1 = contactPoint.getPositionWorldOnA();
			Vector3f pos2 = contactPoint.getPositionWorldOnB();
			Vector3f normal = contactPoint.normalWorldOnB;

			Vector3f rel_pos1 = stack.vectors.get();
			rel_pos1.sub(pos1, body1.getCenterOfMassPosition());

			Vector3f rel_pos2 = stack.vectors.get();
			rel_pos2.sub(pos2, body2.getCenterOfMassPosition());

			Vector3f vel1 = stack.vectors.get(body1.getVelocityInLocalPoint(rel_pos1));
			Vector3f vel2 = stack.vectors.get(body2.getVelocityInLocalPoint(rel_pos2));
			Vector3f vel = stack.vectors.get();
			vel.sub(vel1, vel2);

			float rel_vel;
			rel_vel = normal.dot(vel);

			float Kfps = 1f / solverInfo.timeStep;

			//btScalar damping = solverInfo.m_damping ;
			float Kerp = solverInfo.erp;
			float Kcor = Kerp * Kfps;

			ConstraintPersistentData cpd = (ConstraintPersistentData) contactPoint.userPersistentData;
			assert (cpd != null);
			float distance = cpd.penetration;
			float positionalError = Kcor * -distance;
			float velocityError = cpd.restitution - rel_vel;// * damping;

			float penetrationImpulse = positionalError * cpd.jacDiagABInv;

			float velocityImpulse = velocityError * cpd.jacDiagABInv;

			float normalImpulse = penetrationImpulse + velocityImpulse;

			// See Erin Catto's GDC 2006 paper: Clamp the accumulated impulse
			float oldNormalImpulse = cpd.appliedImpulse;
			float sum = oldNormalImpulse + normalImpulse;
			cpd.appliedImpulse = 0f > sum ? 0f : sum;

			normalImpulse = cpd.appliedImpulse - oldNormalImpulse;


			//#ifdef USE_INTERNAL_APPLY_IMPULSE
			Vector3f tmp = stack.vectors.get();
			if (body1.getInvMass() != 0f) {
				tmp.scale(body1.getInvMass(), contactPoint.normalWorldOnB);
				body1.internalApplyImpulse(tmp, cpd.angularComponentA, normalImpulse);
			}
			if (body2.getInvMass() != 0f) {
				tmp.scale(body2.getInvMass(), contactPoint.normalWorldOnB);
				body2.internalApplyImpulse(tmp, cpd.angularComponentB, -normalImpulse);
			}
			//#else //USE_INTERNAL_APPLY_IMPULSE
			//	body1.applyImpulse(normal*(normalImpulse), rel_pos1);
			//	body2.applyImpulse(-normal*(normalImpulse), rel_pos2);
			//#endif //USE_INTERNAL_APPLY_IMPULSE

			{
				//friction
				vel1.set(body1.getVelocityInLocalPoint(rel_pos1));
				vel2.set(body2.getVelocityInLocalPoint(rel_pos2));
				vel.sub(vel1, vel2);

				rel_vel = normal.dot(vel);

				tmp.scale(rel_vel, normal);
				Vector3f lat_vel = stack.vectors.get();
				lat_vel.sub(vel, tmp);
				float lat_rel_vel = lat_vel.length();

				float combinedFriction = cpd.friction;

				if (cpd.appliedImpulse > 0) {
					if (lat_rel_vel > BulletGlobals.FLT_EPSILON) {
						lat_vel.scale(1f / lat_rel_vel);

						Vector3f temp1 = stack.vectors.get();
						temp1.cross(rel_pos1, lat_vel);
						body1.getInvInertiaTensorWorld().transform(temp1);

						Vector3f temp2 = stack.vectors.get();
						temp2.cross(rel_pos2, lat_vel);
						body2.getInvInertiaTensorWorld().transform(temp2);

						Vector3f java_tmp1 = stack.vectors.get();
						java_tmp1.cross(temp1, rel_pos1);

						Vector3f java_tmp2 = stack.vectors.get();
						java_tmp2.cross(temp2, rel_pos2);

						tmp.add(java_tmp1, java_tmp2);

						float friction_impulse = lat_rel_vel /
								(body1.getInvMass() + body2.getInvMass() + lat_vel.dot(tmp));
						float normal_impulse = cpd.appliedImpulse * combinedFriction;

						friction_impulse = Math.min(friction_impulse, normal_impulse);
						friction_impulse = Math.max(friction_impulse, -normal_impulse);

						tmp.scale(-friction_impulse, lat_vel);
						body1.applyImpulse(tmp, rel_pos1);

						tmp.scale(friction_impulse, lat_vel);
						body2.applyImpulse(tmp, rel_pos2);
					}
				}
			}

			return normalImpulse;
		}
		finally {
			stack.vectors.pop();
		}
	}

	public static float resolveSingleFrictionEmpty(
			RigidBody body1,
			RigidBody body2,
			ManifoldPoint contactPoint,
			ContactSolverInfo solverInfo) {
		return 0f;
	}
	
}