# 3D Rotation problem

This topic is 2637 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone!

I am currently working on a game which involves moving (and rotating) objects in 3D-space.

I have heard (read) a great deal about using Quaternions to represent and calculate rotations in 3D-space. However, after googling for hours I still don't understand at all how they work, let alone how I could use them to represent/calculate rotations.

Can anyone point me in the right direction? Maybe some (pseudo)code as to how to work with a quaternion, or a site/tutorial/example where a quaternion is used.
All I have read so far are theoretical concepts which I don't understand at all. I know vectors, matrixes and complex numbers, but I still don't understand how this stuff works.

Thanks!

[EDIT]:
I just came across this article on gamedev.net: Do we really need quaternions? In short, it says you might be better off using an axis/angle approach than by using a quaternion. However, I actually tried this, but the problem with that is that I can't manage to make proper calculations.

Say I have this:
- Current direction vector: (1; 1; 0)
- Rotation axis: the x-axis
- Rotation speed: 1 radian per second
(Location is of no concern to the calculations, since that doesn't change with a rotation)

Code example: rotation of the object around the x-axis:
double ry = direction.y * Math.cos(rotation) - direction.z * Math.sin(rotation);
double rz = direction.y * Math.sin(rotation) + direction.z * Math.cos(rotation);
direction.y = ry;
direction.z = rz;

After one second, this would mean:
direction = (1; 0,54; 0,84)

So this calculation works.
However, I can only calculate the direction when the object rotates around the x-, y- or z-axis. Does anyone know how I can apply this kind of calculations when trying to rotate around ANY axis in 3D-space? For example: the axis (-1; 1; 0).

##### Share on other sites
There are only a couple of formulas you really need to use quaternions for 3D rotations. A quaternion is something of the form w+x*i+y*j+z*k, were we call w the real part and the rest the imaginary part. A 3D vector can be seen as a quaternion by making w=0. You should implement quaternion multiplication, for which it's easy to find information online.

The quaternion that represents rotation of an angle alpha around an axis (x,y,z) has real part cos(alpha/2) and imaginary part that is sin(alpha/2) times the unit vector indicating the axis. The way you apply the rotation is by computing

v' = q*v*conj(q)

where conj() means conjugation, which means flipping the sign of the imaginary part (all three numbers).

You should be aware that both q and -q represent the same rotation. This fact is specially important if you try to do interpolation (flip the sign of one of the vectors if the dot product is less than 0).

If you can't get some example to work, post your best attempt and I'll see if I can fix it. I recommend you use Boost.Quaternions.

##### Share on other sites
Thanks for the response.

I think I have figured out some things about the quaternion now. However, I would like to - at any given time - extract the rotation axis and angle from this quaternion.
Is the following assumption correct?
Rotation axis (vector) = (quaternion.x; quaternion.y; quaternion.z);
Angle = quaternion.w;

##### Share on other sites

Thanks for the response.

I think I have figured out some things about the quaternion now. However, I would like to - at any given time - extract the rotation axis and angle from this quaternion.
Is the following assumption correct?
Rotation axis (vector) = (quaternion.x; quaternion.y; quaternion.z);
Angle = quaternion.w;

That's close: The angle is 2*acos(quaternion.w) and you need to normalize the rotation axis.

1. 1
2. 2
Rutin
22
3. 3
4. 4
A4L
14
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013639
×