-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton.h
411 lines (312 loc) · 10.2 KB
/
skeleton.h
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
#ifndef _SKELETON_H_
#define _SKELETON_H_
#ifndef _SHAPEBASE_H_
#include "T3D/shapeBase.h"
#endif
#ifndef _GAMEBASE_H_
#include "T3D/gameBase/gameBase.h"
#endif
#ifndef _STRINGUNIT_H_
#include "core/strings/stringUnit.h"
#endif
class Skeleton;
class JiggleBone;
class tempJBone;
struct ShapeBaseData;
class TSShapeInstance;
//currently, Bones will only support a single child untill i can figure out a good way to have multiple-inheritant IK
class Bone : public SimObject
{
typedef SimObject Parent;
friend Skeleton;
friend JiggleBone; //<-stupid compiler of stupidness. >:(
friend tempJBone;
private:
S32 boneNode;
Vector<S32> 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)
S32 parent;
VectorF boneVec; //vectors to our child from this bone
F32 length; //length of our origin to our child
public:
StringTableEntry boneName;
Box3F bounds;
ShapeBaseData* mTarget; //the object we're trying to set a Bone to
EulerF mDof[1]; //a min/max for each axis
F32 mass;
bool onAdd();
Bone(){
length = 0.1f;
bounds = Box3F();
parent = -1;
boneNode = -1;
boneVec = VectorF(0,0,0);
boneName = "";
bounds = Box3F(0.f);
mDof[0] = EulerF(-120, -120, -120); //min
mDof[1] = EulerF(120, 120, 120); //max
}
~Bone(){}
static void initPersistFields();
U32 getBoneNode() { return boneNode; }
DECLARE_CONOBJECT(Bone);
};
class JiggleBone : public Bone
{
typedef Bone Parent;
public:
//below is used to generate the spring's data
//flexible
//the bone has full range of motion, forward/back, left/right and up/down.
bool isFlexible;
Point3F moveConstraint;
Point3F moveFriction;
//rigid
//the bone stays a set length away from the base, but moves around freely left/right and up/down
bool isRigid;
//universal properties
F32 tip_mass;
F32 specificGravity;
F32 stiffness;
Point3F dampening;
F32 mass;
bool active;
VectorF force; //force of the spring!
VectorF velocity;
void applyForce(VectorF f)
{
force += f; // The external force is added to the force of the mass
}
/*
void simulate(float dt) method calculates the new velocity and new position of
the mass according to change in time (dt). Here, a simulation method called
"The Euler Method" is used. The Euler Method is not always accurate, but it is
simple. It is suitable for most of physical simulations that we know in common
computer and video games.
*/
bool onAdd();
JiggleBone(){
bounds = Box3F();
parent = -1;
force = Point3F(0,0,0);
velocity = Point3F(0,0,0);
length = 1.f;
tip_mass = 1.f;
specificGravity = 1.f;
stiffness = 1.f;
dampening = Point3F(1,1,1);
mass = 1.f;
moveConstraint = Point3F(1,1,1);
moveFriction = Point3F(1,1,1);
}
~JiggleBone(){}
static void initPersistFields();
DECLARE_CONOBJECT(JiggleBone);
};
class tempJBone : public JiggleBone
{
typedef JiggleBone Parent;
public:
bool onAdd(){return true;}
tempJBone(){
bounds = Box3F();
parent = -1;
force = Point3F(0,0,0);
length = 1.f;
tip_mass = 1.f;
stiffness = 1.f;
dampening = Point3F(1,1,1);
}
~tempJBone(){}
void setFromBone(Bone *bone);
};
struct mass
{
S32 boneNum;
VectorF force;
VectorF velocity;
mass(){
boneNum = -1;
force = VectorF(0,0,0);
velocity = VectorF(0,0,0);
}
};
//===============================================================
//esentially a special container class for a list of bones
//the number is arbitrary, you define a start and end of the chain, but it has to be more than 1 bone.
class IKChain : public SimObject
{
typedef SimObject Parent;
friend Skeleton;
public:
StringTableEntry rootBoneName;
StringTableEntry endBoneName;
ShapeBaseData* mTarget;
S32 targetID;
F32 tolerance; //how tolerant are we of being away from our end goal
bool dontReach; //if the chain isn't long enough, should we at least reach for it anyways?
Vector<Bone*> chain;
bool onAdd();
IKChain()
{
mTarget = NULL;
targetID = 0;
tolerance = 0.1f;
dontReach = true;
rootBoneName = "";
endBoneName = "";
}
~IKChain(){}
static void initPersistFields();
void addBone(Bone *bone) {
U32 node = bone->getBoneNode();
for(U32 i=0; i<chain.size(); i++){
//if it already exists, abort adding it.
if(chain[i]->getBoneNode() == node)
return;
}
//otherwise, we can add it to our list no problem.
chain.push_front(bone);
}
S32 getBoneIndex(S32 boneNode)
{
for(U32 i=0; i<chain.size(); i++){
if(chain[i]->getBoneNode() == boneNode)
return i;
}
return -1;
}
DECLARE_CONOBJECT(IKChain);
};
class JiggleChain : public IKChain
{
typedef IKChain Parent;
friend Skeleton;
public:
//NOTE THESE SETTINGS APPLY AUTOMATICALLY TO ALL BONES CREATED BY THE CHAIN
//IF THE BONE ALREADY EXISTED, IT WILL USE THE SETTINGS IT HAS
//flexible
//the bone has full range of motion, forward/back, left/right and up/down.
bool isFlexible;
Point3F moveConstraint;
Point3F moveFriction;
Vector<JiggleBone*> chain;
//rigid
//the bone stays a set length away from the base, but moves around freely left/right and up/down
bool isRigid;
//universal properties
F32 tip_mass;
F32 specificGravity;
F32 stiffness;
Point3F dampening;
F32 mass;
bool active;
bool onAdd();
JiggleChain(){}
~JiggleChain(){}
static void initPersistFields();
DECLARE_CONOBJECT(JiggleChain);
};
class IKRule : public SimObject
{
typedef SimObject Parent;
friend Skeleton;
public:
IKChain* targetChain;
ShapeBaseData* mTarget;
StringTableEntry animationName;
StringTableEntry goalNodeName;
S32 triggerID;
F32 blendAmount;
Point3F offset;
bool matchOrientToGoal; //do we rotate our endbone to match the goal node's rotation? (useful for grabbing)
bool onAdd();
IKRule()
{
targetChain = NULL;
mTarget = NULL;
animationName = ""; //during what animation should we do the IK?
goalNodeName = "";
triggerID = -1; //what trigger number should start/stop the IK effect(useful for walking animations)
blendAmount = 0.f; //how much do we blend between the IK and the original animation
offset = Point3F(0,0,0);
}
~IKRule(){}
static void initPersistFields();
DECLARE_CONOBJECT(IKRule);
};
struct boneTransform
{
S32 bone;
MatrixF trans;
};
//===============================================================
class Skeleton {
friend class ShapeBase;
friend struct ShapeBaseData;
private:
TSShape meshShape; //the static model information
Vector<Bone*> boneList; //this is the entire list of bones for the skeleton, it's then furrther
//broken into IK chains if needed
Vector<String> boneNames;
Vector<String> jBoneNames;
Vector<String> ikChainNames;
Vector<String> jChainNames;
Vector<String> ikRuleNames;
public:
Vector<IKChain*> ikChains; //chains are separated much like the bones are. IK chains are for limbs,
Vector<JiggleChain*> jChains; //jigglechains are for clothing(capes, cloaks, etc) or rope/chains.
Vector<JiggleBone*> jBoneList; //jigglebones are handled separately from normal bones/IKChains.
//the benifit of this is you can define IK-capable bones and jigglebones
//on the same mesh bone
Vector<IKRule*> ikRules;
//Vector<boneTransform> newTransQueue; //our updated bone transform information.
//old transforms, used for jiggleBone accumulation
Vector<boneTransform> oldNodeTransforms;
MatrixF objectTransform;
Skeleton();
~Skeleton();
void CCDIK(TSShapeInstance* shapeInstance, IKChain *ikchain, MatrixF endTrans);
void physicalIK(TSShapeInstance* shapeInstance, IKChain *ikchain, MatrixF endTrans, F32 dt);
void analiticalIK(TSShapeInstance* shapeInstance, IKChain *ikchain, MatrixF endTrans);
MatrixF directionToMatrix(VectorF dir, VectorF up); //MATH!
MatrixF CheckDofsRestrictions(Bone *bone, MatrixF mat);
void updateChildren(TSShapeInstance* shapeInstance, Bone *parent);
void updateChildren(TSShapeInstance* shapeInstance, Bone *parent, MatrixF newTrans);
void updateChildren(TSShapeInstance* shapeInstance, S32 parent);
void updateChildren(TSShapeInstance* shapeInstance, Bone *current, IKChain *chain);
MatrixF setForwardVector(MatrixF *mat, VectorF axisY, VectorF up = VectorF(0,0,1));
void solveJiggleBone(TSShapeInstance* shapeInstance, JiggleBone* jB, mass *m);
void updateJiggleBone(TSShapeInstance* shapeInstance, JiggleBone* jB, mass *m, F32 dt);
void accumulateVelocity(TSShapeInstance* shapeInstance);
void addBone(Bone* bone);
void addJiggleBone(JiggleBone* jBone);
void addIKChain(IKChain* ikchain);
void addJiggleChain(JiggleChain* jchain);
void addIKRule(IKRule* ikrule);
bool isBone(S32 boneID, Bone *bone);
bool isJiggleBone(S32 boneID, Bone *bone);
//this searches the whole skeleton for the bone, this is a god bit slower, hence why we prefer to region-search
//if we don't find it here, bad bad juju.
Bone* findBone(S32 boneID);
F32 getBoneLength(S32 boneA, S32 boneB);
MatrixF* getBoneTrans(TSShapeInstance* shapeInstance, Bone *bone);
MatrixF getLocalBoneTrans(TSShapeInstance* shapeInstance, Bone *bone);
//void setBoneTrans(TSShapeInstance* shapeInstance, Bone *bone, MatrixF &mat);
void setBoneTrans(TSShapeInstance* shapeInstance, U32 boneNode, MatrixF &mat);
MatrixF* getBoneDefaultTrans(Bone *bone);
VectorF getBoneEndPoint(TSShapeInstance* shapeInstance, Bone *bone);
void storeBoneTrans(TSShapeInstance *shapeInstance, U32 boneNode, MatrixF &mat);
void storeBoneTrans(TSShapeInstance *shapeInstance, Bone *bone, MatrixF &mat); //we store our new transforms here, and then once we're done, the target will
//grab the updated transforms and apply them
void clearStoredTransforms(); //afterwards, we'll clear the stored transforms via this function
MatrixF getStoredBoneTrans(TSShapeInstance* shapeInstance, U32 boneNode);
bool isStoredBoneTrans(TSShapeInstance* shapeInstance, U32 boneNode);
//returns the vector the bones have using the default translation. good for refernce
VectorF getBoneHomeVector(Bone *boneA, Bone *boneB);
//returns our current forward vector
VectorF getBoneForwardVector(TSShapeInstance* shapeInstance, Bone *bone);
void clearSkeletalData();
void clearSkeletalNames();
};
#endif