# Quaternions - rotations

I;m new to this subject. I have the following code

``````Quaternion q = new Quaternion() ;

player.setPhysicsLocation(new Vector3f(290.86438f, 1065.6818f, 1190.1605f));
player.setLinearVelocity(Vector3f.ZERO);
player.setAngularVelocity(Vector3f.ZERO);
player.setPhysicsRotation(q.fromAngleAxis(-90 * FastMath.PI/180, new Vector3f(0,1,0)));

System.out.println("InitLocation rotation = " + q.getY() * 180/FastMath.PI);

System.out.println("Player rotation = " + player.getPhysicsRotation().getY()*180/FastMath.PI);
``````

The output of this is

InitLocation rotation = -40.514233
Player rotation = -40.514233
âŚ
player is a VehicleControl type
Iâm just rotating a model around the Y axis. I donât understand why I donât get the same rotation printed out as I put in (-90 degrees).

You get the Y component of the quaternion which is not an angle but a number that youâll only understand if you can think in three dimensions.

So how can I get the angle from that ?

The same way you put it inâŚ toAngleAxis.

Iâm really starting to believe we should rename Quaternionâs x,y,z,w to something like giraffe, jello, green, and bloop. Then maybe people will stop thinking they understand what these mean.

If you want ârotation around some axisâ then use toAngleAxis(). Recognize that it is giving you an angle AND an axisâŚ and that may not be the axis you gave it initially. ProbablyâŚ but you canât necessarily count on it. Euler angles are ambiguous but quaternions are not.

You can use toAngles() which will tell you rotations around the x, y, and z axesâŚ but you may see rotations appear in those other axes.

If you must track rotation around the y axis then you need to keep rotation around the y axis and convert it to a quaternion only when you need to apply it to a spatial. You canât expect to get that same value back out of the quaternion again later.

1 Like

Rofl @pspeed.
Itâs not the first time you say that and I think youâre right

But âgiraffe, jello, green, and bloopâ are not magical enough words for the pure magic of which the Quaternions are made of.

I vote for âabraCadaBra, hocusPocus, flyingUnicorn and sarumansWrathâ

Perhaps too similar and then someone will read something into them. (So âabracadabraâ and âhocuspocusâ must be my x and z since they are related and then âflyingUnicornâ must be y because it goes up and downâŚ" Two magic incantations plus a magic creature. I see you are trying to be âmagicâ but Iâm trying to be completely nonsensical.

Though, mine only works if you donât know the joke âHow many surrealists does it take to change a lightbulb?â

(Answer: âTwo. One to hold the giraffe and the other to fill the bathtub with green jello.â

1 Like

Back to our reality: How many programmers does it take to change a lightbulb?

(Spoiler: No one. Itâs a hardware problem)

1 Like

3 Likes

If I now do:-

`````` Quaternion q = new Quaternion() ;
float rotationY ;

player.setPhysicsLocation(new Vector3f(290.86438f, 1065.6818f, 1190.1605f));
player.setLinearVelocity(Vector3f.ZERO);
player.setAngularVelocity(Vector3f.ZERO);
player.setPhysicsRotation(q.fromAngleAxis(-90 * FastMath.PI/180, new Vector3f(0,1,0)));

rotationY = player.getPhysicsRotation().toAngleAxis(new Vector3f(0,1,0));

System.out.println(" rotationY=" + rotationY*180/FastMath.PI) ;
``````

I get the output

rotationY=90.0

-90 in , +90 out

Whatâs going on ?
thanks

I think you completely ignored my post where I explained toAngleAxis() works. You have no idea what axis itâs returning in the above codeâŚ soâŚ

1 Like

If I say ânew Vector3f(0,1,0)â why am I not entitled to assume itâs returning the Y axis ?

Because thatâs for STORING THE RESULT. Not passing whatever you want. It is physically/mathematically/whatever impossible to turn a quaternion into a single rotation around âwhatever axis you wantâ.

I hate repeating myself so I recommend you actually read my previous post.

I did actually read your post and I thank you for your suggestions which I will now attempt to implement. Now I know that you canât get out of a Quat what you put in Iâll use your suggested approach, and thanks for your advice.

Can you point me to any literature which will give me a better understanding of this subject ?

Not sure. I no longer remember where I picked up what I knowâŚ but you might read about the relationship between Euler angles and Quaternions. Potentially do a few thought experiments about what it means that âEuler angles are ambiguousâ. And you can search for similar forum questions because this same topic just came up last weekâŚ though I think the magic words I used for the quaternion values were different.

Bottom line: for any given orientation there are exactly two Quaternions and one is just an inverse of the other.

For that same orientation, there are infinite number of Euler angle combinations that will produce that orientation.