How to understand ofNode::setOrientation(const glm::vec3& eulerAngles) of OF0.12.0?

Hi,

I’m using OF0.12.0, Linux version.
I found a problem that I can’t understand:

glm::vec3 o(-13.2398, 90, -47.4342)
ofNode::setOrientation(o);
cout << getOrientationEulerDeg << endl;

It outputs:
34.1943, 89.9802, 180

The rendering effect is also wrong.

When I changes the input 90 to 89, it is right.

So is there anything I don’t understand yet?
Thanks!

Hi,
ofNode internally uses quaternions rather than euler angles to represent rotations. So, when you pass in the vec3 containing euler angles in degrees it will get converted to a quaternion. Then when you want to retrieve euler angles from the quaternion you might not get the same numerical values as you had input because there are several different combinations of euler angles that end up in the same orientation. Thus, dont rely on the numerical values of the euler angles. If you want to check id recommend you to use something as the ofNode::getLookAtDir() direction, as in that case it will give you the same numerical values regardless of the “choice of euler angles”

good tips from @roymacdonald

@zhang_411 - yeah the getOrientationEulerDeg() is tricky because orientation is hard to determine especially when objects got close to being orientated towards an axis. See: Euler Angles and Gimbal Lock: Things You Need to Know | Tech Art Learning

A better approach is to use quaternions to set the orientation of an object.

One way to do that is to create a rotation that rotates an object relative to a starting vector.

So say you have a object lying on the x axis and you want it rotated 90 degrees to be upright, you would do this:

glm::quat myQuat = glm::rotation(glm::vec3(1, 0, 0), glm::vec3(0, -1, 0)); //rotate from lying down to upright. In this case -1 is up in Y.
myNode.setOrientation(myQuat); 

or if you want to add a rotation to a current orientation:

	auto quat = myNode.getOrientationQuat();
	quat = glm::rotate(quat, ofDegToRad(45.0), glm::vec3(0, 0, 1)); //add a rotation of 45 degrees to the current orientation around the z axis
	myNode.setOrientation(quat);

It takes a bit of getting used to but quaternions make this stuff a lot more precise and avoid the gimble lock issues.

1 Like

@roymacdonald , @theo ,
Thank you very much!
I’ll try the methods you mentioned.