-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_dynamics_world.cpp
More file actions
796 lines (626 loc) · 32.1 KB
/
Copy pathcustom_dynamics_world.cpp
File metadata and controls
796 lines (626 loc) · 32.1 KB
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#include "custom_dynamics_world.h"
void CustomDynamicsWorld::internalSingleStepSimulation(btScalar timeStep)
{
// TODO: Enable this? Don't think it's so important for our purposes here though.
// BT_PROFILE("internalSingleStepSimulation");
if (0 != m_internalPreTickCallback)
{
(*m_internalPreTickCallback)(this, timeStep);
}
btDispatcherInfo& dispatchInfo = getDispatchInfo();
dispatchInfo.m_timeStep = timeStep;
dispatchInfo.m_stepCount = 0;
dispatchInfo.m_debugDraw = getDebugDrawer();
getSolverInfo().m_timeStep = timeStep;
integrateConstrainedBodiesWithCustomPhysics(timeStep);
///update vehicle simulation
updateActions(timeStep);
updateActivationState(timeStep);
if (0 != m_internalTickCallback)
{
(*m_internalTickCallback)(this, timeStep);
}
}
vector<btRigidBody *> collectBodies(btCollisionObjectArray & objects,
vector<btRigidBody *> && buffer = vector<btRigidBody *>())
{
const auto num_obj = objects.size();
for (int i = 0; i < num_obj; ++i)
{
const auto & obj = objects[i];
const auto body = btRigidBody::upcast(obj);
if (body)
{
buffer.push_back(body);
}
}
return buffer;
}
vector<btPersistentManifold*> fetchManifolds (btDynamicsWorld& world){
const auto num_manifolds = world.getDispatcher()->getNumManifolds();
vector<btPersistentManifold*> manifolds;
for (int i = 0; i < num_manifolds; ++i) {
manifolds.push_back(world.getDispatcher()->getManifoldByIndexInternal(i));
}
return manifolds;
}
/* sequential impulses method:
2. Until convergence or maximum number of iterations:
2.1 Apply correctional impulses for joints
2.2 Apply correctional normal impulses
2.3 Apply correctional friction impulses */
void CustomDynamicsWorld::sequentialImpulses(btScalar timeStep){
// filter out the btPoint2PointConstraint instances:
const auto num_constraints = this->getNumConstraints();
vector<btPoint2PointConstraint *> point_constraints;
vector<btHingeConstraint *> hinge_constraints;
vector<btScalar> accumulated_impulses;
for (int i = 0; i < num_constraints; ++i){
auto c = this->getConstraint(i);
if(c->getConstraintType() == POINT2POINT_CONSTRAINT_TYPE){
point_constraints.push_back(dynamic_cast<btPoint2PointConstraint *>(c));
}else if(c->getConstraintType() == HINGE_CONSTRAINT_TYPE){
hinge_constraints.push_back(dynamic_cast<btHingeConstraint *>(c));
accumulated_impulses.push_back(0);
}
}
//fetch all contact Manifolds
auto manifolds = fetchManifolds(*this);
vector<btScalar> target_velocities;
//2
for (int i = 0; i < getConstraintIterations(); i++){
//2.1 Ball joints constraints
if(getApplyBallJointsCorrections()) point2PointConstraintCorrection(point_constraints, timeStep);
if(getApplyHingeJointsCorrections()) hingeJointConstraintCorrection(hinge_constraints, timeStep, accumulated_impulses);
// 2.2 Non-penetration and friction constraints
if(getApplyFrictionCorrections() || getApplyContactCorrections()) manifoldCorrection(manifolds, timeStep, i, target_velocities);
}
}
/*2.1 Foreach Constraint i: Compute Si for the system of the two rigid bodies constrained only by the current constraint in isolation
2.2 Compute an impulse represented by ∆λ˜_i by ∆λ˜_i = S^{−1]_i(−Giu), where u = (u_j , u_k).
2.3 Apply the impulse by updating the velocities of rigid bodies j and k, i.e. u <- u^p + M^{-1}G^T_i ∆λ˜*/
void CustomDynamicsWorld::point2PointConstraintCorrection(vector<btPoint2PointConstraint *> &constraints, btScalar timeStep){
for (auto c : constraints){
btRigidBody& body_j = c->getRigidBodyA();
btRigidBody& body_k = c->getRigidBodyB();
// 2.1
const auto r_j = c->getPivotInA(); // attachement point for body j
const auto r_k = c->getPivotInB(); // attachement point for body k
const auto R_j = body_j.getWorldTransform().getBasis();
const auto R_k = body_k.getWorldTransform().getBasis();
const auto m_inv_j = body_j.getInvMass();
const auto m_inv_k = body_k.getInvMass();
auto tensor_j = body_j.getInvInertiaTensorWorld();
auto tensor_k = body_k.getInvInertiaTensorWorld();
btVector3 v1,v2,v3;
(R_j * r_j).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_j = btMatrix3x3(v1,v2,v3);
(R_k * r_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_k = btMatrix3x3(v1,v2,v3);
//Calculate S = GM^-1G^T
btMatrix3x3 S = I*(m_inv_j+m_inv_k) - K_j*tensor_j*K_j - K_k*tensor_k*K_k;
//2.2
//Get Velocities
btVector3 lV_j = body_j.getLinearVelocity();
btVector3 aV_j = body_j.getAngularVelocity();
btVector3 lV_k = body_k.getLinearVelocity();
btVector3 aV_k = body_k.getAngularVelocity();
//Calculate impulse
btVector3 dC = lV_j-K_j*aV_j-lV_k+K_k*aV_k; // G*u
btVector3 C = (body_j.getCenterOfMassPosition() + R_j*r_j)-(body_k.getCenterOfMassPosition() + R_k*r_k);
btVector3 target_velocity = (-getBallGamma())*C*(1/timeStep) - dC;
btVector3 impulse = {0,0,0};
if(!isZero(S.determinant())){
impulse = S.inverse() * target_velocity;
}
if(isZero(impulse)) continue;
// 2.3
body_j.setLinearVelocity(lV_j + m_inv_j*impulse);
body_j.setAngularVelocity(aV_j + tensor_j*K_j*impulse);
body_k.setLinearVelocity(lV_k - m_inv_k*impulse);
body_k.setAngularVelocity(aV_k - tensor_k*K_k*impulse);
}
}
/**/
void CustomDynamicsWorld::hingeJointConstraintCorrection(vector<btHingeConstraint *> &constraints, btScalar timeStep, vector<btScalar> &accumulated_impulses){
for (int c_ind = 0; c_ind < constraints.size(); c_ind++){
auto c = constraints.at(c_ind);
hingeBallJointConstraint(c, timeStep);
if(getHingeWith2x2()){
hingeCombinedAxisConstraint(c, timeStep);
}else{
for(int i = 0; i < getHingeIterations(); i++){
hingeIndividualAxisConstraint(c, timeStep);
}
}
if(c->getEnableAngularMotor()) hingeMotorConstraint(c, c_ind, timeStep, accumulated_impulses);
}
}
void CustomDynamicsWorld::hingeBallJointConstraint(btHingeConstraint* c, btScalar timeStep){
btRigidBody& body_j = c->getRigidBodyA();
btRigidBody& body_k = c->getRigidBodyB();
const auto r_j = c->getFrameOffsetA().getOrigin(); // Ball joint connection point for body j
const auto r_k = c->getFrameOffsetB().getOrigin(); // Ball joint connection point for body k
const auto R_j = body_j.getWorldTransform().getBasis();
const auto R_k = body_k.getWorldTransform().getBasis();
const auto m_inv_j = body_j.getInvMass();
const auto m_inv_k = body_k.getInvMass();
auto tensor_j = body_j.getInvInertiaTensorWorld();
auto tensor_k = body_k.getInvInertiaTensorWorld();
btVector3 v1,v2,v3;
(R_j * r_j).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_j = btMatrix3x3(v1,v2,v3);
(R_k * r_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_k = btMatrix3x3(v1,v2,v3);
//Calculate S = GM^-1G^T
btMatrix3x3 S = I*(m_inv_j+m_inv_k) - K_j*tensor_j*K_j - K_k*tensor_k*K_k;
//Get Velocities
btVector3 lV_j = body_j.getLinearVelocity();
btVector3 aV_j = body_j.getAngularVelocity();
btVector3 lV_k = body_k.getLinearVelocity();
btVector3 aV_k = body_k.getAngularVelocity();
//Calculate impulse
btVector3 dC = lV_j-K_j*aV_j-lV_k+K_k*aV_k; // G*u
btVector3 C = (body_j.getCenterOfMassPosition() + R_j*r_j)-(body_k.getCenterOfMassPosition() + R_k*r_k);
btVector3 target_velocity = (-getHingeGamma())*C*(1/timeStep) - dC;
btVector3 impulse = {0,0,0};
if(!isZero(S.determinant())){
impulse = S.inverse() * target_velocity;
}
if(!isZero(impulse)){
body_j.setLinearVelocity(lV_j + m_inv_j*impulse);
body_j.setAngularVelocity(aV_j + tensor_j*K_j*impulse);
body_k.setLinearVelocity(lV_k - m_inv_k*impulse);
body_k.setAngularVelocity(aV_k - tensor_k*K_k*impulse);
}
}
void CustomDynamicsWorld::hingeCombinedAxisConstraint(btHingeConstraint* c, btScalar timeStep){
btRigidBody& body_j = c->getRigidBodyA();
btRigidBody& body_k = c->getRigidBodyB();
const auto R_j = body_j.getWorldTransform().getBasis();
const auto R_k = body_k.getWorldTransform().getBasis();
// Bullet stores the orthogonal basis p, q, h as columns of an "offset frame" basis
const auto h_j = c->getFrameOffsetA().getBasis().getColumn(2);
const auto p_k = c->getFrameOffsetB().getBasis().getColumn(0);
const auto q_k = c->getFrameOffsetB().getBasis().getColumn(1);
auto tensor_j = body_j.getInvInertiaTensorWorld();
auto tensor_k = body_k.getInvInertiaTensorWorld();
btVector3 v1,v2,v3;
(R_k * p_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_p = btMatrix3x3(v1,v2,v3);
(R_k * q_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_q = btMatrix3x3(v1,v2,v3);
btVector3 h_world = R_j * h_j;
//Get Velocities
btVector3 aV_j = body_j.getAngularVelocity();
btVector3 aV_k = body_k.getAngularVelocity();
cpMatrix dC(2,1);
dC(0,0) = h_world.dot((K_p*(aV_j - aV_k)));
dC(1,0) = h_world.dot((K_q*(aV_j - aV_k)));
cpMatrix S(2,2);
btMatrix3x3 tensor_sum = tensor_j+tensor_k;
btMatrix3x3 temp = K_p*tensor_sum*K_p;
S(0,0) = -multiplyVector3withMatrix3x3FromBothSides(h_world, temp);
temp = K_p*tensor_sum*K_q;
S(0,1) = -multiplyVector3withMatrix3x3FromBothSides(h_world, temp);
temp = K_q*tensor_sum*K_p;
S(1,0) = -multiplyVector3withMatrix3x3FromBothSides(h_world, temp);
temp = K_q*tensor_sum*K_q;
S(1,1) = -multiplyVector3withMatrix3x3FromBothSides(h_world, temp);
cpMatrix impulse(2,1);
if(!isZero(S.det2x2())){
cpMatrix C(2,1);
C(0,0) = h_world.dot(R_k*p_k);
C(1,0) = h_world.dot(R_k*q_k);
cpMatrix target_velocity = C*(1/timeStep)*getHingeGamma()*(-1);
impulse = S.invert2x2() * (target_velocity - dC);
}
if(!(isZero(impulse(0,0)) && isZero(impulse(1,0)))){
body_j.setAngularVelocity(aV_j - tensor_j*K_p*h_world*impulse(0,0)
- tensor_j*K_q*h_world*impulse(1,0));
body_k.setAngularVelocity(aV_k + tensor_k*K_p*h_world*impulse(0,0)
+ tensor_k*K_q*h_world*impulse(1,0));
}
}
void CustomDynamicsWorld::hingeIndividualAxisConstraint(btHingeConstraint* c, btScalar timeStep){
btRigidBody& body_j = c->getRigidBodyA();
btRigidBody& body_k = c->getRigidBodyB();
const auto R_j = body_j.getWorldTransform().getBasis();
const auto R_k = body_k.getWorldTransform().getBasis();
// Bullet stores the orthogonal basis p, q, h as columns of an "offset frame" basis
const auto h_j = c->getFrameOffsetA().getBasis().getColumn(2);
const auto p_k = c->getFrameOffsetB().getBasis().getColumn(0);
const auto q_k = c->getFrameOffsetB().getBasis().getColumn(1);
auto tensor_j = body_j.getInvInertiaTensorWorld();
auto tensor_k = body_k.getInvInertiaTensorWorld();
btVector3 v1,v2,v3;
(R_k * p_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_p = btMatrix3x3(v1,v2,v3);
(R_k * q_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_q = btMatrix3x3(v1,v2,v3);
btVector3 h_world = R_j * h_j;
//Get Velocities
btVector3 aV_j = body_j.getAngularVelocity();
btVector3 aV_k = body_k.getAngularVelocity();
btScalar dC_p = h_world.dot((K_p*(aV_j - aV_k)));
btScalar dC_q = h_world.dot((K_q*(aV_j - aV_k)));
btMatrix3x3 temp = K_p * (tensor_j + tensor_k) * K_p;
btScalar S_p = -multiplyVector3withMatrix3x3FromBothSides(h_world, temp);
temp = K_q * (tensor_j + tensor_k) * K_q;
btScalar S_q = -multiplyVector3withMatrix3x3FromBothSides(h_world, temp);
btScalar impulse_p = 0;
if(!isZero(S_p)){
btScalar C = h_world.dot(R_k*p_k);
btScalar target_velocity = -getHingeGamma()*C*(1/timeStep);
impulse_p = (target_velocity-dC_p) * (1/S_p);
}
btScalar impulse_q = 0;
if(!isZero(S_q)){
btScalar C = h_world.dot(R_k*q_k);
btScalar target_velocity = -getHingeGamma()*C*(1/timeStep);
impulse_q = (target_velocity-dC_q) * (1/S_q);
}
if(!(isZero(impulse_p) && isZero(impulse_q))){
body_j.setAngularVelocity(aV_j - tensor_j*K_p*h_world*impulse_p
- tensor_j*K_q*h_world*impulse_q);
body_k.setAngularVelocity(aV_k + tensor_k*K_p*h_world*impulse_p
+ tensor_k*K_q*h_world*impulse_q);
}
}
void CustomDynamicsWorld::hingeMotorConstraint(btHingeConstraint* c, int c_ind, btScalar timeStep, vector<btScalar> &accumulated_impulses){
btRigidBody& body_j = c->getRigidBodyA();
btRigidBody& body_k = c->getRigidBodyB();
const auto R_j = body_j.getWorldTransform().getBasis();
const auto R_k = body_k.getWorldTransform().getBasis();
// Bullet stores the orthogonal basis p, q, h as columns of an "offset frame" basis
const auto h_j = c->getFrameOffsetA().getBasis().getColumn(2);
const auto h_k = c->getFrameOffsetB().getBasis().getColumn(2);
auto tensor_j = body_j.getInvInertiaTensorWorld();
auto tensor_k = body_k.getInvInertiaTensorWorld();
btVector3 h = (R_j*h_j + R_k*h_k)/2;
if(h.length() != 0) h.normalize();
btVector3 aV_j = body_j.getAngularVelocity();
btVector3 aV_k = body_k.getAngularVelocity();
btScalar aV_target = c->getMotorTargetVelocity();
btMatrix3x3 tensor_sum = tensor_j+tensor_k;
btScalar S = multiplyVector3withMatrix3x3FromBothSides(h, tensor_sum);
btScalar impulse = 0;
if(!isZero(S)){
btScalar dC = h.dot(aV_j-aV_k) - aV_target;
impulse = (1/S) * -dC;
auto& acc_impulse = accumulated_impulses.at(c_ind);
auto max_impulse = c->getMaxMotorImpulse();
btClamp<btScalar>(impulse, -1 * max_impulse - acc_impulse, max_impulse - acc_impulse);
acc_impulse += impulse;
}
body_j.setAngularVelocity(aV_j + tensor_j*h*impulse);
body_k.setAngularVelocity(aV_k - tensor_k*h*impulse);
}
/*
Combines contactCorrection and frictionCorrection as well as computation of t1, t2 to need to loop over all manifolds only once.
*/
void CustomDynamicsWorld::manifoldCorrection(vector<btPersistentManifold *> &manifolds, btScalar timeStep, int iteration, vector<btScalar> &target_velocities){
auto num_manifolds = this->getDispatcher()->getNumManifolds();
int tV_index = 0;
for(size_t i = 0; i < num_manifolds; ++i){
auto manifold = manifolds[i];
// The const_cast is not nice, but apparently necessary
auto body_j = btRigidBody::upcast(const_cast<btCollisionObject *>(manifold->getBody0()));
auto body_k = btRigidBody::upcast(const_cast<btCollisionObject *>(manifold->getBody1()));
const auto is_rigidJ = body_j != nullptr;
const auto is_rigidK = body_k != nullptr;
if(!is_rigidJ || !is_rigidK) {
//Only do contact handling when both bodies are Rigidbodies
continue;
}
const auto num_contacts = manifold->getNumContacts();
for(int c = 0; c < num_contacts; ++c){
auto& contact = manifold->getContactPoint(c);
//Get all Variables
auto n = contact.m_normalWorldOnB;
const auto r_j = contact.m_localPointA;
const auto r_k = contact.m_localPointB;
auto R_j = body_j->getWorldTransform().getBasis();
auto R_k = body_k->getWorldTransform().getBasis();
const auto m_inv_j = body_j->getInvMass();
const auto m_inv_k = body_k->getInvMass();
auto tensor_j = body_j->getInvInertiaTensorWorld();
auto tensor_k = body_k->getInvInertiaTensorWorld();
btVector3 v1,v2,v3;
(R_j * r_j).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_j = btMatrix3x3(v1,v2,v3);
(R_k * r_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_k = btMatrix3x3(v1,v2,v3);
//Get Velocities
btVector3 lV_j = body_j->getLinearVelocity();
btVector3 aV_j = body_j->getAngularVelocity();
btVector3 lV_k = body_k->getLinearVelocity();
btVector3 aV_k = body_k->getAngularVelocity();
// if first iteration reset applied impulse α1 (and α2) and compute tangent vectors t1 (and t2) for each contact
if (iteration == 0){
// relative velocity at the contact point v_r = derivative C_b = G_b*u
btVector3 v_r = lV_j-K_j*aV_j-lV_k+K_k*aV_k;
// tangent velocity v_T is the projection of v_r onto the tangent plane
btVector3 v_t = v_r - (v_r.dot(n))*n;
if(v_t.length() == 0) contact.m_lateralFrictionDir1 = v_t;
else contact.m_lateralFrictionDir1 = v_t / v_t.length();
contact.m_lateralFrictionDir2 = n.cross(contact.m_lateralFrictionDir1);
if(contact.m_lateralFrictionDir2.length() != 0) contact.m_lateralFrictionDir2.normalize();
if (!getWarmStarting()){
contact.m_appliedImpulse = 0;
contact.m_appliedImpulseLateral1 = 0;
contact.m_appliedImpulseLateral2 = 0;
}
}
auto KJK_j = K_j*tensor_j*K_j;
auto KJK_k = K_k*tensor_k*K_k;
// CONTACT CORRECTION:
if(getApplyContactCorrections()){
btVector3 worldDiff = (body_j->getCenterOfMassPosition() + R_j*r_j)-(body_k->getCenterOfMassPosition() + R_k*r_k);
btScalar C = n.dot(worldDiff);
btScalar dC = n.dot(lV_j)-n.dot(K_j*aV_j)-n.dot(lV_k)+n.dot(K_k*aV_k); //G*u
if(iteration == 0){
btScalar stabilization = (-getContactGamma())*C*(1/timeStep);
btScalar restitution = 0;
if (dC < -(body_k->getGravity().length()/10)) restitution = (-contact.m_combinedRestitution) * dC; // might be better ways for the threshold to be relative to gravity but it works for regular gravity values e.g. (0, -x, 0). (Have not conducted testing for irregular values)
btScalar tV = max(stabilization, restitution);
if(C > epsilon) tV = 0;
target_velocities.push_back(tV);
}
if(C < -epsilon || (isZero(C) && dC < -epsilon)){ //Constraint is only violated when the bodies are already inside each other or if they are currently touching and will be inside each other
btScalar impulse = 0;
if(iteration == 0 && getWarmStarting()){
contact.m_appliedImpulse = contact.m_appliedImpulse * getWarmStartingFactor();
impulse = contact.m_appliedImpulse;
}else{
//We only need this S when we reach this conditional
btScalar S = (m_inv_j + m_inv_k)*n.length2() - multiplyVector3withMatrix3x3FromBothSides(n, KJK_j)
- multiplyVector3withMatrix3x3FromBothSides(n, KJK_k);
if(!isZero(S)){ // S needs to be invertible
impulse = (target_velocities.at(tV_index+c) - dC) * (1/S);
}
if(isZero(impulse)) continue;
if(contact.m_appliedImpulse + impulse < 0) {
impulse = -contact.m_appliedImpulse;
}
contact.m_appliedImpulse += impulse;
}
//apply impulses
body_j->setLinearVelocity(lV_j+m_inv_j*impulse*n);
body_j->setAngularVelocity(aV_j+impulse*(tensor_j*K_j*n));
body_k->setLinearVelocity(lV_k-m_inv_k*impulse*n);
body_k->setAngularVelocity(aV_k-impulse*(tensor_k*K_k*n));
}
}
// FRICTION CORRECTION:
if(getApplyFrictionCorrections()) {
auto t1 = contact.m_lateralFrictionDir1;
auto t2 = contact.m_lateralFrictionDir2;
R_j = body_j->getWorldTransform().getBasis();
R_k = body_k->getWorldTransform().getBasis();
tensor_j = body_j->getInvInertiaTensorWorld();
tensor_k = body_k->getInvInertiaTensorWorld();
(R_j * r_j).getSkewSymmetricMatrix(&v1,&v2,&v3);
K_j = btMatrix3x3(v1,v2,v3);
(R_k * r_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
K_k = btMatrix3x3(v1,v2,v3);
KJK_j = K_j*tensor_j*K_j;
KJK_k = K_k*tensor_k*K_k;
// get new velocities, since we may have changed them for contact correction
lV_j = body_j->getLinearVelocity();
aV_j = body_j->getAngularVelocity();
lV_k = body_k->getLinearVelocity();
aV_k = body_k->getAngularVelocity();
auto mu = contact.m_combinedFriction;
auto lambda = contact.m_appliedImpulse;
btScalar deltaA1 = 0;
btScalar deltaA2 = 0;
if(iteration == 0 && getWarmStarting()){
contact.m_appliedImpulseLateral1 *= getWarmStartingFactor();
contact.m_appliedImpulseLateral2 *= getWarmStartingFactor();
deltaA1 = contact.m_appliedImpulseLateral1;
deltaA2 = contact.m_appliedImpulseLateral2;
}else{
//Calculate S = GM^-1G^T
btScalar S1 = (m_inv_j + m_inv_k)*t1.length2() - multiplyVector3withMatrix3x3FromBothSides(t1, KJK_j)
- multiplyVector3withMatrix3x3FromBothSides(t1, KJK_k);
btScalar S2 = (m_inv_j + m_inv_k)*t2.length2() - multiplyVector3withMatrix3x3FromBothSides(t2, KJK_j)
- multiplyVector3withMatrix3x3FromBothSides(t2, KJK_k);
if(!isZero(S1)){
auto& a1 = contact.m_appliedImpulseLateral1;
btScalar dC1 = t1.dot(lV_j)-t1.dot(K_j*aV_j)-t1.dot(lV_k)+t1.dot(K_k*aV_k);
deltaA1 = dC1 * -1 * (1/S1);
btClamp<btScalar>(deltaA1, -1 * (mu * lambda) - a1, (mu * lambda) - a1);
if(isZero(deltaA1)) deltaA1 = 0;
a1 += deltaA1;
}
if(!isZero(S2)){
auto& a2 = contact.m_appliedImpulseLateral2;
btScalar dC2 = t2.dot(lV_j)-t2.dot(K_j*aV_j)-t2.dot(lV_k)+t2.dot(K_k*aV_k);
deltaA2 = dC2 * -1 * (1/S2);
btClamp<btScalar>(deltaA2, -1 * (mu * lambda) - a2, (mu * lambda) - a2);
if(isZero(deltaA2)) deltaA2 = 0;
a2 += deltaA2;
}
}
//apply impulses
body_j->setLinearVelocity(lV_j+m_inv_j*(deltaA1*t1 + deltaA2*t2));
body_j->setAngularVelocity(aV_j+tensor_j*(K_j*t1*deltaA1 + K_j*t2*deltaA2));
body_k->setLinearVelocity(lV_k-m_inv_k*(deltaA1*t1 + deltaA2*t2));
body_k->setAngularVelocity(aV_k-tensor_k*(K_k*t1*deltaA1 + K_k*t2*deltaA2));
}
}
tV_index += manifold->getNumContacts();
}
}
void CustomDynamicsWorld::integrateConstrainedBodiesWithCustomPhysics(btScalar timeStep) {
// We typically perform collision detection at the beginning of the step
performDiscreteCollisionDetection();
// Collect all instances of btRigidBody (as pointers) in the current simulation. Note: there may also be
// collision objects which are not rigid bodies.
auto bodies = collectBodies(getCollisionObjectArray());
//Update velocity for all bodies (for constrained bodies we calculate u_p here)
for(auto body : bodies){
body->setLinearVelocity(body->getLinearVelocity() + timeStep * body->getInvMass() * body->getTotalForce());
body->setAngularVelocity(body->getAngularVelocity() + body->getInvInertiaTensorWorld() * timeStep * body->getTotalTorque());
}
sequentialImpulses(timeStep);
for (auto body : bodies)
{
auto x = body->getCenterOfMassPosition();
auto q = body->getOrientation();
body->applyGravity();
x += timeStep * body->getLinearVelocity();
btVector3 omega = body->getAngularVelocity();
q += btQuaternion(omega.x(), omega.y(), omega.z(), 0) * q * timeStep * btScalar(0.5f);
q.safeNormalize();
body->setCenterOfMassTransform(btTransform(q, x));
}
// When looping over your constraints, you can use getConstraintIterations() to obtain the number of
// constraint iterations configured in the Project Settings:
// for (int i = 0; i < getConstraintIterations(); ++i)
// {
// // process constraints
// }
// Important types that you may wish to use:
// btVector3 (3-element vector)
// btMatrix3 (3x3 matrix)
// btTransform (encapsulates rotation transform and translation)
// Matrix/vector types support "obvious" functionality, such as:
// v.cross(u)
// v.dot(u)
// a * v (matrix-vector product)
// a * b (matrix-matrix product)
// a * alpha (scaling matrix by scalar)
// Below we have included some examples of things you may want to do
// Given a rigid body `body`:
// const auto x = body->getCenterOfMassPosition();
// const auto q = body->getOrientation();
// const auto v = body->getLinearVelocity();
// const auto omega = body->getAngularVelocity();
// const auto m_inv = body->getInvMass();
// const auto I_inv = body->getInvInertiaTensorWorld();
// const auto f = body->getTotalForce();
// const auto tau = body->getTotalTorque();
// Create quaternion from angular velocity (with 0 "w" component)
// const auto omega_quat = btQuaternion(omega.x(), omega.y(), omega.z(), 0);
// Normalize a quaternion:
// q.safeNormalize();
// Update transform (generalized position) of rigid body with new values q and x
// (note: implicitly also updates "world" inertia tensor)
// body->setCenterOfMassTransform(btTransform(q, x));
// Get rotation matrix for a body
// body->getWorldTransform().getBasis()
}
// =========================== HELPER FUNCTIONS ===========================
#pragma region Helper_Functions
/* calculates the constraint velocity map for a ball joint connecting two rigid bodies.
Parameters
R_j btMatrix3x3 object representing the rotation matrix of the first body.
R_k btMatrix3x3 object representing the rotation matrix of the first body.
r_j btVector3 object representing the attachment point of the first body.
r_k btVector3 object representing the attachment point of the second body.
Returns
cpMatrix object representing the constraint velocity map G. It is a 3x12 matrix that
describes the relationship between the velocities of the two rigid bodies connected by a ball joint.*/
cpMatrix CustomDynamicsWorld::computeConstraintVelocityMap(btMatrix3x3 R_j, btMatrix3x3 R_k,btVector3 r_j, btVector3 r_k){
btVector3 v1,v2,v3;
(R_j * r_j).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_j = btMatrix3x3(v1,v2,v3);
(R_k * r_k).getSkewSymmetricMatrix(&v1,&v2,&v3);
btMatrix3x3 K_k = btMatrix3x3(v1,v2,v3);
cpMatrix G(3,12);
G.initializeBlock(I, 0, 0);
G.initializeBlock(K_j*-1, 0, 3);
G.initializeBlock(I*-1, 0, 6);
G.initializeBlock(K_k, 0, 9);
return G;
}
/* calculates the inverse mass matrix for a pair of rigid bodies.
Parameters
body_j btRigidBody object representing the first rigid body.
body_k btRigidBody object representing the second rigid body.
Returns
cpMatrix object representing the inverse mass matrix M_inv. It is a 12x12 matrix that
is comprised by the inverse mass and inverse inertia tensors of the two rigid bodies.*/
cpMatrix CustomDynamicsWorld::computeInverseMassMatrix(btRigidBody body_j, btRigidBody body_k){
btMatrix3x3 mI_j = I * body_j.getInvMass();
btMatrix3x3 mI_k = I * body_k.getInvMass();
btMatrix3x3 tensor_j = body_j.getInvInertiaTensorWorld();
btMatrix3x3 tensor_k = body_k.getInvInertiaTensorWorld();
// the block-diagonal matrix M = diag(Mj,Mk) // see crash_course.pdf 2.2 end, page 14
cpMatrix M_inv(12,12);
M_inv.initializeBlock(mI_j, 0, 0);
M_inv.initializeBlock(tensor_j, 3, 3);
M_inv.initializeBlock(mI_k, 6, 6);
M_inv.initializeBlock(tensor_k, 9, 9);
return M_inv;
}
/* calculates a 12x1 matrix that represents the linear and angular velocities of the bodies.
Parameters
body_j btRigidBody object representing the first rigid body.
body_k btRigidBody object representing the second rigid body.
Returns
cpMatrix object representing the constraint velocity matrix u_mat. It is a 12x1 matrix that
is comprised by the linear and angular velocities of the two rigid bodies.*/
cpMatrix CustomDynamicsWorld::computeConstraintVelocityMatrix(btRigidBody body_j, btRigidBody body_k){
// Retrieve the linear and angular velocities of the rigid bodies
btVector3 velocities[] = {body_j.getLinearVelocity(), body_j.getAngularVelocity(), body_k.getLinearVelocity(), body_k.getAngularVelocity()};
// construct velocity matrix
cpMatrix u_mat(12,1); //12x1
for(int i = 0; i < 4; i++){
u_mat.setWithBtVector3(velocities[i], i*3, 0);
}
return u_mat;
}
/* modifies the linear and angular velocities of the bodies based on the given impulse vector.
Parameters
body_j btRigidBody object representing the first rigid body.
body_k btRigidBody object representing the second rigid body.
impulse cpMatrix object representing the impulse vector to be applied. The dimensions of impulse should be 12x1.*/
void CustomDynamicsWorld::applyImpulse(btRigidBody& body_j, btRigidBody& body_k, cpMatrix impulse){
body_j.setLinearVelocity( body_j.getLinearVelocity() + impulse.getBtVector3(0,0));
body_j.setAngularVelocity(body_j.getAngularVelocity() + impulse.getBtVector3(3,0));
body_k.setLinearVelocity( body_k.getLinearVelocity() + impulse.getBtVector3(6,0));
body_k.setAngularVelocity(body_k.getAngularVelocity() + impulse.getBtVector3(9,0));
}
/*calculates vec^T*mat*vec which isn't easily possible with only bullet datatypes
Parameters
vec btVector3 that gets multiplied to the matrix.
mat btMatrix3x3 its obvious isnt it.
Returns
btScalar, since the dimensions are 1x3 * 3x3 * 3*1 = 1*1.*/
btScalar CustomDynamicsWorld::multiplyVector3withMatrix3x3FromBothSides(btVector3& vec, btMatrix3x3& mat){
btScalar res = mat[0][0]*vec.getX()*vec.getX() + mat[1][1]*vec.getY()*vec.getY() + mat[2][2]*vec.getZ()*vec.getZ()
+ (mat[1][0]+mat[0][1])*vec.getX()*vec.getY()
+ (mat[2][0]+mat[0][2])*vec.getX()*vec.getZ()
+ (mat[2][1]+mat[1][2])*vec.getY()*vec.getZ();
return res;
}
#pragma endregion Helper_Functions
#pragma region Prints
void CustomDynamicsWorld::printVector(const btVector3& vector, string name) {
cout << "Vector " << name << ":";
for (int i = 0; i < 3; i++) {
cout << dec << vector[i] << ", ";
}
cout << endl;
}
void CustomDynamicsWorld::printMatrix(const btMatrix3x3& matrix, string name) {
cout << "Matrix " << name << ":" << endl;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << dec << matrix[row][col] << ", ";
}
cout << endl;
}
cout << endl;
}
void CustomDynamicsWorld::printQuat(const btQuaternion quat, string name) {
cout << name << ": (";
cout << dec << quat.w() << ", ";
cout << dec << quat.x() << ", ";
cout << dec << quat.y() << ", ";
cout << dec << quat.z() << ")\n";
}
#pragma endregion Prints