From a427e0a8a20375344b488e76f77706f5692ae3f4 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 17 Nov 2018 17:59:53 -1000 Subject: [PATCH] Fix lookAt --- src/Math.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Math.cpp b/src/Math.cpp index c4410d8..c68e2e7 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -167,14 +167,20 @@ CTransform lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3 CVector3f vLook, vRight, vUp; vLook = lookPos - pos; - if (vLook.magnitude() < FLT_EPSILON) + if (vLook.magnitude() <= FLT_EPSILON) vLook = {0.f, 1.f, 0.f}; - vLook.normalize(); + else + vLook.normalize(); - vRight = vLook.cross(up); - vRight.normalize(); - - vUp = vRight.cross(vLook); + vUp = up - vLook * clamp(-1.f, up.dot(vLook), 1.f); + if (vUp.magnitude() <= FLT_EPSILON) + { + vUp = CVector3f(0.f, 0.f, 1.f) - vLook * vLook.z; + if (vUp.magnitude() <= FLT_EPSILON) + vUp = CVector3f(0.f, 1.f, 0.f) - vLook * vLook.y; + } + vUp.normalize(); + vRight = vLook.cross(vUp); CMatrix3f rmBasis(vRight, vLook, vUp); return CTransform(rmBasis, pos);