Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Transform.h"
- static const float MaxVerticalAngle = 89.0f; //must be less than 90 to avoid gimbal lock
- void Transform::NormalizeAngles()
- {
- m_Rotation.y = fmodf(m_Rotation.y, 360.0f);
- //fmodf can return negative values, but this will make them all positive
- if(m_Rotation.y < 0.0f)
- m_Rotation.y += 360.0f;
- if(m_Rotation.x > MaxVerticalAngle)
- m_Rotation.x = MaxVerticalAngle;
- else if(m_Rotation.x < -MaxVerticalAngle)
- m_Rotation.x = -MaxVerticalAngle;
- }
- void Transform::LookAt(glm::vec3 targetPosition)
- {
- if((targetPosition - m_Position) == glm::vec3(0,0,0)) return;
- glm::vec3 direction = glm::normalize(targetPosition - m_Position);
- m_Rotation.x = asinf(-direction.y)* RadToDeg;
- m_Rotation.y = -atan2f(-direction.x, -direction.z)*RadToDeg;
- NormalizeAngles();
- }
- glm::mat4 Transform::GetOrientation() const
- {
- glm::mat4 orientation;
- orientation = glm::rotate(orientation, m_Rotation.x, glm::vec3(1,0,0));
- orientation = glm::rotate(orientation, m_Rotation.y, glm::vec3(0,1,0));
- orientation = glm::rotate(orientation, m_Rotation.z, glm::vec3(0,0,1));
- return orientation;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement