Avoiding Gimbal Lock in rotations between vectors?

Hello all, thank you for considering my question.

I have read a lot of questions on Gimbal Lock but none of them covered a situation with the involvement of vectors.


Description:
I have two spheres that are moving in a random path (used timelines for that) and the spheres represent two VR hands. The object that is in the middle is irrelevant, it’s just a mesh.

Goal:
What I’m trying to do is to calculate the rotation that an object between the two spheres needs to rotate using only the two spheres.

Current Approach:
The basic idea that I’m building upon is creating a vector from sphere 2 (white) to sphere 1 (red) then saving it, and in the next frame, I’m calculating the vector’s new position and comparing it to the saved one to get the rotational difference. I’m using Rot from XY node to get the Yaw and Pitch, and Rot from ZX to get the Roll.

Current Problems:
The blueprint seems to be doing fine, except for the Gimbal Lock effect that happens with the Roll element at a certain degree (check [GIF][1]).

Questions:

  1. Do you think there's a way to fix this problem without changing the basic approach to the problem (*check my failed solution attempt in the Gimbal Sol function blueprint*)
  2. If you have any experience with quaternions, please share details on how you would use them in this case, and try to be as clear as possible (*I have no experience with quaternions*). Keeping in mind that we're using 2 vectors to calculate the rotation.

P.S.:
You can check the blueprint for this [GIF][1], it’s attached, but it has a lot of trial and error nodes in it, so it might be slightly tedious.


I thank you again for your time.

.

If I understand correctly, you are trying to orient an object along the vector between the two spheres. I haven’t used quaternions in Unreal, but what I think you need to do is convert the rotation to a quaternion by getting the rotation as an axis and angle. To get the axis of rotation, take the cross product of the two vectors (after normalizing them). The resulting vector will be orthogonal to the two original vectors, and its length will be proportional to the sine of the angle between them.

Actually, I just saw that FQuat has a function to create a quaternion from two vectors (in C++). Not sure if there’s an analagous function in blueprints.

So while there’s no “FQuat” node in blueprints, I found a MakeQuat node that returns a variable type Quaternion. But I have no idea as to how to translate that into a rotation.

But there’s still the problem that for some reason the angle between the two vectors is always coming up as 0 when I was trying to use Rotator from Angle and Axis…

Nevermind! I just realized that one of the normalize nodes was not connected… It works perfectly, thanks man!

Hey if you print this to screen, while rotating the Y Axis , do the numbers jump all over the place? or are they consistent from 0 to 360? Looking for a solution where I can Rotate all 3 Axis and have consistent numbers.