Visualizing quaternions – proper rotation with Bullet Physics

By , last updated September 30, 2019

In our game “Burnt islands”, you should be able to place objects with an arbitrary orientation.

We’re using Bullet Physics for our physics engine, and it uses quaternions as the internal representation of bullet rotation and orientation. So here we will need to implement some quaternion physics.

To rotate an object by mouse input (dx/dy/dz), you want to rotate the object around the X, Y and Z axis regardless of the previous orientation of the object. After some trial and failure, we found the solution to be quite easy.

The inputs are dx, dy and dz.

double dx = message.Dx;
double dy = message.Dy;
double dz = message.Dz;

Next is some housekeeping where we get the current transform of the shape.

btTransform trans       = m_Body->getCenterOfMassTransform();
btQuaternion transrot   = trans.getRotation();

Next we make our own quaternion where we explicitly set the XYZ-components of the quaternion to the dx/dy/dz values.

btQuaternion rotquat;
rotquat = rotquat.getIdentity();
rotquat.setX(dx);
rotquat.setY(dy);
rotquat.setZ(dz);

We multiply our rotation quaternion with the original quaternion. Note, the order of multiplication is important!

transrot = rotquat * transrot;

And finally set the rotation to the bullet shape.

trans.setRotation(transrot);
m_Body->setCenterOfMassTransform(trans);

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.