From 75af2c15c7adda08fbe49f930cd15b7ecdf26453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Mon, 20 Nov 2017 11:26:53 +0100 Subject: [PATCH] Overrides - Fixed NaN in camera handling --- camera.cc | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/camera.cc b/camera.cc index 5e0f21a..940ff75 100644 --- a/camera.cc +++ b/camera.cc @@ -48,14 +48,19 @@ void T_Camera::cvtAnglesToVectors( ) noexcept void T_Camera::cvtVectorsToAngles( ) noexcept { dir_ = lookAt_ - pos_; - distance_ = glm::length( dir_ ); - if ( distance_ == 0 ) { - angles_ = glm::vec3( 0 ); + const float distance{ length( dir_ ) }; + if ( distance == 0 || length( up_ ) == 0 ) { + return; + } + distance_ = distance; + + const glm::vec3 nDir{ - dir_ / distance_ }; + const glm::vec3 nUp{ normalize( up_ ) }; + if ( abs( dot( nUp , nDir ) ) == 1 ) { return; } - const glm::vec3 nDir{ - dir_ / distance_ }; - const glm::vec3 side{ normalize( cross( normalize( up_ ) , nDir ) ) }; + const glm::vec3 side{ normalize( cross( nUp , nDir ) ) }; const glm::vec3 up{ normalize( cross( nDir , side ) ) }; column( rotMat_ , 0 ) = side; column( rotMat_ , 1 ) = up;