-
Notifications
You must be signed in to change notification settings - Fork 2
/
boneActor.cpp
385 lines (282 loc) · 10.6 KB
/
boneActor.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
#include "skeletalDrawing.h"
#include "boneActor.h"
#include "renderer.h"
#include "input.h"
#include "meshData.h"
BoneActor::BoneActor(){
name="Bone";
drawType=DRAW_CUBE;
scale=Vector3f(1, 1, 1);
mySystem=NULL;
influence=10;
bComputeLight=false;
bZTest=true;
bZWrite=true;
bPhysicsBone=false;
bJointedToBase=true;
bFixToWorld=false;
bDoIK=false;
sideRotation = 0.0;
f=0.0;
registerProperties();
}
BoneActor::~BoneActor(){}
void BoneActor::registerProperties(){
createMemberID("BDOIK",&bDoIK,this);
Actor::registerProperties();
}
void BoneActor::setup(){
Actor::setup();
}
void BoneActor::trigger(Actor * other){
}
void BoneActor::setLocation(Vector3f loc){
//has two parents, is vboMesh and skeletal
if (1==0)//bDoIK)
doIK(loc, false);
else{
Actor::setLocation(loc);
}
//make sure base's x-Axis always points to this bone!
}
void BoneActor::update(double deltaTime){
Actor::update(deltaTime);
/*
if (Control::bRunning)
drawType=DRAW_NULL;
else
drawType=DRAW_CUBE;
*/
}
void BoneActor::alignBaseRotation(){
//update base so it has its x-Axis always pointed towards us!
//and update our own rotation to compensate accordingly...
Vector3f nLoc=transformMatrix.getTranslation();
//Vector3f nLoc=baseMatrix.getTranslation() - base->baseMatrix.getTranslation();
float dist=nLoc.length();
nLoc.normalize();
//create base axis
Vector3f xA,yA,zA;
xA=nLoc;
//use zAxis or yAxis?
if (abs(xA.y) > 0.8){
zA=Vector3f(0,0,1);
yA=xA.crossProduct(zA);
zA=yA.crossProduct(xA);
}
else{
yA=Vector3f(0,1,0);
zA=xA.crossProduct(yA);
yA=zA.crossProduct(xA);
}
Vector3f oldBaseRotation=base->rotation;
base->setRotationFromAxis(xA,yA,zA);
Vector3f newLoc= Vector3f(1,0,0) * dist;
Actor::setLocation(newLoc);
Actor::setRotation(-oldBaseRotation);
}
void BoneActor::doIK(Vector3f loc, bool bAbsolute){
/*********************************************************************************
3 part process:
1. gather all necessary numbers
2. rotate the system into target plane (plane between target point and shoulder)
3. do 2D IK
*********************************************************************************/
//leave if we don't have two parents
if (!base || !base->base ){
cout << "no double-base!!! Cannot do IK" << endl;
Actor::setLocation(loc);
return;
}
//for debug stuff
f+=0.01;
///clear transform Matrices!
Matrix4d initialTransform;
initialTransform=base->base->transformMatrix;
base->base->transformMatrix.identity();
base->transformMatrix.identity();
transformMatrix.identity();
base->base->baseMatrix=calcMatrix(base->base);
base->baseMatrix=calcMatrix(base);
baseMatrix=calcMatrix(this);
///gathering numbers
Vector3f currentUpperVec, currentLowerVec, initialUpperVec, initialLowerVec;
MeshData* myMesh=renderer->vboList[parent->vboMeshID];
//find original angles for non-aligned bones - from mesh data!
for (int i=0;i<(int)myMesh->bones.size();i++){
if (myMesh->bones[i]->name==base->name)
initialUpperVec=myMesh->bones[i]->boneMatrix->getTranslation();
if (myMesh->bones[i]->name==name)
initialLowerVec=myMesh->bones[i]->boneMatrix->getTranslation();
}
float upperLength = initialUpperVec.length();
float lowerLength = initialLowerVec.length();
//initial Full - without any transformations
//this is for the relative positions of the bones to each other
Vector3f initialFull= initialUpperVec + initialLowerVec;
//project upper on full vector to get upper Angle
initialUpperVec.normalize();
initialLowerVec.normalize();
initialFull.normalize();
Vector3f currentFull=base->base->baseMatrix.getTranslation() - baseMatrix.getTranslation();
Vector3f targetFull=base->base->baseMatrix.getTranslation() - loc;
float targetFullLength = targetFull.length();
///out of bounds!!!
if (targetFullLength>=upperLength+lowerLength){
cout << "IK on " << name << " is out of bounds!" << endl;
cout << targetFullLength << endl;
return;
}
currentFull.normalize();
targetFull.normalize();
///rotating into IK plane
Matrix4f parentMatrix=parent->baseMatrix;
parentMatrix.setTranslation(Vector3f(0,0,0));
Vector3f rotAxis = currentFull.crossProduct(targetFull);
rotAxis=parentMatrix.inverse() * rotAxis;
rotAxis.normalize();
float rot= acos(currentFull.dotProduct(targetFull));
//check if they're on each other!
if (rot!=rot){
cout << "already in plane!" << endl;
rot=0.0; //no rotation in that case!
}
Vector3f xA,yA,zA;
yA=rotAxis;
xA=currentFull;
zA=yA.crossProduct(xA);
zA.normalize();
Matrix4f rotMatrix,angleMatrix;
rotMatrix[0]=xA.x;
rotMatrix[1]=xA.y;
rotMatrix[2]=xA.z;
rotMatrix[4]=yA.x;
rotMatrix[5]=yA.y;
rotMatrix[6]=yA.z;
rotMatrix[8]=zA.x;
rotMatrix[9]=zA.y;
rotMatrix[10]=zA.z;
angleMatrix=angleMatrix.createRotationAroundAxis(0,rot,0);
rotMatrix=rotMatrix * angleMatrix * rotMatrix.inverse();
//now rotate into IK plane
base->base->transformMatrix= base->base->transformMatrix * rotMatrix; //note: we can add the transformMatrix here, since rot should be 0 anyway after this!
base->base->baseMatrix=calcMatrix(base->base);
base->baseMatrix=calcMatrix(base);
baseMatrix=calcMatrix(this); //update all our matrices after transform!
///Math for 2D IK
float upperAngle=0.0, middleAngle=0.0, lowerAngle=0.0;
//using the following 2D stuff:
//sine rule:
// a/sinA = b/sinB = c/sinC
//cosine rule:
//c*c = b*b + a*a + 2ba*cosC
//find biggest angle first
if (upperLength>lowerLength){
if (upperLength>targetFullLength){
//upperLength = longest!
lowerAngle=acos( (lowerLength*lowerLength + targetFullLength*targetFullLength - upperLength*upperLength) / (2* lowerLength * targetFullLength) );
middleAngle=asin( targetFullLength * sin(lowerAngle) / upperLength);
upperAngle=asin( lowerLength * sin(lowerAngle)/ upperLength);
}else{
//fullLength = longest!
middleAngle=acos( (lowerLength*lowerLength + upperLength*upperLength - targetFullLength*targetFullLength) / (2* lowerLength * upperLength) );
lowerAngle=asin( upperLength * sin(middleAngle) / targetFullLength);
upperAngle=asin( lowerLength * sin(middleAngle) / targetFullLength);
}
}else if (lowerLength > targetFullLength){
//lowerLength = longest!
upperAngle=acos( ( targetFullLength*targetFullLength + upperLength*upperLength - lowerLength*lowerLength) / (2* upperLength * targetFullLength) );
middleAngle=asin( targetFullLength * sin(lowerAngle) / lowerLength);
lowerAngle=asin( upperLength * sin(lowerAngle) / lowerLength);
}
else{
//fullLength = longest!
middleAngle=acos( (lowerLength*lowerLength + upperLength*upperLength - targetFullLength*targetFullLength)/(2* lowerLength * upperLength) );
lowerAngle=asin( upperLength * sin(middleAngle) / targetFullLength);
upperAngle=asin( lowerLength * sin(middleAngle) / targetFullLength);
}
//phew!!!
//middle rotation
//NaN test!
if (middleAngle!=middleAngle)
middleAngle=0.0;
//test NaN
if (upperAngle!=upperAngle)
upperAngle=0.0;
///gather current Info
currentUpperVec=base->base->baseMatrix.getTranslation() - base->baseMatrix.getTranslation();
currentUpperVec.normalize();
currentLowerVec=base->baseMatrix.getTranslation() - baseMatrix.getTranslation();
currentLowerVec.normalize();
//origUpperAngle is the current 2D rotation!
float currentUpperAngle=acos(targetFull.dotProduct(currentUpperVec));
//check for NaN
if (currentUpperAngle!=currentUpperAngle)
currentUpperAngle=0.0;
//project upper on lower Vector to get middle Angle
float currentMiddleAngle=acos(currentLowerVec.dotProduct(currentUpperVec));
//check for NaN
if (currentMiddleAngle!=currentMiddleAngle)
currentMiddleAngle=0.0;
//add original values!
upperAngle= upperAngle - currentUpperAngle;
middleAngle= M_PI + middleAngle + currentMiddleAngle;// -;
///rotate 2D IK
Vector3f initialRotYAxis,initialRotZAxis, initialRotXAxis;
//now find our rotation Axis for 2D IK!
if (currentUpperAngle==0.0){
initialRotYAxis=Vector3f(0,1,0);
initialRotZAxis=Vector3f(0,0,1);
initialRotXAxis=Vector3f(1,0,0);
}else{
initialRotYAxis=initialFull.crossProduct(initialUpperVec);
initialRotYAxis=initialUpperVec.crossProduct(initialFull);
initialRotYAxis.normalize();
initialRotXAxis=initialUpperVec;
initialRotZAxis=initialRotXAxis.crossProduct(initialRotYAxis);
initialRotZAxis.normalize();
}
xA=initialRotXAxis;
yA=initialRotYAxis;
zA=initialRotZAxis;
Matrix4d initialRotMatrix;
initialRotMatrix[0]=xA.x;
initialRotMatrix[1]=xA.y;
initialRotMatrix[2]=xA.z;
initialRotMatrix[4]=yA.x;
initialRotMatrix[5]=yA.y;
initialRotMatrix[6]=yA.z;
initialRotMatrix[8]=zA.x;
initialRotMatrix[9]=zA.y;
initialRotMatrix[10]=zA.z;
//create a rotation matrix that applies the rotations to the x-Axis
Matrix4f midMatrix,upMatrix;
midMatrix=midMatrix.createRotationAroundAxis(0,middleAngle,0);
upMatrix=upMatrix.createRotationAroundAxis(0,upperAngle,0);
//rotate around arbitrary axis by rotating to the axis, then rotating your desired rotation, then rotating back
midMatrix= initialRotMatrix * midMatrix * initialRotMatrix.inverse();
upMatrix= initialRotMatrix * upMatrix * initialRotMatrix.inverse();
//rotate around first angle first
base->base->transformMatrix= base->base->transformMatrix * upMatrix;
base->transformMatrix= base->transformMatrix * midMatrix;
///update matrices
base->base->baseMatrix=calcMatrix(base->base);
base->baseMatrix=calcMatrix(base);
baseMatrix=calcMatrix(this);
}
void BoneActor::remove(){
//TODO: very Hacky!
//clear parent!
if (base){
SkeletalDrawing* myChar=dynamic_cast<SkeletalDrawing*>(base);
if (myChar){
for (int i=0;i<(int)myChar->bones.size();i++){
if (myChar->bones[i]==this){
myChar->bones[i]=NULL;
}
}
}
}
Actor::remove();
}
void BoneActor::create(){renderer->addActor(this);}