0

How to use inverse kinematics on a finger (with rotation)? Hi, I hope you can help me with this problem! I am trying to use the inverse kinematics of a finger, where I only know the rotation of the target (from a sensor) and another in the wrist, in this example I only have two bones (distal, middle), which I am guided by Dogzer example.

This is what my example does, but the inverse kinematics is being executed with respect to the position, and what I need is that it moves with respect to the rotation of the target exampl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[ExecuteInEditMode]

 public class InverseKinematics7 : MonoBehaviour {

  public Transform upperArm;
  public Transform forearm;
  public Transform hand;
  public Transform elbow;
  public Transform target;
  [Space(20)]
  public Vector3 uppperArm_OffsetRotation;
  public Vector3 forearm_OffsetRotation;
  public Vector3 hand_OffsetRotation;
  [Space(20)]
  public bool handMatchesTargetRotation = true;
  [Space(20)]
  public bool debug;

  float angle;
  float upperArm_Length;
  float forearm_Length;
  float arm_Length;
  float targetDistance;
  float adyacent;

  // Use this for initialization
  void Start () {

  }

  // Update is called once per frame
  void LateUpdate () {
      if(upperArm != null && forearm != null && hand != null && elbow != null && target != null){
          upperArm.LookAt (target, elbow.position - upperArm.position);
          upperArm.Rotate (uppperArm_OffsetRotation);

          Vector3 cross = Vector3.Cross (elbow.position - upperArm.position, forearm.position - upperArm.position);



          upperArm_Length = Vector3.Distance (upperArm.position, forearm.position);
          forearm_Length =  Vector3.Distance (forearm.position, hand.position);
          arm_Length = upperArm_Length + forearm_Length;
          targetDistance = Vector3.Distance (upperArm.position, target.position);
          targetDistance = Mathf.Min (targetDistance, arm_Length - arm_Length * 0.001f);

          adyacent = ((upperArm_Length * upperArm_Length) - (forearm_Length * forearm_Length) + (targetDistance * targetDistance)) / (2*targetDistance);

          angle = Mathf.Acos (adyacent / upperArm_Length) * Mathf.Rad2Deg;

          upperArm.RotateAround (upperArm.position, cross, -angle);

          forearm.LookAt(target, cross);
          forearm.Rotate (forearm_OffsetRotation);

          if(handMatchesTargetRotation){
              hand.rotation = target.rotation;
              hand.Rotate (hand_OffsetRotation);
          }

          if(debug){
              if (forearm != null && elbow != null) {
                  Debug.DrawLine (forearm.position, elbow.position, Color.blue);
              }

              if (upperArm != null && target != null) {
                  Debug.DrawLine (upperArm.position, target.position, Color.red);
              }
          }

      }

  }

  void OnDrawGizmos(){
      if (debug) {
          if(upperArm != null && elbow != null && hand != null && target != null && elbow != null){
              Gizmos.color = Color.gray;
              Gizmos.DrawLine (upperArm.position, forearm.position);
              Gizmos.DrawLine (forearm.position, hand.position);
              Gizmos.color = Color.red;
              Gizmos.DrawLine (upperArm.position, target.position);
              Gizmos.color = Color.blue;
              Gizmos.DrawLine (forearm.position, elbow.position);
          }
      }
  }

}

  • why aren't you applying the rotations to the corresponding joints? – Senbazuru Oct 09 '18 at 20:24
  • What do you mean? – Donaldo Manzano Oct 10 '18 at 14:43
  • oh, sorry I misunderstood your example a little bit. I thought you had rotation values for all finger joints. But in your case, you could at least apply the rotation from your "target" IMU to the distal joint (represented in your example by the sphere between distal and middle bone). But I don't think that IK is the way to go for you since you don't have any positional tracking – Senbazuru Oct 10 '18 at 15:49
  • I am using the IK to calculate the angles of the joints that exist between the fingers, the positioning is being calculated with other hardware – Donaldo Manzano Oct 10 '18 at 16:21
  • Correct me if I'm wrong, but isn't "Inverse Kinematics with rotation rather than position" essentially Forward Kinematics? – Enfyve Oct 10 '18 at 22:43

0 Answers0