# Quaternion toAngleAxis always Positive?

Hi folks, total noob here, and I’m a bit confused why I can’t get a zero, or negative angle when calling:

float f = q.toAngleAxis(Vector3f.UNIT_Y);

(actually the first time I call the method I do get back an angle of 0, (i.e. the intial angle is zero).

i.e.Here is what I’m doing:

I have a node that I rotate upto 30 degrees, then I try to rotate it back to the original location (which was 0 at the start), but whenever I call the method…it never makes it back down to zero…The angle returned over time decreases to a minimum of about 0.02+, then it starts increasing again, causing the node to continue to rotate.

Obviously I’m missing something…any pointers/tips greatly appreciated.

well, I see calling q.toAngles(float[])

appears to return the angles with the appropriate sign, guess I can use that method instead You can’t get a negative angle. It’s always positive.

Remember that an angle can be from 0 to 359 degrees. Forward to side, to back, to other side then back to front.

@danomano said:

(actually the first time I call the method I do get back an angle of 0, (i.e. the intial angle is zero).

i.e.Here is what I’m doing:
I have a node that I rotate upto 30 degrees, then I try to rotate it back to the original location (which was 0 at the start), but whenever I call the method..it never makes it back down to zero..The angle returned over time decreases to a minimum of about 0.02+, then it starts increasing again, causing the node to continue to rotate.

Nice if it works now. That has 0.02 thing is a classic tho :). It happens when you keep adding to a float variable rather then setting it to a new value each time. At least that could be the issue here.

http://cboard.cprogramming.com/game-programming/79192-numerical-drift-quaternion-rotations.html

Well as it turns out using:

float[] angles = new float3[];

angles = q.toAngles(angles);

is also not reliable…while it does appear to give you -ve angles…it oddly will flip the sign sometimes giving a postive…

i.e. here is sample output, from doing this:

public void simpleUpdate(float tpf) {

Quaternion q = obj.pivotNode.getLocalRotation();

float[] angles = new float;

angles = q.toAngles(angles);

System.out.println(“UP:” +angles + " " + obj.restAngle);

if (angles > 2.45) {

System.out.println(“rotation complete”);

}

}

below we see the object happily rotating (clockwise) with increasing negative values per iteration…then all of sudden it decides to return a positive value, causing rotation to stop prematurely…(I’m thinking of attempting a hack, such that if the sign changes between iteration…it changes the test condition (I’m only rotating my object by about 40 degrees in total each way…so …it ‘should’ work if I know the current direction of rotation).

----

UP:-3.1375005 -2.792527

rotating

UP:-3.1378212 -2.792527

rotating

UP:-3.1401656 -2.792527

rotating

UP:-3.1404715 -2.792527

rotating

UP:-3.1408424 -2.792527

rotating

UP:-3.1410923 -2.792527

rotating

UP:-3.14139 -2.792527

rotating

UP:3.1414945 -2.792527

rotation complete

sorry I missed the inclusion of the actual rotate call in the above code snippet…here is the snippet with the rotation call…

public void simpleUpdate(float tpf) {

Quaternion q = obj.pivotNode.getLocalRotation();

float[] angles = new float;

angles = q.toAngles(angles);

System.out.println(“UP:” +angles + ” ” + obj.restAngle);

if (angles > 2.45) {

System.out.println(“rotation complete”);

}

else {

obj.pivot.rotate(0, (-1f)*tpf, 0);

}

Its not switching signs all over sudden. It’s not using degrees, but radians. Look at the value where it switches, 3.14… its pi (or 180 degrees).

I’m pretty sure this could be done in a better way tho, but I don’t quite understand what it is you’re trying to accomplish. Do you want that object to rotate back and forth between two set angles all the time? And is the rotation around any of the main axes?

You don’t need to know that stuff btw, check out this page its got description of how to rotate and other stuff: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

@androlo said:
Its not switching signs all over sudden. It's not using degrees, but radians.

Just realized I wanted to mention the radians at the end of my post but forgot. :/