simplified global - local transformation methods

fixed position bug when posing bones
initial IK method
This commit is contained in:
devZoGok
2021-10-19 19:06:41 +03:00
parent 7ed0eed7e2
commit 3ee5555652
9 changed files with 204 additions and 78 deletions
+47 -4
View File
@@ -12,13 +12,56 @@ namespace vb01{
}
void Skeleton::update(){
/*
for(Bone *b : bones)
b->update();
*/
for(Bone *b : bones){
//b->update();
if(b->getIkTarget())
solveIk(b);
}
controller->update();
}
void Skeleton::solveIk(Bone *ikBone){
const int chainLength = ikBone->getIkChainLength();
Bone *ikTarget = ikBone->getIkTarget();
Bone *boneChain[chainLength];
Bone *ikBoneAncestor = ikBone;
Vector3 boneIkPos[chainLength], targetPos = ikTarget->getModelSpacePos();
float sumLengths = 0;
for(int i = 0; i < chainLength; i++){
sumLengths += ikBoneAncestor->getLength();
boneChain[i] = ikBoneAncestor;
ikBoneAncestor = (Bone*)ikBoneAncestor->getParent();
boneIkPos[i] = boneChain[i]->getModelSpacePos();
}
Vector3 startPos = boneChain[chainLength - 1]->getModelSpacePos();
if(startPos.getDistanceFrom(targetPos) < sumLengths){
int numIterations = 500;
for(int i = 0 ; i < numIterations; i++){
bool backward = i % 2;
for(int j = 0; j < chainLength; j++){
int boneId = (backward ? j : chainLength - 1 - j);
Vector3 ikPos = (j == 0 ? (backward ? targetPos : startPos) : boneIkPos[boneId]);
Vector3 fromBoneToIkPos = ikPos - boneChain[boneId]->getModelSpacePos();
boneIkPos[boneId] = ikPos - fromBoneToIkPos * boneChain[boneId]->getLength();
}
}
}
/*
else{
Vector3 fromStartToTarget = targetPos - startPos;
for(int i = 0; i < chainLength; i++){
}
}
*/
Bone *rootBone = getRootBone();
for(int i = 0; i < chainLength; i++)
(boneChain[i])->lookAt(boneIkPos[i], rootBone->getParent());
}
void Skeleton::addBone(Bone *bone, Bone *parent){
if(parent)
parent->attachChild(bone);