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. :smile:

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. :slight_smile: (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.

How about adding to the javadoc the first line @pspeed

If you reading this, you won’t understand this, so just use the methods offered, and stop thinking what the fields actually mean. (But if you are interested they are a 3 dimenional projection off a 4dimensional spheric entity that has 2 imaginary components), see? you dont understand, so stop pretending to be"

I knew one guy who wrote 4D Tetris one day. Ok, I can imagine that more or less. What I can’t imagine - he was really playing it!

3 Likes

https://pbs.twimg.com/media/Bt9HYEtIUAAodFj.jpg

Yep, which is why it’s important to treat the quaterion x,y,z,w as unfathomable magic values. To use quaternions, you definitely do not need to understand them.