From 65a49aca9257398a701f8067624f08ec6f3cb867 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Mon, 4 Apr 2016 15:50:27 -1000 Subject: [PATCH] Non-inverted lookAt implementation --- src/Math.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Math.cpp b/src/Math.cpp index 2435a5e..ad1bde8 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -73,16 +73,18 @@ CTransform lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3 { CVector3f vLook,vRight,vUp; - vLook = pos - lookPos; + vLook = lookPos - pos; + if (vLook.magnitude() < FLT_EPSILON) + vLook = {0.f, 1.f, 0.f}; vLook.normalize(); - vRight = up.cross(vLook); + vRight = vLook.cross(up); vRight.normalize(); - vUp = vLook.cross(vRight); - - CMatrix3f rmBasis(vRight, vUp, vLook); - return CTransform(rmBasis.transposed(), CVector3f(-pos.dot(vRight), -pos.dot(vUp), -pos.dot(vLook))); + vUp = vRight.cross(vLook); + + CMatrix3f rmBasis(vRight, vLook, vUp); + return CTransform(rmBasis, pos); } CVector3f getBezierPoint(const CVector3f& a, const CVector3f& b, const CVector3f& c, const CVector3f& d, float t)