How can I get this rotation with quaternions ?

I have this simple rotation:

void Start()
{
		transform.Rotate(new Vector3(45, 0, 0), Space.World);
		transform.Rotate(new Vector3(0, 90, 0), Space.World);
}

I want to turn this rotation into quaternion rotation. This is what I have tried but it is not the same rotation:

transform.rotation = Quaternion.AngleAxis(45, Vector3.right) * Quaternion.AngleAxis(90, Vector3.up);

How could I write above rotation in quaternion notation ?

AngleAxis is to convert from axis-angle representation, it’s not necessarily the same thing.

think of it like this, you said 45 degrees around ‘right’… 45 degrees off what though? What’s 0 degrees around ‘right’? Well axis-angle representation has a definition for what is the 0 position around an axis… but it’s nothing like euler angles.

you want euler

transform.rotation = Quaternion.Euler(45,0,0) * Quaternion.Euler(0,90,0);

Huh, I’m a little confused.

You sad:

but that’s 45 deg. around “x” in World space. Since “transform.rotation” in doc says that it rotates around World coords.
The rotation I’m getting with my quaternion equation is around local and that’s why I can’t get it in world.

Anywhay I have tried the equation you submitted and it is still the same rotation I get with AngleAxis.

Is there maybe some other way to rotate with quaternions and get the initial rotation with Rotate ?

45 degrees OFF WHAT though? What do you think AngleAxis thinks 0 degrees around ‘right’ looks like? AngleAxis is NOT Euler angles, and it’s NOT the same as 45 degrees pitch (which is 45 degrees around right-axis off the forward-axis). Sometimes they may appear similar, but not always, what do you think 45 degrees around <0.707, 0.707, 0> looks like (a valid axis-angle)? If you want to deal with Euler angles, deal with Euler angles.

As for your continuing problem, it may be because of order operations. Quaternion * appends one rotation to the next.

It’d be like doing ‘Space.Self’ on the transform.Rotate method.

So combine them into one:

transform.rotation = Quaternion.Euler(45,90,0);

and if you don’t have the initial rotation to append to world space wise like this… well get it as eulers

var e = transform.rotation.eulerAngles;
e.y += 90;
transform.rotation = Quaternion.Euler(e);

0 degrees around “right” is no rotation. In AngleAxis first parameter is angle that is in degrees and the second is the axis as sad in doc. But that angle is around axis you choose. It can be any rotation. So if you want it you can make AngleAxis to rotate same as Euler angle as Euler angles is only a rotation around base vectors, am I right ?
Like Euler(20, 0, 0) is the same as AngleAxis(20, Vector3.right) and that being around current frame of reference, usualy local coord system.

Last rotation that you submitted works. Thx very much.
It is the same rotation as the first one.

Now I’m beginning to wonder what is Euler(45, 90, 0) function doing inside ? What is the quaternion math to arrive at that result ?

yep, 0 around axis A is no rotation

0 degrees pitch, 0 degrees yaw, 0 degrees roll, IS a rotation

And that was my point. And why I linked to angle-axis notation wikipedia entry so you can see the math of WHY euler and angle-axis aren’t the same.

As for the conversion of euler to and from quaternion:

Ok, I see I’m missing something out here. Becase I would say that 0 pitch, 0 yaw, 0 roll is no rotation.

This is how I would do it before:

		quaternion = new Quaternion(Mathf.Sin(angle/2.0f)*v.x, Mathf.Sin(angle/2.0f)*v.y, Mathf.Sin(angle/2.0f)*v.z, Mathf.Cos(angle/2.0f));

Here “angle” is the angle to rotate in radians, and “v” is the axis to rotate about.
This equation si the same as AngleAxis exept that AngleAxis takes angles in degrees.

Well anywhay, to not go too deep, what my first intention was is to see how could I rotate with quaternion math (with using quaternion combinations or something else) an object around only world coords. Becase when you start to use combinations (*) you no longer rotate around world coords but instead around previous quaternion.
You sad that I can use Quaternion.Euler() which does rotate around world coords since there is no quaternion used in combination before. And I can use three rotations here.

So the last thing that I wanted to ask you how would you rotate using multiple rotations around world coord ? Using Quaternion.Euler() in combination wont work since the next rotation will depend on previous Quaternion.Euler() rotation and not on world coord.

For eg.:

void Start()
{
        transform.Rotate(new Vector3(45, 0, 0), Space.World);
        transform.Rotate(new Vector3(0, 90, 0), Space.World);
transform.Rotate(new Vector3(45, 0, 0), Space.World);
}

0 pitch, 0 yaw, and 0 roll IS a rotation. It’s the default rotational position.

No rotation would mean… No orientation. This is a very different case. Try doing this somewhere:

		var v = new Vector3(0.7071f,0.7071f,0f);
		var q = Quaternion.AngleAxis(0, v);
		transform.rotation = q;

And you’ll see NOTHING happens. (note the arbitrary axis)

This is important to know… when you attempt to create this quaternion, the default quaternion is set for you. But this is mathematically different. You can’t define what 0 degrees around axis N looks like, because there’s infinite possibilities. Where as we know EXACTLY what 0 roll, 0 pitch, and 0 yaw looks like… it’s looks like default rotation (that’s not NO rotation, that’s DEFAULT rotation… or the origin).

As for your question about local vs global rotation with quaternions.

With quaternions doing world rotations vs local rotations is just an order of multiplication.

//do on update message or something for animating representation
		var q = Quaternion.AngleAxis(5, Vector3.right);
		obj1.transform.rotation = obj1.transform.rotation * q; //rotate to obj1's local space
		obj2.transform.rotation = q * obj2.transform.rotation; //rotate to obj2's global space

I was suggesting earlier using euler angles earlier because you seem to like to think about things in euler angles. Quaternions are NOT euler angles.

Rotation is merely an orientation in N dimensional space (in this case 3 dimensional space).
Orientation is not numeric, it can be described in numerics. Just like translation/position can be described in numerics and isn’t the numbers themselves.

For instance translation can be described in various coordinate systems:
cartesian - these are the x,y,z you are used to

spherical coords - describing points on a series of sphere shells

cylindrical coords - describing points on a series of cylinder shells

WELL

orientation has various representations

Euler angles - the pitch, roll and yaw you’re used to. This is to orientation that cartesian is to translation

Axis Angle - an axis and the angle around the axis

Quaternion - a 4 dimensional spatial orientation

1 Like

Ok, Thank you very much.

I did try the example you suggested. I got no rotation. Than I tried with 0 yaw, 0 pitch, 0 roll and still no rotation. So I assume it is the same as 0 around axis that points in yaw direction (y coord), 0 around axis thats points in roll direction and 0 around axis that points in roll direction.

I have tried using “opposite” combination with quaternions (world coord) and I did get the same results as with the last one that I posted.

		transform.rotation = Quaternion.AngleAxis(45, Vector3.right) * transform.rotation;
		transform.rotation = Quaternion.AngleAxis(90, Vector3.up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(45, Vector3.right) * transform.rotation;

Now I have to figure out why the “opposite” combination results in rotation around world coords.

Yes, I know that quaternions are not in euler angles. I like more to work with quaternions becose I feel I have more freedom. Before when I did not know about Unity I used rotation with matrices but I never had to rotate an object like this around world coord.
So I assume this also works when multiplying matrices.