-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.cpp
1378 lines (1095 loc) · 41 KB
/
skeleton.cpp
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "T3D/skeleton.h"
#include "console/console.h"
#include "console/consoleTypes.h"
#include "core/stream/bitStream.h"
#include "ts/tsShapeInstance.h"
#include "math/mTransform.h"
#include "math/mathUtils.h"
IMPLEMENT_CO_DATABLOCK_V1(Bone);
IMPLEMENT_CO_DATABLOCK_V1(JiggleBone);
IMPLEMENT_CO_DATABLOCK_V1(IKChain);
IMPLEMENT_CO_DATABLOCK_V1(JiggleChain);
IMPLEMENT_CO_DATABLOCK_V1(IKRule);
//=================================================================
bool Bone::onAdd()
{
if(!Parent::onAdd())
return false;
ShapeBaseData *db = ((ShapeBaseData*)mTarget);
db->mSkeleton->addBone(this);
return true;
}
void Bone::initPersistFields(){
Parent::initPersistFields();
}
//=================================================================
bool JiggleBone::onAdd()
{
if(!Parent::onAdd())
return false;
mTarget->mSkeleton->addJiggleBone(this);
return true;
}
void JiggleBone::initPersistFields(){
Parent::initPersistFields();
}
void tempJBone::setFromBone(Bone *bone)
{
boneNode = bone->boneNode;
children = bone->children; //storing the nodeID is cleaner and more effecient
//than a full Bone ref, usefull for updating non-bone children(like hands at the end of an arm IK)
parent = bone->parent;
boneVec = bone->boneVec; //vectors to our child from this bone
length = bone->length; //length of our origin to our child
boneName = bone->boneName;
bounds = bone->bounds;
mTarget = bone->mTarget; //the object we're trying to set a Bone to
mDof[0] = bone->mDof[0]; //a min/max for each axis
mDof[1] = bone->mDof[1]; //a min/max for each axis
}
//=================================================================
bool IKChain::onAdd()
{
if(!Parent::onAdd())
return false;
ShapeBaseData* test = mTarget;
if(mTarget)
{
ShapeBaseData *db = ((ShapeBaseData*)mTarget);
db->mSkeleton->addIKChain(this);
}
return true;
}
void IKChain::initPersistFields()
{
Parent::initPersistFields();
addField("mTarget", TYPEID< ShapeBaseData >(), Offset(mTarget, IKChain));
addField("rootBoneName", TypeString, Offset(rootBoneName, IKChain));
addField("endBoneName", TypeString, Offset(endBoneName, IKChain));
addField("tolerance", TypeF32, Offset(tolerance, IKChain));
addField("dontReach", TypeBool, Offset(dontReach, IKChain));
}
//=================================================================
bool JiggleChain::onAdd()
{
if(!Parent::onAdd())
return false;
mTarget->mSkeleton->addJiggleChain(this);
return true;
}
void JiggleChain::initPersistFields(){
Parent::initPersistFields();
}
//=================================================================
// chain IK(arms)
// leg IK(linear to ground)
// look at
bool IKRule::onAdd()
{
if(!Parent::onAdd())
return false;
ShapeBaseData* test = mTarget;
if(mTarget)
{
ShapeBaseData *db = ((ShapeBaseData*)mTarget);
for(U32 i=0; i< db->mSkeleton->ikChains.size(); i++)
if(db->mSkeleton->ikChains[i] == targetChain)
{
db->mSkeleton->addIKRule(this);
}
}
return true;
}
void IKRule::initPersistFields()
{
Parent::initPersistFields();
addField("mTarget", TYPEID< ShapeBaseData >(), Offset(mTarget, IKRule));
addField("TargetChain", TYPEID< IKChain >(), Offset(targetChain, IKRule));
addField("animationName", TypeString, Offset(animationName, IKRule));
addField("goalNodeName", TypeString, Offset(goalNodeName, IKRule));
addField("blendAmount", TypeF32, Offset(blendAmount, IKRule));
addField("offset", TypeF32, Offset(offset, IKRule));
addField("triggerID", TypeS32, Offset(triggerID, IKRule));
addField("matchOrientToGoal", TypeBool, Offset(matchOrientToGoal, IKRule));
}
//=================================================================
Skeleton::Skeleton()
{
}
Skeleton::~Skeleton()
{
}
void Skeleton::CCDIK(TSShapeInstance* shapeInstance, IKChain *ikchain, MatrixF endTrans)
{
Point3F rootPos, curEnd, desiredEnd, endPos = endTrans.getPosition();
VectorF targetVector, curVector, crossResult;
double cosAngle,turnAngle, distCheck;
S32 tries, link;
// start at the last link in the chain
link = ikchain->chain.size();
link -= 1;
tries = 0;
F32 len = 0; F32 dist = 0;
for(S32 i=0; i < ikchain->chain.size(); i++) {
len += ikchain->chain[i]->length; //we can presume(for now) that we're only gunna have linear bone chains
//only use bones we need to?
if(len >= dist){
link = i;
continue;
}
}
Vector<VectorF> pastCurVectors;
Vector<VectorF> pastTargetVectors;
do
{
MatrixF boneTrans = *getBoneTrans(shapeInstance, ikchain->chain[link]);
rootPos = boneTrans.getPosition();
//MatrixF rootLocal = shapeInstance->smNodeLocalTransforms[ikchain->chain[link]->boneNode];
//==================================================================
//End bone position
curEnd = getBoneEndPoint(shapeInstance, ikchain->chain[ikchain->chain.size()-1]);
//End bone position
//==================================================================
desiredEnd = endPos;
VectorF endVec = desiredEnd - curEnd;
double distance = endVec.len();
// see if i'm already close enough
if (distance > ikchain->tolerance)
{
// create the vector to the current effector pos
curVector = curEnd - rootPos;
// create the desired effector position vector
targetVector = endPos - rootPos;
curVector.normalize();
targetVector.normalize();
cosAngle = mDot(curVector, targetVector);
F32 ang = mRadToDeg(cosAngle);
if (cosAngle < 0.9999999)
{
/*if(cosAngle < -0.9999999)
{
//the 2 vectors are collinear
// check if we can use cross product of source vector and [1, 0, 0]
crossResult.set(0, curVector.x, -curVector.y);
if( crossResult.magnitudeSafe() < 1.0e-10 )
{
// nope! we need cross product of source vector and [0, 1, 0]
crossResult.set( -curVector.z, 0, curVector.x ) ;
}
}
else
{
// use the cross product to check which way to rotate
crossResult = mCross(curVector, targetVector);
}
crossResult.normalize();
turnAngle = mAcos(cosAngle); // get the angle
if(turnAngle > M_PI / 12) //max angle change
{
turnAngle = M_PI / 12;
}*/
QuatF rot, tester = QuatF(boneTrans);
rot.shortestArc(curVector, targetVector);
MatrixF oldBoneTrans = boneTrans;
TSTransform::setMatrix(rot, boneTrans.getPosition(), &boneTrans);
//check dof's
MatrixF newBoneTrans = CheckDofsRestrictions(ikchain->chain[link], boneTrans);
//storeBoneTrans(shapeInstance, ikchain->chain[link]->boneNode, newBoneTrans);
shapeInstance->mNodeTransforms[ikchain->chain[link]->boneNode] = newBoneTrans;
updateChildren(shapeInstance, ikchain->chain[link], ikchain);
}
if (--link < 0)
link = ikchain->chain.size()-1; // START OF THE CHAIN, RESTART
}
// quit if i am close enough or been running long enough
} while (++tries < 60 &&
//curEnd.SquaredDistance(desiredEnd) > ikchain->tolerance);
VectorF(desiredEnd - getBoneEndPoint(shapeInstance, ikchain->chain[ikchain->chain.size()-1])).len() > ikchain->tolerance);
}
void Skeleton::physicalIK(TSShapeInstance* shapeInstance, IKChain *ikchain, MatrixF endTrans, F32 dt)
{
//make a temp copy of the ikchain as a jiggle chian
Vector<tempJBone*> physicalBones;
Vector<mass*> masses;
mass *m;
for(U32 i=0; i<ikchain->chain.size(); i++)
{
tempJBone* jb = new tempJBone();
jb->setFromBone(ikchain->chain[i]);
//we don't need gravity simulation on the physical IK
jb->specificGravity = 0.f;
physicalBones.push_front(jb);
m = new mass();
m->boneNum = physicalBones[i]->boneNode;
masses.push_front(m);
}
//now that we have an impromptu jChain, simulate the end bone towards our goal point
S32 count = physicalBones.size()-1;
S32 tries = 0;
VectorF dir;
do{
storeBoneTrans(shapeInstance, ikchain->chain[count]->boneNode, endTrans);
//solve the 'springs' first
for(U32 x=0; x<count; x++) {
//solveJiggleBone(shapeInstance, physicalBones[x], masses[x]);
Point3F rootPos = getBoneTrans(shapeInstance, findBone(physicalBones[x]->parent))->getPosition();
Point3F endPos = getBoneTrans(shapeInstance,physicalBones[x])->getPosition();
VectorF springVector = rootPos - endPos; //vector between the two masses
VectorF force = VectorF(0,0,0); //force initially has a zero value
//solve the 'spring' between the two bones
F32 vecLen = springVector.len(); //distance between the two masses
if (vecLen != 0) //to avoid a division by zero check if r is zero
force += (springVector / vecLen) * (vecLen - physicalBones[x]->length);// * (-physicalBones[x]->stiffness); //the spring force is added to the force
VectorF vel = physicalBones[x]->velocity * physicalBones[x]->moveFriction; //The air friction
if(x<count-1)
force += -(vel - physicalBones[x+1]->velocity) * physicalBones[x]->moveFriction.z; //the friction force is added to the force
else
force += -(vel) * physicalBones[x]->moveFriction.z; //with this addition we obtain the net force of the spring
VectorF b = (VectorF(0,0,-9.81f) * physicalBones[x]->specificGravity) * physicalBones[x]->mass;
force += b;//(VectorF(0,0,-9.81f) * jB->specificGravity) * jB->mass; //The gravitational force
masses[x]->force += force; //net forces
}
//then simulate/update the bones
for(U32 y=0; y<count; y++){
updateJiggleBone(shapeInstance, physicalBones[y], masses[y], dt);
updateChildren(shapeInstance, physicalBones[y], ikchain);
}
MatrixF boneTrans = *getBoneTrans(shapeInstance, ikchain->chain[count]);
dir = endTrans.getPosition() - boneTrans.getPosition();
}
while(++tries < 3 && dir.len() > ikchain->tolerance);
}
void Skeleton::analiticalIK(TSShapeInstance* shapeInstance, IKChain *ikchain, MatrixF endTrans)
{
/// Local Variables ///////////////////////////////////////////////////////////
/*float l1,l2; // BONE LENGTH FOR BONE 1 AND 2
float ex,ey; // ADJUSTED TARGET POSITION
float sin2,cos2; // SINE AND COSINE OF ANGLE 2
float angle1,angle2;// ANGLE 1 AND 2 IN RADIANS
float tan1; // TAN OF ANGLE 1
///////////////////////////////////////////////////////////////////////////////
// SUBTRACT THE INITIAL OFFSET FROM THE TARGET POS
ex = endPos.x - (m_UpArm.trans.x * m_ModelScale);
ey = endPos.y - (m_UpArm.trans.y * m_ModelScale);
// MULTIPLY THE BONE LENGTHS BY THE WINDOW SCALE
l1 = m_LowArm.trans.x * m_ModelScale;
l2 = m_Effector.trans.x * m_ModelScale;
// CALCULATE THE COSINE OF ANGLE 2
cos2 = ((ex * ex) + (ey * ey) - (l1 * l1) - (l2 * l2)) / (2 * l1 * l2);
// IF IT IS NOT IN THIS RANGE, IT IS UNREACHABLE
if (cos2 >= -1.0 && cos2 <= 1.0)
{
angle2 = (float)acos(cos2); // GET THE ANGLE WITH AN ARCCOSINE
m_LowArm.rot.z = RADTODEG(angle2); // CONVERT IT TO DEGREES
sin2 = (float)sin(angle2); // CALC THE SINE OF ANGLE 2
// COMPUTE ANGLE 1
// HERE IS WHERE THE BUG WAS SEE THE README.TXT FOR MORE INFO
// CALCULATE THE TAN OF ANGLE 1
tan1 = (-(l2 * sin2 * ex) + ((l1 + (l2 * cos2)) * ey)) /
((l2 * sin2 * ey) + ((l1 + (l2 * cos2)) * ex));
// GET THE ACTUAL ANGLE
angle1 = atan(tan1);
m_UpArm.rot.z = RADTODEG(angle1); // CONVERT IT TO DEGREES
return TRUE;
}
else
return FALSE;*/
}
MatrixF Skeleton::CheckDofsRestrictions(Bone *bone, MatrixF mat)
{
EulerF angles = mat.toEuler();
bool modified = false;
F32 xMin = mDegToRad(bone->mDof[0].x);
F32 xMax = mDegToRad(bone->mDof[1].x);
if(angles.x < xMin)
{
angles.x = xMin;
modified = true;
}
else
if(angles.x > xMax)
{
angles.x = xMax;
modified = true;
}
F32 yMin = mDegToRad(bone->mDof[0].y);
F32 yMax = mDegToRad(bone->mDof[1].y);
if(angles.y < yMin)
{
angles.y = yMin;
modified = true;
}
else
if(angles.y > yMax)
{
angles.y = yMax;
modified = true;
}
F32 zMin = mDegToRad(bone->mDof[0].z);
F32 zMax = mDegToRad(bone->mDof[1].z);
if(angles.z < zMin)
{
angles.z = zMin;
modified = true;
}
else
if(angles.z > zMax)
{
angles.z = zMax;
modified = true;
}
if(modified)
{
QuatF q = QuatF(angles);
MatrixF moddedXfm;
TSTransform::setMatrix(q, mat.getPosition(), &moddedXfm);
return moddedXfm;
}
return mat;
}
//helper function to the main IK, takes a direction and the given bone, and we compile out a transform for the destired rotation
MatrixF Skeleton::directionToMatrix(VectorF dir, VectorF up)
{
MatrixF matr;
AngAxisF aAng;
VectorF MatX = mCross(dir, up); // the x-column of our 3x3 matrix. perpendicular to both %vDirection and %vUp.
matr.setColumn(0, MatX);
matr.setColumn(1, dir); // the y-column of our 3x3 matrix. we're just saying "be what i want".
matr.setColumn(2, mCross(MatX, dir)); // the z-column of our 3x3 matrix. perpendicular to X and Y.
matr.setPosition(Point3F(0,0,0));
F32 * mat = matr;
F32 theta = mAcos((mat[0,0] + mat[1,1] + mat[2,2] - 1) / 2);
F32 eps = 0.0001;
if(mAbs(theta) < eps){
// singularity at zero
//return "0 0 0 0 0 1 0";
}
else if ( mAbs(3.14159265359 - theta) < eps)
{
// ditto at 180, but i leave it to you to grok the whole largest-diagonal thing at that site.
//return "0 0 0 0 0 1 0";
}
F32 denom = mSqrt((mat[2,1] - mat[1,2]) * (mat[2,1] - mat[1,2]) +
(mat[0,2] - mat[2,0]) * (mat[0,2] - mat[2,0]) +
(mat[1,0] - mat[0,1]) * (mat[1,0] - mat[0,1]));
aAng.axis.x = (mat[2,1] - mat[1,2]) / denom;
aAng.axis.y = (mat[0,2] - mat[2,0]) / denom;
aAng.axis.z = (mat[1,0] - mat[0,1]) / denom;
aAng.angle = theta;
TransformF mats(Point3F(0,0,0), aAng);
return mats.getMatrix();
}
void Skeleton::updateChildren(TSShapeInstance* shapeInstance, Bone *current, IKChain *chain)
{
F32 boneCount = chain->chain.size() - 1;
//first update our root
MatrixF parentTrans, curTrans, newTrans;
for(U32 i = 1; i < boneCount - 1; i++)
{
if(i==0)
parentTrans = shapeInstance->mNodeTransforms[current->boneNode];
else
parentTrans = getLocalBoneTrans(shapeInstance, chain->chain[i-1]);
curTrans = getLocalBoneTrans(shapeInstance, chain->chain[i]); //our current
newTrans.mul(parentTrans, curTrans);
storeBoneTrans(shapeInstance, chain->chain[i]->boneNode, newTrans);
}
}
MatrixF Skeleton::setForwardVector(MatrixF *mat, VectorF axisY, VectorF up)
{
VectorF axisX;
VectorF axisZ;
// Validate and normalize input:
F32 lenSq;
lenSq = axisY.lenSquared();
if (lenSq < 0.000001f)
{
axisY.set(0.0f, 1.0f, 0.0f);
Con::errorf("SceneObject::setForwardVector() - degenerate forward vector");
}
else
{
axisY /= mSqrt(lenSq);
}
lenSq = up.lenSquared();
if (lenSq < 0.000001f)
{
up.set(0.0f, 0.0f, 1.0f);
Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
}
else
{
up /= mSqrt(lenSq);
}
if (fabsf(mDot(up, axisY)) > 0.9999f)
{
Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
// i haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
F32 tmp = up.x;
up.x = -up.y;
up.y = up.z;
up.z = tmp;
}
// construct the remaining axes:
mCross(axisY, up , &axisX);
mCross(axisX, axisY, &axisZ);
mat->setColumn(0, axisX);
mat->setColumn(1, axisY);
mat->setColumn(2, axisZ);
return *mat;
}
void Skeleton::updateChildren(TSShapeInstance* shapeInstance, Bone *parent)
{
for(S32 i=0; i<parent->children.size(); i++)
{
Bone *child = NULL;
if(isBone(parent->children[i], child))
{
MatrixF childTrans = *getBoneTrans(shapeInstance, child);
childTrans.mul(*getBoneTrans(shapeInstance, parent));
storeBoneTrans(shapeInstance, child->boneNode, childTrans);
if(meshShape.nodes[child->boneNode].firstChild != -1)
updateChildren(shapeInstance, child);
}
}
}
/*void Skeleton::updateChildren(TSShapeInstance* shapeInstance, Bone *parent)
{
//parse through any children we may have in our skeleton system
for(S32 i=0; i<parent->children.size(); i++)
{
Bone *child = NULL;
MatrixF newOrient;
//confirm it's actually a bone
if(isBone(parent->children[i], child))
{
MatrixF *childMat = getBoneTrans(shapeInstance, child);
MatrixF *parentMat = getBoneTrans(shapeInstance, parent);
VectorF curForVec, newVec, parOrient, testOrient, childOrient;
Point3F endPos = getBoneEndPoint(shapeInstance, parent);
curForVec = getBoneForwardVector(shapeInstance, parent);
//since these never change, use them as a reference
VectorF homeBoneVec = meshShape.defaultTranslations[child->boneNode] -
meshShape.defaultTranslations[parent->boneNode];
//take the difference of the default vector and the current vector, and the parent's end position
//newOrient = MatrixF(homeBoneVec+curForVec, parentMat->getPosition()/*+endPos*/ /*+ meshShape.defaultTranslations[child->boneNode]);
newOrient = MatrixF(homeBoneVec+curForVec, endPos);
//store the update, which is applied at the owner's discretion
//setBoneTrans(child, newOrient);
storeBoneTrans(child->boneNode, newOrient);
if(meshShape.nodes[child->boneNode].firstChild != -1)
updateChildren(shapeInstance, child);
}
//it's not? well update it so we don't have wayward nodes
//else
// if(meshShape.nodes[parent->boneNode].firstChild != -1)
// updateChildren(shapeInstance, parent->boneNode);
}
//check if we have normal nodes as children
//if(meshShape.nodes[parent->boneNode].firstChild != -1)
//if we do, then update them
// updateChildren(shapeInstance, parent->boneNode);
}*/
//used for updating non-skeleton bone based bone nodes in the mesh(child bones)
void Skeleton::updateChildren(TSShapeInstance* shapeInstance, S32 parent)
{
S32 childIdx = meshShape.nodes[parent].firstChild;
//get our first child, then step through the siblings
MatrixF newOrient;
VectorF curForVec, newVec, parOrient, testOrient, childOrient, forVec;
shapeInstance->mNodeTransforms[childIdx].getColumn(0,&forVec);
forVec.normalize();
Point3F first = meshShape.defaultTranslations[childIdx];
Point3F second = meshShape.defaultTranslations[parent];
VectorF vec = second - first;
Point3F endPoint = forVec * vec.len();
//since these never change, use them as a reference
VectorF homeBoneVec = meshShape.defaultTranslations[childIdx] -
meshShape.defaultTranslations[parent];
newOrient = MatrixF(homeBoneVec+forVec, endPoint);
//store to apply later
// meshInstance->mNodeTransforms[childIdx] = newOrient;
storeBoneTrans(shapeInstance, childIdx, newOrient);
if(meshShape.nodes[childIdx].firstChild != -1)
updateChildren(shapeInstance, childIdx);
//now walk through the siblings, if we have any
while (meshShape.nodes[childIdx].nextSibling>=0)
{
S32 sib = meshShape.nodes[childIdx].nextSibling;
VectorF curForVec, newVec, parOrient, testOrient, childOrient;
shapeInstance->mNodeTransforms[sib].getColumn(0,&forVec);
forVec.normalize();
Point3F first = meshShape.defaultTranslations[sib];
Point3F second = meshShape.defaultTranslations[parent];
VectorF vec = second - first;
Point3F endPoint = forVec * vec.len();
//since these never change, use them as a reference
VectorF homeBoneVec = meshShape.defaultTranslations[sib] - meshShape.defaultTranslations[parent];
newOrient = MatrixF(homeBoneVec+forVec, endPoint);
//store to apply later
// meshInstance->mNodeTransforms[sib] = newOrient;
storeBoneTrans(shapeInstance, sib, newOrient);
if(meshShape.nodes[sib].firstChild != -1)
updateChildren(shapeInstance, sib);
}
}
void Skeleton::updateChildren(TSShapeInstance* shapeInstance, Bone *parent, MatrixF newTrans)
{
for(S32 i=0; i<parent->children.size(); i++)
{
Bone *child = NULL;
isBone(parent->children[i], child);
MatrixF *childMat = getBoneTrans(shapeInstance, child);
MatrixF *parentMat = getBoneTrans(shapeInstance, parent);
getBoneTrans(shapeInstance, child)->mul(newTrans);
updateChildren(shapeInstance, child);
}
}
void Skeleton::solveJiggleBone(TSShapeInstance* shapeInstance, JiggleBone* jB, mass* m) //solve() method: the method where forces can be applied
{
Point3F rootPos = getBoneTrans(shapeInstance, findBone(jB->parent))->getPosition();
Point3F endPos = getBoneTrans(shapeInstance,jB)->getPosition();
VectorF springVector = rootPos - endPos; //vector between the two masses
VectorF force = VectorF(0,0,0); //force initially has a zero value
JiggleBone* jBParent = NULL;
//solve the 'spring' between the two bones
F32 vecLen = springVector.len(); //distance between the two masses
if (vecLen != 0){ //to avoid a division by zero check if r is zero
VectorF a = (springVector / vecLen) * (vecLen - jB->length) * (-jB->stiffness);
force += a;//(springVector / vecLen) * (vecLen - jB->length) * (-jB->stiffness); //the spring force is added to the force
}
//solve the rest of the forces
//repurpose for collision code
/*if (masses[a]->pos.y < groundHeight) //Forces from the ground are applied if a mass collides with the ground
{
Vector3D v; //A temporary Vector3D
v = masses[a]->vel; //get the velocity
v.y = 0; //omit the velocity component in y direction
//The velocity in y direction is omited because we will apply a friction force to create
//a sliding effect. Sliding is parallel to the ground. Velocity in y direction will be used
//in the absorption effect.
masses[a]->applyForce(-v * groundFrictionConstant); //ground friction force is applied
v = masses[a]->vel; //get the velocity
v.x = 0; //omit the x and z components of the velocity
v.z = 0; //we will use v in the absorption effect
//above, we obtained a velocity which is vertical to the ground and it will be used in
//the absorption force
if (v.y < 0) //let's absorb energy only when a mass collides towards the ground
masses[a]->applyForce(-v * groundAbsorptionConstant); //the absorption force is applied
//The ground shall repel a mass like a spring.
//By "Vector3D(0, groundRepulsionConstant, 0)" we create a vector in the plane normal direction
//with a magnitude of groundRepulsionConstant.
//By (groundHeight - masses[a]->pos.y) we repel a mass as much as it crashes into the ground.
Vector3D force = Vector3D(0, groundRepulsionConstant, 0) *
(groundHeight - masses[a]->pos.y);
jB->applyForce(force); //The ground repulsion force is applied
}*/
VectorF vel = jB->velocity * jB->moveFriction; //The air friction
if(isJiggleBone(jB->parent, jBParent))
force += -(vel - jBParent->velocity) * jB->moveFriction.z; //the friction force is added to the force
else
force += -(vel) * jB->moveFriction.z; //with this addition we obtain the net force of the spring
VectorF b = (VectorF(0,0,-9.81f) * jB->specificGravity) * jB->mass;
force += b;//(VectorF(0,0,-9.81f) * jB->specificGravity) * jB->mass; //The gravitational force
m->force += force; //net forces
}
//simulate the bone here
void Skeleton::updateJiggleBone(TSShapeInstance* shapeInstance, JiggleBone* jB, mass* m, F32 dt) //solve() method: the method where forces can be applied
{
m->velocity += (m->force / jB->mass) * dt; // Change in velocity is added to the velocity.
// The change is proportinal with the acceleration (force / m) and change in time
MatrixF finMat, mat = *getBoneTrans(shapeInstance, jB);
Point3F pos = mat.getPosition();
pos += m->velocity * dt; // Change in position is added to the position.
// Change in position is velocity times the change in time
TSTransform::setMatrix(mat, pos, &finMat);
//mat.setPosition(pos);
MatrixF newBoneTrans = CheckDofsRestrictions(jB, finMat);
storeBoneTrans(shapeInstance, jB->boneNode, newBoneTrans);
}
void Skeleton::accumulateVelocity(TSShapeInstance* shapeInstance)
{
//what we do here is compare our last know bone and object positions, and then see the difference,
//accumulating a velocity out of the distance/rotation that we use to solve the jigglebone animation
MatrixF nodeTransform, difference;
//MatrixF globalDifference = shapeInstance->getTransform() - objectTransform;
for(U32 i=0; i < oldNodeTransforms.size(); i++)
{
//obviously we have some transforms
//so get our current transforms for any jigglebone's parents
for(U32 x=0; x < jBoneList.size(); x++)
{
//looks like we have a hit
if(oldNodeTransforms[i].bone == jBoneList[x]->parent)
{
nodeTransform = shapeInstance->mNodeTransforms[jBoneList[x]->parent];
//difference = nodeTransform - oldNodeTransforms[i];
//blahblahmathblah
}
}
}
//now flush our current transforms and store them for the next update
}
void Skeleton::addBone(Bone* bone)
{
bool nameMatch = false, boneMatch = false;
//check our bone name list
for(U32 x=0; x < boneNames.size(); x++)
{
//we have a hit!
if(!dStrcmp(boneNames[x].c_str(), bone->boneName))
nameMatch = true;
}
//no name here
if(!nameMatch)
boneNames.push_front(bone->getName());
//check our physical bones list
for(U32 i=0; i< boneList.size(); i++)
{
//yup, it's here
if(boneList[i]->boneNode == bone->boneNode)
boneMatch = true;
}
//otherwise, we can add it to our list no problem.
if(!boneMatch)
boneList.push_front(bone);
}
void Skeleton::addJiggleBone(JiggleBone* jBone)
{
//jigglebones are distinct from regular bones, so we can have a jigglebone with the same
//bonenode as a regular bone, but cannot have 2 jigglebones on the same node.
for(U32 i=0; i< jBoneList.size(); i++)
{
//if it already exists, abort adding it.
if(jBoneList[i]->boneNode == jBone->boneNode)
return;
}
//otherwise, we can add it to our list no problem.
jBoneNames.push_front(jBone->getName());
jBoneList.push_front(jBone);
}
void Skeleton::addIKChain(IKChain* ikchain)
{
bool nameMatch = false, chainMatch = false;
//first, double check we're not trying to re-add this thing
for(U32 x=0; x<ikChainNames.size(); x++)
if(!dStrcmp(ikChainNames[x].c_str(), ikchain->getName()))
nameMatch = true;
for(U32 q=0; q<ikChains.size(); q++)
if(!dStrcmp(ikChains[q]->rootBoneName, ikchain->rootBoneName) &&
!dStrcmp(ikChains[q]->endBoneName, ikchain->endBoneName))
chainMatch = true;
if(!chainMatch)
{
S32 rootIdx = /*bodyData->shape->*/meshShape.findNode(ikchain->rootBoneName);
S32 endIdx = /*bodyData->shape->*/meshShape.findNode(ikchain->endBoneName);
S32 currIdx = endIdx;
S32 priorIdx = 0;
VectorF boneVector;
S32 test = -1;
Bone *bone = new Bone();
if(rootIdx == -1 || endIdx == -1){
Con::errorf("IKChain::addIKChain - unable to find either the root or end nodes!");
return;
}
do
{
if(currIdx == -1)
break;
//first, check that we don't already have a bone at each step. If we do, just tweak the bone
//data to hook in. otherwise, create a new bone and string it together
bool aBone = isBone(currIdx, bone);
if(!aBone)
{
bone->boneNode = currIdx;
if(currIdx != endIdx){
bone->length = getBoneLength(currIdx, priorIdx);
bone->children.push_back(priorIdx);
}
else
bone->length = getBoneLength(currIdx, meshShape.nodes[currIdx].firstChild);
getBoneDefaultTrans(bone)->getColumn(1, &boneVector);
bone->boneVec = boneVector;
bone->parent = meshShape.nodes[currIdx].parentIndex;
String parentName = meshShape.getName(meshShape.nodes[currIdx].parentIndex);
bone->boneName = meshShape.getName(meshShape.nodes[currIdx].nameIndex);
bone->bounds = Box3F();
bone->mTarget = ikchain->mTarget; //the object we're trying to set a Bone to
//as we've created an entirely new bone, we need to add it to our skeleton's global bone list
String temp = bone->getName();
if(temp.isEmpty())
bone->assignName(bone->boneName);
//register this hooooo
bone->registerObject();
addBone(bone);
}
else
bone = findBone(currIdx);
priorIdx = currIdx;
currIdx = bone->parent;
//if we can't find the node, bail this one, otherwise, add it in
if(bone->boneNode != -1)
{
//add to the chain
ikchain->addBone(bone);
}
//clear it out for the next iteration
bone = new Bone();
U32 rootNodeParentVal = meshShape.nodes[rootIdx].parentIndex;
U32 currentNodeVal = meshShape.nodes[currIdx].parentIndex;
U32 savethescope = -1;
}while(currIdx != meshShape.nodes[rootIdx].parentIndex); //if we've reached the parent of our chain, or somehow hit the end of the skeleton, end.
//now add our completed chain to the skeleton
ikChains.push_back(ikchain);
}
if(!nameMatch)
ikChainNames.push_back(ikchain->getName());
}
void Skeleton::addIKRule(IKRule* ikrule)
{
bool nameMatch = false, ruleMatch = false;
//check our bone name list
for(U32 x=0; x < ikRuleNames.size(); x++)
{
//we have a hit!
if(!dStrcmp(ikRuleNames[x].c_str(), ikrule->getName()))
nameMatch = true;
}
//no name here
if(!nameMatch)
ikRuleNames.push_front(ikrule->getName());
//check our physical bones list
for(U32 i=0; i< ikRules.size(); i++)
{
//yup, it's here
if(ikRules[i] == ikrule)
ruleMatch = true;
}
//otherwise, we can add it to our list no problem.
if(!ruleMatch)
ikRules.push_back(ikrule);
}
void Skeleton::addJiggleChain(JiggleChain* jchain)
{
S32 rootIdx = /*bodyData->shape->*/meshShape.findNode(jchain->rootBoneName);
S32 endIdx = /*bodyData->shape->*/meshShape.findNode(jchain->endBoneName);
S32 currIdx = endIdx;
S32 priorIdx = 0;
VectorF boneVector;
do
{
//first, add our end node, then step backwards up the heirarchy and add those
JiggleBone *newBone = new JiggleBone();
newBone->boneNode = currIdx;
getBoneDefaultTrans(newBone)->getColumn(1, &boneVector); //get the forward vector of the bone instead
newBone->boneVec = boneVector;
newBone->parent = /*bodyData->shape->*/meshShape.nodes[currIdx].parentIndex;
newBone->length = boneVector.len();
newBone->boneName = /*bodyData->shape->*/meshShape.getName(/*bodyData->shape->*/meshShape.nodes[currIdx].nameIndex);
newBone->bounds = Box3F();