# Sphere orientation

Hi, i’m trying to understand how the orientation of a sphere works.

I’ve declared an ofImage and an ofSpherePrimitve in my ofApp.h, then called the following code in the setup:

``````ofEnableDepthTest();

ofEnableNormalizedTexCoords();

sphere.set(300, 50);

sphere.setPosition(ofGetWidth()/2, ofGetHeight()/2, 0);

sphere.setOrientation(ofQuaternion(0, 1, 0, 0));

sphere.mapTexCoords(0,0,1,1);
``````

when I draw this scene I get what I want: the sphere drawn and the center of the texture is aligned with the camera.
However, I don’t understand why. I understand how quaternions work to change an orientation, but not how they define an absolute orientation. The values that I used were based on trial and error.

Can someone explain me how setOrientation works?

that way of creating a quaternion is probably not very useful unless you are getting the values from some calculation. the easiest is to use:

``````ofQuaternion(angle, axis);
``````

where axis is an ofVec3f to aplly the angle to. for example to rotate the object 90 degrees in z:

``````spehere.setOrientation(ofQuaternion(90,ofVec3f(0,0,1));
``````

wait, it’s (angle,axis)? I got confused and thought it was (axis,angle).
When you use .setOrientation which is the reference orientation?
One last question,
ofQuaternion(90,0,1,0);
is not equivalent to
ofQuaternion(90, ofVec3f(0,1,0)); ?

Hey there, no it is of course not! A quat is essentially a 4D vector, normalized to 1 (!), which encodes a rotation in 3D space. You can convert from an axis-and-angle or rotation matrix representation to a quaternion and vice versa. Without having looked at the actual API, I pretty much suppose the former form initialises a quat with those 4 values, whereas the latter does a conversion from AAA to quat representation. The values you supply in the first form don’t even make for a well-defined quat/rotation since it is not normalized! I hope that makes things clear, otherwise ask, I’ve dealt with those 3D transform math quite a lot in the past.

To follow up on this, the exact meaning of the 4 vector entries is as follows:

[w, x, y, z] = [cos(a/2), sin(a/2) * nx, sin(a/2)* ny, sin(a/2) * nz]

Where a is the angle of rotation, and [nx, ny, nz] a normaized axis.

The most advantageous properties of quats are

1. They avoid the “gimbal lock” problem of Euler angles which is essentially a singularity in the transform math, creates numerical instabilities and needs to be accounted for in an implementation.
2. They can be interpolated nicely using the “slerp” spherical linear interpolation, which kinda interpolates between the two quats on a 4D unit sphere, making things like keypoint based animation rather straightforward.

[rant edited away, it has been a long day…]

1 Like

Don’t worry about that, you’ll lose the new member status soon enough, that’s just to avoid spam.

Yeah, I found that out later researching; I had just assumed the quaternion behaved as a 3dvec + an angle. I was confused because the ofQuaternion class in oF can be created in such a way, but requires you to explicitly pass the ofVec3f as a second argument.