How to convert glm::quat data to glm::vec3 angles?...
Created on: February 25, 2025
Answered using O3 Mini High by Chat01
Created on: February 25, 2025
Answered using O3 Mini High by Chat01
How to convert glm::quat data to glm::vec3 angles?
Example: I've start data:
glm::vec3 start = glm::vec3(90, 30, 45);
and after convert to quat
(0,730946)(0,016590)(0,677650)
.... How to convert quat to glm::vec3(90, 30, 45) ??? And get initial data (90, 30, 45)
When working with GLM quaternions and Euler angles, it’s important to remember that GLM’s functions expect angles in radians. If you start with Euler angles in degrees, you must convert them to radians when constructing the quaternion, and then convert back to degrees when extracting Euler angles. For example:
cpp#include <glm/glm.hpp> #include <glm/gtc/quaternion.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtx/quaternion.hpp> #include <glm/gtc/constants.hpp> // Original Euler angles in degrees glm::vec3 startEulerDegrees(90.0f, 30.0f, 45.0f); // Convert to radians glm::vec3 startEulerRadians = glm::radians(startEulerDegrees); // Create a quaternion from Euler angles (assumes a particular rotation order) glm::quat q = glm::quat(startEulerRadians); // Convert back from quaternion to Euler angles (in radians) glm::vec3 eulerRadians = glm::eulerAngles(q); // Convert radians to degrees glm::vec3 eulerDegrees = glm::degrees(eulerRadians);
After this, eulerDegrees
should be approximately (90, 30, 45)
.
Key Points:
Radians vs. Degrees:
GLM’s quaternion constructor (and its conversion functions) expect angles in radians. Always convert degrees to radians using glm::radians()
when creating the quaternion and use glm::degrees()
when converting back.
Rotation Order:
The conversion functions assume a particular order of rotations. If the order used when creating the quaternion is different from what you expect when extracting Euler angles, the result may not match your original values exactly. GLM typically uses Tait–Bryan angles (often equivalent to applying rotations in Z, Y, X order), so ensure that both conversion steps use the same convention.
Numerical Precision and Ambiguity:
Due to the nature of Euler angles (where multiple sets of angles can represent the same rotation) and floating-point precision, the recovered angles might differ slightly from the original ones even if everything is set up correctly.
Using this approach should help you convert your quaternion back to the original Euler angles.