mirror of
https://github.com/devZoGok/vb01.git
synced 2026-08-26 19:43:30 +00:00
simplified global - local transformation methods
fixed position bug when posing bones initial IK method
This commit is contained in:
+47
-4
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user