Search code examples
mathquaternions

Keep a relative position using quaternion orientation


Let's say I have an object (A) with a position stored as a 3-number vector, and an orientation stored as a quaternion.

I have a second object (B) with the same information.

Object B is, for whatever reason, suddenly welded to object A. It is now stuck, and should perfectly move around relative to object A.

If object B is stuck to a corner of object A, and object A rotates, object B should be in the same relative position and orientation in that same corner as it was before.

To illustrate what I'm asking, it should look something like this:

Image

So far I've managed to take object B's position and transform it by the inverse of the object A's position/orientation, storing an accurate relative position - then when object A rotates, I simply transform the relative position by object A's position/orientation, and teleport object B to that position. This keeps the object B on the corner of A, as expected... however, this does not rotate object B to match object A... how should I go about tracking the relative orientation, using quaternions or matrices (Euler angles are a worst case scenario only, as they are expensive to calculate).

EDIT: I only have a quaternion from before and a quaternion from after. No details on the rotation itself.

In addition, the rotation is arbitrary and free, it could be about any axis or multiple axes at once.


Solution

  • The most perfect answer has come to me after a lot of googling and experimentation: Disregard original ideas, acquire matrices!

    B.Matrix * Invert(A.Matrix) is stored as a relative marker, And then to restore, simply replace B's matrix with relative * A.MATRIX.

    This perfectly adjusts both rotation and position in one all-mighty swoop.

    I have no idea how to do the same with quaternions, but who cares, when we have matrices!