From 94c39bc4af1ac912bba9c2ba6e0b46c5db2db56c Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 5 Jun 2018 14:43:33 -0700 Subject: [PATCH] Better fix for looking down at your own body. --- libraries/animation/src/AnimUtil.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/libraries/animation/src/AnimUtil.cpp b/libraries/animation/src/AnimUtil.cpp index ad11623a25..acb90126fc 100644 --- a/libraries/animation/src/AnimUtil.cpp +++ b/libraries/animation/src/AnimUtil.cpp @@ -120,22 +120,27 @@ glm::quat computeBodyFacingFromHead(const glm::quat& headRot, const glm::vec3& u // initially take the body facing from the head. glm::vec3 headUp = headRot * Vectors::UNIT_Y; glm::vec3 headForward = headRot * Vectors::UNIT_Z; - const float THRESHOLD = cosf(glm::radians(30.0f)); + glm::vec3 headLeft = headRot * Vectors::UNIT_X; + const float NOD_THRESHOLD = cosf(glm::radians(45.0f)); + const float TILT_THRESHOLD = cosf(glm::radians(30.0f)); glm::vec3 bodyForward = headForward; - float dot = glm::dot(headForward, bodyUp); + float nodDot = glm::dot(headForward, bodyUp); + float tiltDot = glm::dot(headLeft, bodyUp); - if (dot < -THRESHOLD) { // head is looking down - // the body should face in the same direction as the top the head. - bodyForward = headUp; - } else if (dot > THRESHOLD) { // head is looking upward - // the body should face away from the top of the head. - bodyForward = -headUp; + if (fabsf(tiltDot) < TILT_THRESHOLD) { // if we are not tilting too much + if (nodDot < -NOD_THRESHOLD) { // head is looking downward + // the body should face in the same direction as the top the head. + bodyForward = headUp; + } else if (nodDot > NOD_THRESHOLD) { // head is looking upward + // the body should face away from the top of the head. + bodyForward = -headUp; + } } // cancel out upward component - bodyForward = glm::normalize(bodyForward - dot * bodyUp); + bodyForward = glm::normalize(bodyForward - nodDot * bodyUp); glm::vec3 u, v, w; generateBasisVectors(bodyForward, bodyUp, u, v, w);