0

I'm trying to implement a third person camera in OpenGL. I'm able to get it to work without any problem using euler angles and basic trigonometry:

float cameraDistance = 10.0f;
float cameraPositionX = sin(targetRotation.y) * cameraDistance;
float cameraPositionY = cos(targetRotation.y) * cameraDistance;

ViewMatrix = glm::lookAt(
    glm::vec3(cameraPositionX, cameraPositionY, 30.0f) + targetLocation,
    targetLocation,
    glm::vec3(0,1,0)
);

The problem is when I start to use quaternion to store the target rotations. Converting quaternion to euler angles gives ambiguous results and the above code doesn't work well anymore. For example after crossing 90 degrees, y rotation starts from 0 again but x and z are flipped, so I guess I would need to include x and z in the trigonometry equations as well, but my brain is to tiny figure them out. Is there any way to translate the above code to work with quaternions properly ?

genpfault
  • 51,148
  • 11
  • 85
  • 139

0 Answers0