Search code examples
c++quaternions

Orienting an object based on a parent with quaternions?


I have 2 objects in 3D space, A & B, and object B is parented to A.

Both objects have 3D positions, as well as a Quaternion representing their specific orientations.

I have translation working fine, so whenever A moves, B moves.

However, I can't seem to get the orientation from the parent to be correctly applied to its children.

Lets say A's orientation represents a 90 degree rotation around the X axis. With my code, object B seems to rotate around 180 degrees for some reason.

Here's a picture of exactly what's happening.

A is on the left, B is on the right

Here's how I'm attempting to generate a vector for any particular vertex, given the child and parent's position and orientation:

vec4 finalVertex = rotVertexByQuat(parentOrientation, vec4(parentPos,1) +  vec4(objPos,1) + rotVertexByQuat(objOrientation, vertex) );

I rotate the vertex by the quaternion this way:

vec4 rotVertexByQuat(Quaternion quat, vec4 vert)
{
    Quaternion p1 = Quaternion(1, vec3(vert.x,vert.y,vert.z));
    Quaternion p2 = multiplyQuaternion(quat,p1);
    Quaternion p3 = multiplyQuaternion(p2, inverseQuaternion(quat));
    return vec4(round(p3.v.x), round(p3.v.y), round(p3.v.z),1);
}

Is there something wrong with my order of operations?


Solution

  • See the answer to this question.

    I have a feeling what you are trying to implement is the Quaternion-Vector rotation operation. There are many different, obscure combinations of Quaternion conventions that can ruin your day (see this paper). if your rotation operation is not working try:

    V_inertial = q (x) V_body (x) q*
    

    Where (x) is the quaternion multiplication operator and q* is the conjugate quaternion of q.

    In addition your "stacked" quaternions may be either:

    q_C = q_A (x) q_B
    or 
    q_C = q_B (x) q_A
    

    depending on the convention used.