Get angles of a node

Hello,

I’m trying to rotate a node and then read the angles of the rotated node.

When i do it like this it seems to work (at least for this angle):

Node nodeTest = new Node("Test");

// angles bevor rotating
System.out.println("getW()= " + nodeTest.getLocalRotation().getW());
System.out.println("getX()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getX());
System.out.println("getY()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getY());
System.out.println("getZ()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getZ());

// Rotating  90° around x-axis        
Quaternion quat = new Quaternion(FastMath.DEG_TO_RAD * 90, 0, 0, 0);
nodeTest.rotate(quat);

// angles after rotating
System.out.println("getW()= " + nodeTest.getLocalRotation().getW());
System.out.println("getX()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getX());
System.out.println("getY()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getY());
System.out.println("getZ()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getZ());        

But when i do the rotation with the following commands it is not working (different results).

nodeTest.rotate(FastMath.DEG_TO_RAD * 90, 0, 0);

Shouldn’t both ways produce the same results ?

Use Quaternion.toAngles(null) to get the angles, but no, there are many ways to store the same rotation. This starts with the fact that you can rotate 90° right or 270° left and when you combine rotations there are many ways to get there, so I think a Quaternion will always be the most compact way (smallest angles) to get there.

Apart from that: 1. Sample output would be cool, 2. What are you trying to do? There most likely is a better way for that.

I dont’ understand how to get the angles by Quaternion.toAngles(null). Could you explain this a bit more exact?.
I must admit that i don’t understand the values i get back when i rotate a node. Example:

Node nodeTest = new Node("Test");
System.out.println("getW()= " + nodeTest.getLocalRotation().getW());
System.out.println("getX()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getX());
System.out.println("getY()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getY());
System.out.println("getZ()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getZ());

results in

getW()= 1.0
getX()= 0.0
getY()= 0.0
getZ()= 0.0

this makes sense to me. When i rotate this node by 90° around the X-Axis by doing this

Quaternion quat = new Quaternion(FastMath.DEG_TO_RAD * 90, 0, 0, 0);
nodeTest.rotate(quat);
System.out.println("getW()= " + nodeTest.getLocalRotation().getW());
System.out.println("getX()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getX());
System.out.println("getY()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getY());
System.out.println("getZ()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getZ());

it results in

getW()= 0.0
getX()= 90.0
getY()= 0.0
getZ()= 0.0

This makes sense to me too (x-axis is rotated by 90°). But when I now rotate the node 90° round the y-axis i don’t understand the result anymore:

quat = new Quaternion(0, FastMath.DEG_TO_RAD * 90, 0, 0);
nodeTest.rotate(quat);
System.out.println("getW()= " + nodeTest.getLocalRotation().getW());
System.out.println("getX()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getX());
System.out.println("getY()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getY());
System.out.println("getZ()= " + FastMath.RAD_TO_DEG * nodeTest.getLocalRotation().getZ());

results in

getW()= 0.0
getX()= 0.0
getY()= 0.0
getZ()= 141.37167

i thought the result should be

getW()= 0.0
getX()= 90.0
getY()= 90.0
getZ()= 0.0

or something similar.
Seems that I have misunderstood something completely?!?!?
By the way. What do i need the “w”-component for when doing rotations???

Quaternions do not contain angles. They are magic 4 dimension numbers. Do not use them. Treat quaternion like a black box and leave it’s internal values alone.

99% of the time people think they need angles, they don’t. Why do you want the angles?

If you want the angles that the quaternion represents then use toAngles(). Like:
float[] angles = myQuat.toAngles(null);

…but this is likely to be unsatisfying because quaternions are a unique and compact form of a rotation and the angles might not be what you expect in all cases.

The long and short is start with the angles and set them yourself and create a quaternion from them when you need a quaternion to rotate a spatial.

float[] angles = new float[] { 0, 0, 0 } // my x, y, z rotation.
angles[1] = FastMath.HALF_PI; // rotate the Y axis by 90 degrees.

Quaternion myRotation = new Quaternion().fromAngles(angles);
mySpatial.setLocalRotation(myRotation);