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!