How to convert glm::quat data to glm::vec3 angles?...

تم الإنشاء في: ٢٥ فبراير ٢٠٢٥

تمت الإجابة باستخدام O3 Mini High بواسطة 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)

الفكرة

Considering quaternion to Euler conversion

The user's asking about converting a quaternion to Euler angles, and I think they’re referencing GLM functions. In GLM, there are conversion functions, like glm::eulerAngles or glm::yaw, glm::pitch, and glm::roll. However, there's a catch: if the original Euler angles are in degrees, but GLM uses radians for conversion, a difference can arise. The user expects to get back (90, 30, 45), but since GLM defaults to radians, the conversion may not be direct without that adjustment.

Exploring quaternion to Euler conversion

To convert a quaternion back to Euler angles in GLM, I can use glm::eulerAngles(quat). This will give me radians, which I can convert to degrees using glm::degrees(angles). Now, the question asks specifically about converting back to the initial values (90, 30, 45). There might be a mismatch in how the quaternion was represented or formatted. The quaternion provided could be in shorthand or potentially missing the full four values, making this tricky. I need to account for rotation order too.

Discussing quaternion to Euler conversion details

I need to consider a few things when converting a quaternion to Euler angles in GLM. Using glm::eulerAngles(quat) gives a vec3 in radians, which I can convert to degrees with glm::degrees(). But head’s up—Euler angles may not always return exactly the same values because of floating-point precision or gimbal lock. GLM defaults to a "Z, Y, X" rotation order for Euler angles in its quaternion constructor. Plus, multiple representations mean conversions aren’t always reversible. There’s also glm::yaw, glm::pitch, and glm::roll functions to consider.

Reasoned for 19 seconds

الإجابة

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.

مشاركة هذا السؤال والجواب