Simple question (I hope) about Quaternions

Hello there,

If I have rotated an object, and I later want to work out the rotations, how do I get back the andles (in degrees) for the x, y and z?

Example: So I've done this first:

Quaternion rotation = new Quaternion();
rotation.fromAngles(FastMath.PI * (xAngle/180f), FastMath.PI * (yAngle/180f), FastMath.PI * (zAngle/180f));

Great, now if I do this:

Quaternion rotation = spatial.getLocalRotation();

How do I get my xAngle, yAngle and zAngle again? I have tried all sorts. I thought something like:

float[] radians = rotation.toAngles(null);
float xAngle = SceneController.radiansToDegrees(radians[0]);
float yAngle = SceneController.radiansToDegrees(radians[1]);
float zAngle = SceneController.radiansToDegrees(radians[2]);

But that ain't right (unless I'm missing something!).

Please help. I'm pulling what little hair I have left out of my head !!


float xAngle = FastMath.RAD_TO_DEG * radians[0];

should work

Thanks dhdd, but that isn't working either.

I think I'm homing in on the problem, but my mathematical skills just do not have the capacity to understand how to solve the problem. The long and short of it is that "Quaternion.toAngles()" will not always (in fact, hardly ever) directly translate the rotation back from the rotation created in "Quaternion.fromAngles()".

    Before (degrees): xDeg=15 yDeg=160 zDeg=170
    Before (radians): xRad=0.2617994 yRad=2.792527 zRad=2.9670596

    Quaternion rotation = new Quaternion();
    rotation.fromAngles(xRad, yRad, zRad);
    float[] newRads = rotation.toAngles(null);

    After (radians): xRad=-2.8797932 yRad=-0.34906578 zRad=0.17453338
    After (degrees): xDeg=-165 yDeg=-20 zDeg=10

You'll notice (maybe) that by adding or subtracting 180 from the "After (degrees)" values, you can get back to the original value. But there doesn't seem to be any consistency on whether it needs subtracting or not. At least, I haven't spotted it yet!

I've created a test class that illustrates the point.

import com.jme.math.FastMath;
import com.jme.math.Quaternion;

public class QuaternionTest {
    public static void main(String[] args) {
        Quaternion rotation = new Quaternion();
        float xRad = FastMath.DEG_TO_RAD * 15.0f;
        float yRad = FastMath.DEG_TO_RAD * 160.0f;
        float zRad = FastMath.DEG_TO_RAD * 170.0f;
        System.out.println("xrad=" + xRad + " yrad=" + yRad + " z=" + zRad);

        rotation.fromAngles(xRad, yRad, zRad);
        System.out.println("rot=" + rotation);
        float[] toRads = rotation.toAngles(null);
        System.out.println("x=" + toRads[0] + " y=" + toRads[1] + " z=" + toRads[2]);

        System.out.println("xdeg=" + (toRads[0] * FastMath.RAD_TO_DEG));
        System.out.println("ydeg=" + (toRads[1] * FastMath.RAD_TO_DEG));
        System.out.println("zdeg=" + (toRads[2] * FastMath.RAD_TO_DEG));

So come on you clever folk... What am I missing!?