View Full Version : Rotate a vector to align with another vector(but keep the same up direction)

07-20-2017, 09:06 AM
I've been trying to find the best way to calculate the rotation that would align vector A with vector B. I have found solutions but in my case I need the relative up direction to be preserved. Therefore I have been trying to accomplish this with just 2 rotations. First on the Y axis to align the vectors relative to Y, then I calculate the angle between them at this point which would give me the X rotation. However, handling these components of rotation separately doesn't produce the desired effect when I apply both rotations. Applying the Y rotation to the vector turns it perfectly to line up with vector B. But when I apply X rotation, it doesn't rotate properly. I know this is most likely because the X rotation is being applied along the regular X axis and not the vector's current x axis. Any suggestions would be awesome. It's really frustrating me

Dark Photon
07-20-2017, 05:44 PM
* gluLookAt code (https://www.khronos.org/opengl/wiki/GluLookAt_code)

Look at the logic for how it recomputes the up direction so that:

it's generally in the passed-in "up" direction,
it's orthogonal to the "forward" and "right" vectors, but
the "forward" and "right" vectors remain fixed to the vectors passed in.

Adapt for which ever set of vectors you want to hold fixed.