Jump to content
  • Advertisement
Sign in to follow this  
Insdeath

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!