Rotation between vectors: how to calculate

Hi all,
I’m trying to animate the fingers of a hand using the bone positions coming from mediapipe.
I need to compute the right rotation (quaternion or euler) for each bone of my hand, but I’ve some troubles right now.

This is what I do at the moment:

  • compute the world positions of each bone
  • calculate the direction vectors of each node respect to the parent and children
  • try to calculate the proper rotation for each bone
  • apply the calculated rotation to each bone

The code to compute the direction vectors is the following:

 private void ComputeHierarchyDirections( out Vector3 fromParent, out Vector3 toChildren )
        {
            Vector3 parentPosition = Vector3.zero;
            Vector3 childrenPosition = Vector3.zero;

            if ( Parent )
                parentPosition = Parent.Position;
            else
                parentPosition = transform.up;

            if ( Children.Length > 0 )
                childrenPosition = ChildrenPosition;
            else
                childrenPosition = -transform.up;

            fromParent = parentPosition - Position;
            toChildren = childrenPosition - Position;
        }

The code I’m using to compute the rotations is:

 public static Quaternion QuaternionFromVectors( Vector3 v1, Vector3 v2 )
        {

            Vector3 axis = Vector3.Cross( v1, v2 );

            float angle1 = Mathf.Acos( Vector3.Dot( v1, v2 ) / v1.magnitude / v2.magnitude );
            float angle2 = Mathf.Acos( Vector3.Dot( axis, v2 ) / axis.magnitude / v2.magnitude );

            // Convert the angles from radians to degrees if desired
            angle1 = Mathf.Rad2Deg * angle1;
            angle2 = Mathf.Rad2Deg * angle2;

            Quaternion q = Quaternion.Euler( angle1, angle2, 0 );

            return q;
        }

Help please!

This is the screenshot of my hand

Why not just use Quaterion.FromToRotation? Unity - Scripting API: Quaternion.FromToRotation

thank you for reply.
I’m trying it and it returns my quite different values respect to the old solution.
I was pretty sure Quaternion.FromToRotation could solve my problem, but unexpectedly it produces worst results.
8704566--1175325--fromto.png

What I really need is to calculate the local rotations of each bone starting from the positions of each one. If so, I’ve solved everything.

The image shows the result using FromToRotation…

But what vectors do you actually use? Do you use local space direction vectors with FromToRotation? Also the resulting quaternion is only the relative rotation from the initial rotation. So it would need to be applied to the localRotation of that bone. The localRotation of an object is of course relative to the parent object.

I’m using 2 normalized vectors that represent the directions to the parent node and the child node (for each bone of the fingers). So, I’m using thos vectors to calculate the rotations to be applied to each bone. 8704950--1175529--6654.png

Hi! I’m having the same trouble, I also tried to modify the holistic solution to take the 3d world landmark of the hand, but it doesn’t work. Those are the results. Did you figured out how to solve it?


8866332--1210155--Schermata 2023-03-10 alle 10.36.00.png