Advertisement
Guest User

Transform.cpp

a guest
Feb 4th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include "Transform.h"
  2.  
  3. static const float MaxVerticalAngle = 89.0f; //must be less than 90 to avoid gimbal lock
  4.  
  5. void Transform::NormalizeAngles()
  6. {
  7.     m_Rotation.y = fmodf(m_Rotation.y, 360.0f);
  8.     //fmodf can return negative values, but this will make them all positive
  9.     if(m_Rotation.y < 0.0f)
  10.         m_Rotation.y += 360.0f;
  11.  
  12.     if(m_Rotation.x > MaxVerticalAngle)
  13.         m_Rotation.x = MaxVerticalAngle;
  14.     else if(m_Rotation.x < -MaxVerticalAngle)
  15.         m_Rotation.x = -MaxVerticalAngle;
  16. }
  17.  
  18. void Transform::LookAt(glm::vec3 targetPosition)
  19. {
  20.     if((targetPosition - m_Position) == glm::vec3(0,0,0)) return;
  21.     glm::vec3 direction = glm::normalize(targetPosition - m_Position);
  22.     m_Rotation.x = asinf(-direction.y)* RadToDeg;
  23.     m_Rotation.y = -atan2f(-direction.x, -direction.z)*RadToDeg;
  24.  
  25.     NormalizeAngles();
  26. }
  27.  
  28. glm::mat4 Transform::GetOrientation() const
  29. {
  30.     glm::mat4 orientation;
  31.     orientation = glm::rotate(orientation, m_Rotation.x, glm::vec3(1,0,0));
  32.     orientation = glm::rotate(orientation, m_Rotation.y, glm::vec3(0,1,0));
  33.     orientation = glm::rotate(orientation, m_Rotation.z, glm::vec3(0,0,1));
  34.     return orientation;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement