About rotations and quaternions

Hi,



i have a doubt about rotations:



suppose that i have to rotate a spatial 90 degrees on X and Y axis, what is the pipeline to do so?



I don’t want to know how to do this in JME but the mathematics involved, for example:



I have a vectro3f that stores the rotations on the axis, how can i get a rotation matrix that can be applied to calcolate the model matrix?

Actually I think you might have better luck finding tutorials by searching on google than asking here. It is difficult subject if you are like me and don’t know so much math. So for me I had to search for tutorials that was on my level of understanding. What you know already is something only you know so people here can’t answer that.

Thaks for the reply,



fortunately i’m enough capable of doing math-things.

However i’m stuck in concatenating rotations with quaternions. I’m trying to do a little JME myself for educational porpose using directly OpenGL with LWJGL and that gone very well before facing quaternions.



The problem is that if i try to apply a rotation on more than one axe the rendering will go carzy…

i’ve also tried to use the method of Quaternion.fromAngles but the result is the same.

And if i try to combine rotations multiplying quaternions i will get the same result.



Assuming that the implementations of the classes are correct (i use the org.lwjgl.util.vector lib ) what is the problem?

@kazeshiro said:
Thaks for the reply,

fortunately i'm enough capable of doing math-things.
However i'm stuck in concatenating rotations with quaternions. I'm trying to do a little JME myself for educational porpose using directly OpenGL with LWJGL and that gone very well before facing quaternions.

The problem is that if i try to apply a rotation on more than one axe the rendering will go carzy..
i've also tried to use the method of Quaternion.fromAngles but the result is the same.
And if i try to combine rotations multiplying quaternions i will get the same result.

Assuming that the implementations of the classes are correct (i use the org.lwjgl.util.vector lib ) what is the problem?

Your understanding :) If you get the same result its probably the correct result and you should try and bias your understanding with that info.

Sounds like you are trying to rotate on multiple axis in one go?



Do you know how 3D rotation works mathematically? If not then the wiki page on rotation matrices is a good read: http://en.wikipedia.org/wiki/Rotation_matrix

That explains how rotation on multiple axis works, but you do need a basic understanding of trigonometry and matrices for it to be useful.

But when I use this method in JME it results in a rotation on more axis. In my rendering the cube, for example, begin to deformate instead of rotating.

One thing that i may not got:



Can quaternions be created directly storing more rotations?

for example →

[java]createQuat(float angleOnXAxi, float angleOnYAxi, float angleOnZAxi) {

Quaternion result = new Quaternion();

// some strange math thigs that set directly the quat components xyzw…

return result;

}[/java]



If this is not possible what is the difference in concatenating quaternions and rot matrix?

With quaternion concatenation, does not occur gimbal lock?

Heres the usage info I put together:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

And heres the scientific info, Quaternions are not easy to really understand as they are 4-dimensional.

Quaternion - Wikipedia

In fact i see in those slides that the method fromAngles create a quaternion that stores multiple rotations but when i use it (copy and paste) and I try to apply it , the mesh stars deforming itself.



I’ve also tried to multiply quaternions together to reach this result but the deformation occurs again

Can you post the code? It should just work if you use the methods that are in the slides.

This is the code copied from JME



[java]

public static final Quaternion fromAnglesJME(float xAngle, float yAngle, float zAngle,

Quaternion quat) {

if (quat == null) {

quat = new Quaternion();

}



float angle;

float sinY, sinZ, sinX, cosY, cosZ, cosX;

angle = zAngle * 0.5f;

sinZ = (float) Math.sin(angle);

cosZ = (float) Math.cos(angle);

angle = yAngle * 0.5f;

sinY = (float) Math.sin(angle);

cosY = (float) Math.cos(angle);

angle = xAngle * 0.5f;

sinX = (float) Math.sin(angle);

cosX = (float) Math.cos(angle);



// variables used to reduce multiplication calls.

float cosYXcosZ = cosY * cosZ;

float sinYXsinZ = sinY * sinZ;

float cosYXsinZ = cosY * sinZ;

float sinYXcosZ = sinY * cosZ;



quat.w = (cosYXcosZ * cosX - sinYXsinZ * sinX);

quat.x = (cosYXcosZ * sinX + sinYXsinZ * cosX);

quat.y = (sinYXcosZ * cosX + cosYXsinZ * sinX);

quat.z = (cosYXsinZ * cosX - sinYXcosZ * sinX);



quat.normalise(quat);

return quat;

}

[/java]

No, best make a test case if you think somethings wrong, you’ll probably find out what you do wrong doing it.

unfortunately i can’t do a test case at the moment because i’m using directly lwjgl and not jme.



what i am doing is simple:



loop {



angle += radians

cubeRotation = fromAngles(angle, angle, angle)



modelMatrix = cubeRotation.toRotMatrix()



mvpMatrix = modelMatrix * viewMatrix * projectionMatrix

uniform(mvpMatrix)

drawCube();



}



Note that if i create the rotation with a axis angle method ( Vector.UNIT_Y, angle )

all goes right



But in the first way it does not work as expected

i didn’t properly read the thread, just saw Quaternion multiplication mentioned a lot. Remember QuatA x QuatB != QuatB x QuatA. When i made the FlyCam purely use Quaternions I had to change the order of Quaternion multiplication depending whether the mouse was moving left/right or up/down, so try both ways.



1 is relative to the origin or something

@wezrule said:
i didn't properly read the thread, just saw Quaternion multiplication mentioned a lot. Remember QuatA x QuatB != QuatB x QuatA. When i made the FlyCam purely use Quaternions I had to change the order of Quaternion multiplication depending whether the mouse was moving left/right or up/down, so try both ways.

1 is relative to the origin or something


Unfortunately my problem seems not to be so simple...
the problem is that i don't succeed in combining many orientation.
And sicerely i have not yet understood if the gimbal lock occurs when multiplying quaternions in the same way as matrices.

as my pseudo-code shows the problem is just that.
And i cannot think that the implementation of the math classes is bugged because i'm using th package in lwjgl.

AFAIK one of the main reasons for using Quaternions is to avoid gimbal lock…

Right, but i was not sure :slight_smile: