Questions about quaternions

Hi. I been having trouble understanding quaternions(yes i have checked out the jme tut) i’ve looked at all kinds of tut’s online but they are all so confusing and dont give much code examples just math stuff. my question is a vector3f(1, 0, 0) rotation same as a quaternion(1,0,0,0) because it doesnt look anything alike when i try them with jme. can someone link me to a really dumbed down tut or show me just how it works? maybe some quick examples would be nice like how to do a rotation of 1, 0, 0 then i can work off from there and maybe start to understand them.

Welcome in the club.
As you’ve already noticed quaternion values are not angles, thankfully the Quaternion class provides some methods that translate radian angles to Quaternions and viceversa. See Quaternion::fromAngles Quaternion::toAngles Quaternion:fromAngleAxis etc…

2 Likes

Hi,

http://wiki.jmonkeyengine.org/doku.php/jme3:math_for_dummies this does give code example. Do not use them directly as in (1,0,0,0) like you tried.

setRotation( new Quaternion().fromAngles(0, FastMath.DEG_TO_RAD * 180, 0)); 

180 around z, so turning your body on the spot.

setRotation( new Quaternion().fromAngles(FastMath.DEG_TO_RAD * 90, 0, 0)); 

90 around x… Image you lying down (can’t remember if you’d be lying on your front or on your back though lol)

I also struggled with Quats at first but manage now using similar stuff to the above. There is some business with ordering though when it comes to creating a Quaternion from these “Euler” angles as they are called. So putting multiple angles in may not give you what you expect (rotations take place one at a time in the order: Y, Z, then X).

I’m no expert but I’m happy to help you with further rotations lol

1 Like

The trick is not to try to understand the values of a quaternion. You can get very far in 3D development simply treating them like a black box. They define a 3D rotation in an efficient way. Don’t worry too much about how the magic happens.

1 Like

Would would i convert it into a vector3f? like for example if i get a physicsRotation from a rigidbody it returns a quaternion how would i go about converting this data to a vector3f is there a method im missing or something?

As written, your question makes no sense. “How would I convert a car into an apple?”

What is it that you are ACTUALLY trying to do? A rotation is not a vector so it makes no sense to convert one to the other.

I’m pretty sure he is talking about Matrix3f :wink:

So the question would be: Would it be possible to convert it into a matrix3f?

Let’s create an example: He is playing with physics boxes. Everytime he throws it, it behaves like it should with standard bullet-physics. After that he wants to create a box which is changing colo(u)r everytime a specific side of the rolling box is showing upwards. Like on a dice… everytime a different number apperas, the colo(u)r should change. For that case he would have to check the rotation of the cube and gets a quaternion as result. But quaternions are kind of ununderstable :smile:, so he wants to convert it to a Matrix3f, cause he is more familiar with this format. Have I got it right?

Do you want to get axis-aligned angles back from quaternion? Then try toAngles method but be aware that it should be avoided due to precision loss if you later use that angles for rotation again - it exists mostly for display purposes. For any operations with angles where you have quaternion it’s better to perform operations on quaternion and then to use it to apply modifications, bypassing conversion to angles and back.

That’s a HUGE leap. It’s much more likely he wants an angle or simply to rotate a vector. Especially since your hypothesis is trivially answered by looking at the javadoc or simply typing a ‘.’ in the SDK.

We just don’t know because the question wasn’t asked. So we have to wait for OP to tell us more about what he actually wants to do instead of what he thinks he wants to do.

I assume you want to know how euler angles stored in Vector3f are calculated in to quaternion? If so, then this should help

EulerToQuat(float x, float y, float z, Quaternion quat)
{
  float cr, cp, cy, sr, sp, sy, cpcy, spsy, qx, qy, qz, qw;
  // trig stuff
  cr = FastMath.cos(z/2f); // z is inverted due to change in handedness of coord systems ? Not sure, if so, just use -z.
  cp = FastMath.cos(x/2f);
  cy = FastMath.cos(y/2f);
  sr = FastMath.sin(z/2f); // z is inverted due to change in handedness of coord systems ? Not sure, if so, just use -z.
  sp = FastMath.sin(x/2f);
  sy = FastMath.sin(y/2f);
  cpcy = cp * cy;
  spsy = sp * sy;
  
  qx =  sr * cpcy - cr * spsy;
  qy =  cr * sp * cy + sr * cp * sy;
  qz =  cr * cp * sy - sr * sp * cy;
  qw = cr * cpcy + sr * spsy;

  quat.set(qx,qy,qz,qw);
  
}

…you already have in built command for this, but this will help you to get better understanding of whats going on…i hope this helps…

EDIT:
…as pointed out, instead of roll, pitch and yaw changed to x,y,z …

One problem with labeling the angles as roll, pitch, and yaw is that there is an inherent assumption as to which axis is ‘forward’. That’s why JME uses x, y, z in its fromAngles() method.

I mean how do would i do a rotation of (1,0,0) as a quaternion

quat.mult(yourVector)

Edit: or wait… what do you mean by a rotation of (1,0,0) as a quaternion. What does a rotation of (1,0,0) mean?

Tell us what you are actually trying to do at a higher level because there is a communication gap that makes it hard to interpret your questions.

I just want a example that i can work from. okay so when you do new Quaternion().fromAngleAxis(angle, axis) how would i get the data from a getLocalRotation() to a vector.

Vector3f out_axis=new Vector3f();
float out_angle=quaternion.toAngleAxis(out_axis) 

http://javadoc.jmonkeyengine.org/com/jme3/math/Quaternion.html

Thank you. also i have looked around and all the quaternion tutorials are pretty hard to understand like im a grade 11 im no mathematician i simply cant find a tut that is simple enough for me to understand. Any of you guys know of any youtube clips or websites that explain them for dummies?

You will have to tell us what you are actually trying to do because your questions make no sense.

It’s becoming more and more like this kind of question:

Tell us what you actually want to do with this information and we might be able to help. You certainly DO NOT want to get a vector from a quaternion. What would you possibly do with it?

You “might” want to transform it… but we don’t know because we don’t know what you are actually trying to do. What is your end result?

Put another way, I’ll use a metaphor…

Here is a quaternion:

Here is a Vector:

So far, you’ve been asking how you turn a wrench into a bolt. It’s not really a common thing to want to do and the answer is not going to be helpful to you.

If you want to know how a wrench can turn a bolt then that’s a better question. You don’t have to know anything about how a wrench is built, what its metallurgic qualities are or any of that. You just have to know how to put it in your hand and how to move it.

Quaternions are the same. Do not concern yourself with what those four values mean. I’ve been doing 3D graphics for over 20 years and I don’t know what they mean. It isn’t important at all to using them.

Math for Game Developers - Rotation Quaternions