Tuesday, December 30, 2025
spot_img

GLM conversion from euler angles to quaternion and again doesn’t maintain


I’m attempting to transform the orientation of an OpenVR controller that I’ve saved as a glm::vec3 of Euler angles right into a glm::fquat and again, however I get wildly totally different outcomes and the in-game conduct is simply flawed (onerous to clarify, however the orientation of the article behaves usually for a small vary of angles, then flips in bizarre axes).

That is my conversion code:

// get `orientation` from OpenVR controller sensor information

const glm::vec3 eulerAnglesInDegrees{orientation[PITCH], orientation[YAW], orientation[ROLL]};
debugPrint(eulerAnglesInDegrees);

const glm::fquat quaternion{glm::radians(eulerAnglesInDegrees)};
const glm::vec3 end result{glm::levels(glm::eulerAngles(quaternion))};
debugPrint(end result);

// `end result` ought to symbolize the identical orientation as `eulerAnglesInDegrees`

I’d anticipate eulerAnglesInDegrees and end result to both be the identical or equal representations of the identical orientation, however that’s apparently not the case. These are some instance values I get printed out:

39.3851 5.17816 3.29104 
39.3851 5.17816 3.29104 

32.7636 144.849 44.3845 
-147.236 35.1512 -135.616 

39.3851 5.17816 3.29104 
39.3851 5.17816 3.29104 

32.0103 137.415 45.1592 
-147.99 42.5846 -134.841 

As you may see above, for some orientation ranges the conversion is right, however for others it’s fully totally different.

What am I doing flawed?

I’ve checked out current questions and tried a number of issues, together with attempting out each doable rotation order listed right here, conjugating the quaternion, and different random issues like flipping pitch/yaw/roll. Nothing gave me the anticipated end result.

How can I convert euler angles to quaternions and again, representing the unique orientation, utilizing glm?


Some extra examples of discrepancies:

authentic:      4; 175;   26; 
computed:   -175;   4; -153; 
distinction:  179; 171;  179; 

authentic:     -6; 173;   32; 
computed:    173;   6; -147; 
distinction: -179; 167;  179; 

authentic:      9; 268;  -46; 
computed:   -170; -88;  133; 
distinction:  179; 356; -179; 

authentic:    -27; -73;  266; 
computed:    -27; -73;  -93; 
distinction:    0;   0;  359; 

authentic:    -33; 111;  205; 
computed:    146;  68;   25; 
distinction: -179;  43;  180; 

I attempted to discover a sample to repair the ultimate computed outcomes, but it surely does not seem to be there’s one simple to establish.


GIF + video of the conduct:

GLM conversion from euler angles to quaternion and again doesn’t maintain


Visible illustration of my instinct/present understanding:

Visual diagram

  • The above image reveals a sphere, and I am within the heart. Once I purpose the gun in the direction of the inexperienced half of the sphere, the orientation is right. Once I purpose the gun in the direction of the purple half of the sphere, it’s incorrect – it looks like each axis is inverted, however I’m not 100% positive that’s the case.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles