From 00715f1906d95d4a56450bb12b3c20c7c6316522 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 27 Jan 2016 10:14:37 -0800 Subject: [PATCH] apply "hips offset" to root of state graph (Somemtimes the hips are not the root. I'm looking at YOU blender!) --- libraries/animation/src/AnimInverseKinematics.cpp | 13 +++++++++++-- libraries/animation/src/AnimInverseKinematics.h | 9 +++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/libraries/animation/src/AnimInverseKinematics.cpp b/libraries/animation/src/AnimInverseKinematics.cpp index e47a12960e..d7c604fff1 100644 --- a/libraries/animation/src/AnimInverseKinematics.cpp +++ b/libraries/animation/src/AnimInverseKinematics.cpp @@ -376,13 +376,13 @@ const AnimPoseVec& AnimInverseKinematics::overlay(const AnimVariantMap& animVars ++constraintItr; } } else { - // shift the hips according to the offset from the previous frame + // shift the everything according to the _hipsOffset from the previous frame float offsetLength = glm::length(_hipsOffset); const float MIN_HIPS_OFFSET_LENGTH = 0.03f; if (offsetLength > MIN_HIPS_OFFSET_LENGTH) { // but only if offset is long enough float scaleFactor = ((offsetLength - MIN_HIPS_OFFSET_LENGTH) / offsetLength); - _relativePoses[_hipsIndex].trans = underPoses[_hipsIndex].trans + scaleFactor * _hipsOffset; + _relativePoses[_rootIndex].trans = underPoses[_rootIndex].trans + scaleFactor * _hipsOffset; } solveWithCyclicCoordinateDescent(targets); @@ -775,9 +775,18 @@ void AnimInverseKinematics::setSkeletonInternal(AnimSkeleton::ConstPointer skele initConstraints(); _headIndex = _skeleton->nameToJointIndex("Head"); _hipsIndex = _skeleton->nameToJointIndex("Hips"); + + // walk up the hierarchy until we find the root and cache its index + _rootIndex = _hipsIndex; + int parentIndex = _skeleton->getParentIndex(_hipsIndex); + while (parentIndex != -1) { + _rootIndex = parentIndex; + parentIndex = _skeleton->getParentIndex(_rootIndex); + } } else { clearConstraints(); _headIndex = -1; _hipsIndex = -1; + _rootIndex = -1; } } diff --git a/libraries/animation/src/AnimInverseKinematics.h b/libraries/animation/src/AnimInverseKinematics.h index 971d2d5ff1..52dd4b5863 100644 --- a/libraries/animation/src/AnimInverseKinematics.h +++ b/libraries/animation/src/AnimInverseKinematics.h @@ -79,13 +79,14 @@ protected: AnimPoseVec _relativePoses; // current relative poses // experimental data for moving hips during IK - int _headIndex = -1; - int _hipsIndex = -1; - glm::vec3 _hipsOffset = Vectors::ZERO; + glm::vec3 _hipsOffset { Vectors::ZERO }; + int _headIndex { -1 }; + int _hipsIndex { -1 }; + int _rootIndex { -1 }; // _maxTargetIndex is tracked to help optimize the recalculation of absolute poses // during the the cyclic coordinate descent algorithm - int _maxTargetIndex = 0; + int _maxTargetIndex { 0 }; }; #endif // hifi_AnimInverseKinematics_h