# Rotate a vector to align with another vector(but keep the same up direction)

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

See:

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

[ul]
[li]it’s generally in the passed-in “up” direction,[/li][li]it’s orthogonal to the “forward” and “right” vectors, but[/li][li]the “forward” and “right” vectors remain fixed to the vectors passed in.[/li][/ul]

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