Search code examples
openglquaternionsglm-mathbullet

Bullet physics and OpenGL convert Orientation to Front, Up and Right


I am using bullet physics and OpenGL to create a 3D space shooter game. I am having a bit of a trouble converting some quaternions retrieved from bullet rigid body to Front, Right and Up vectors. I have managed to retrieve the Front vector as follows :

glm::quat Orientation = this->GetOrientation();

glm::quat qF = Orientation * glm::quat(0, 0, 0, 1) * glm::conjugate(Orientation);
Front = { qF.x, qF.y, qF.z };

But I am not able to retrieve the Up or the Right. Any ideas on how this can be done?


Solution

  • While calculating the front vector, you are applying the orientation to the z axis. The bold part is the vector that is being rotated:

    qlm::quat(0, 0, 0, 1)

    So, to find any other vector like right, left, bottom or top, you just need to rotate the correct axis. Try changing this quaternion to some of the following, and pick the ones you need:

    qlm::quat(0, 1, 0, 0)

    qlm::quat(0, 0, 1, 0)

    qlm::quat(0, -1, 0, 0)

    qlm::quat(0, 0, -1, 0)

    My guess is that y axis is the up direction, so I would try 0, 1, 0 first.

    Once you have the up vector, you can also calculate the right vector using a cross-product (or vice versa):

    right = glm::cross(front, up)