Home > database >  How to use LookRotation to rotate an object based on hand positions?
How to use LookRotation to rotate an object based on hand positions?

Time:04-09

I'm creating an interaction system for a VR game and I'm struggling with two hand interactions. I'm using the Small direction changes can cause the object to flip over. The light green arrows represent the up direction of the hands, while the dark green is the calculated direction used as an argument for the LookRotation() method.

The obvious solution seems to be to pick a different fixed vector instead of "up", one which won't be so easily aligned with the fwd vector. In the example it could be a vector aligned with the fingers. But keep in mind that there are no restrictions on initial hand rotation so no matter which vector you choose the hands can always happen to be rotated so that the vectors align. And even if you pick the an optimal vector dynamically (one that is perpendicular to fwd), it's still at best 90 degrees from aligning with fwd.

To solve this I tried restricting the direction to the values which don't cause problems but this caused another issues (I had difficulties with determining which values are ok and which should be discarded). I feel like I'm doing something wrong here, is there any better solution to this problem?

CodePudding user response:

You can calculate the delta rotations of the hands and apply it to the "base up" of the object (the new up if we only take into account the change in position of hands...which will of course be orthogonal to the axis of the object). Then determine the change in angle that results in that up being rotated with each hand. Average those angles out, then apply those angles with the hand-hand axis using Quaternion.AngleAxis to the "base up" from earlier. Then you have your forward and up for Quaternion.LookRotation).

Below is an example of how you can use this, including VR hand noise simulation. To see the test, create a new scene in unity and attach this to the camera and it will build the scene on play start. There is a grip/release gui that will appear in Play view. You can adjust the hand rotation in Scene view

example of use

    Vector3 leftHandPosCurrent;
    Vector3 rightHandPosCurrent;

    Vector3 worldAxisPrev;

    Quaternion leftHandRotPrev;
    Quaternion leftHandRotCurrent;

    Quaternion rightHandRotPrev;
    Quaternion rightHandRotCurrent;

    bool isGripping;
    bool firstFrameGripping;

    Rigidbody grippedRB;

    Transform leftHand;
    Transform rightHand;

    Quaternion targetRot;

    /* 
     * On subsequent frames of gripping, calculate deltas in positions and
     * rotations, average out the hand's effects, then apply them to the gripped
     * object
     */
    void HandleGrippedRot()
    {
        Quaternion previousRot = targetRot;

        Vector3 worldAxisCurrent = rightHandPosCurrent - leftHandPosCurrent;

        if (!firstFrameGripping)
        {
            Vector3 prevUp = previousRot * Vector3.up;
            // we haven't moved the transform based on the hands yet, so 
            // find the new up would be ignoring hand rotations
            Vector3 newUp = Quaternion.FromToRotation(worldAxisPrev,
                    worldAxisCurrent) * prevUp;

            float leftHandAngle = GetDegRot(newUp, leftHandRotPrev,
                    leftHandRotCurrent, worldAxisCurrent);
            float rightHandAngle = GetDegRot(newUp, rightHandRotPrev,
                    rightHandRotCurrent, worldAxisCurrent);
            float avgAngle = (leftHandAngle   rightHandAngle) * 0.5f;

            newUp = Quaternion.AngleAxis(avgAngle, worldAxisCurrent) * prevUp;

            targetRot = Quaternion.LookRotation(worldAxisCurrent,
                    newUp);
        }
        else
        {
            firstFrameGripping = false;
        }

        leftHandRotPrev = leftHandRotCurrent;
        rightHandRotPrev = rightHandRotCurrent;

        worldAxisPrev = worldAxisCurrent;
    }

    /*
     * Given the "up" of the object without taking hand rotations into account
     * and the axis, determine how a hand's delta rotation affects that up 
     * around the axis and return the angle of that rotation 
     */
    float GetDegRot(Vector3 baseUp, Quaternion prevHandRot, Quaternion curHandRot,
            Vector3 axis)
    {
        Vector3 adjUp = (curHandRot * Quaternion.Inverse(prevHandRot)) * baseUp;
        adjUp = Vector3.ProjectOnPlane(adjUp, axis);

        return Vector3.SignedAngle(baseUp, adjUp, axis);
    }

    void Update()
    {
        AddVRNoise(leftHand);
        AddVRNoise(rightHand);

        leftHandPosCurrent = leftHand.position;
        rightHandPosCurrent = rightHand.position;

        leftHandRotCurrent = leftHand.rotation;
        rightHandRotCurrent = rightHand.rotation;


        if (isGripping)
        {
            HandleGrippedRot();
        }
    }

    void StartGrip()
    {
        if (isGripping) return;
        isGripping = true;
        firstFrameGripping = true;
        // grippedTransform is set accordingly at some point
    }

    void EndGrip()
    {
        if (!isGripping) return;
        isGripping = false;
    }

    /*
     * example of using targetRot to move rb
     */
    private void FixedUpdate()
    {
        if (!isGripping) return;

        Quaternion delta = targetRot
                * Quaternion.Inverse(grippedRB.transform.rotation);

        delta.ToAngleAxis(out float angle, out Vector3 axis);

        // convert to shortest angle form
        if (angle > 180f)
        {
            axis = -axis; angle = 360f - angle;
        }

        grippedRB.angularVelocity = angle * 0.25f * axis;
    }

    /*
     * just for testing purposes
     */
    void Start()
    {
        leftHand = CreateHand(true);
        leftHand.position = Vector3.left;

        rightHand = CreateHand(false);
        rightHand.position = Vector3.right;

        CreateArrow();
    }

    /*
     * just for testing purposes
     */
    void AddVRNoise(Transform hand)
    {
        Quaternion noise = Random.rotation;
        noise.ToAngleAxis(out float angle, out Vector3 axis);
        angle = 100f * Time.deltaTime;
        noise = Quaternion.AngleAxis(angle, axis);

        Quaternion noisyRot = hand.rotation * noise;
        hand.rotation = noisyRot;
    }

    /*
     * just for testing purposes
     */
    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 100, 50), "Grip"))
        {
            StartGrip();
        }

        if (GUI.Button(new Rect(100, 0, 100, 50), "Release"))
        {
            EndGrip();
        }
    }


    /*
     * just for testing purposes
     */
    Transform CreateHand(bool isLeft)
    {
        string handName = isLeft ? "Left" : "Right";
        GameObject hand = new GameObject($"{handName}hand");
        GameObject palm = GameObject.CreatePrimitive(PrimitiveType.Cube);
        palm.transform.localScale = new Vector3(0.5f, 0.2f, 1f);
        palm.transform.SetParent(hand.transform);
        GameObject thumb = GameObject.CreatePrimitive(PrimitiveType.Cube);
        thumb.transform.localScale = new Vector3(0.2f, 0.1f, 0.1f);
        thumb.transform.SetParent(hand.transform);
        thumb.transform.localPosition = new Vector3(isLeft ? 0.32f : -0.32f,
                0f, -.31f);

        return hand.transform;
    }

    /*
     * just for testing purposes
     */
    void CreateArrow()
    {
        GameObject arrow = new GameObject();
        GameObject body = GameObject.CreatePrimitive(PrimitiveType.Cube);
        body.transform.localScale = new Vector3(1f, 1f, 5f);
        body.transform.SetParent(arrow.transform);
        GameObject head = GameObject.CreatePrimitive(PrimitiveType.Cube);
        head.transform.SetParent(arrow.transform);
        head.transform.localEulerAngles = Vector3.up * 45f;
        head.transform.localPosition = new Vector3(0f, 0f, 2.5f);

        grippedRB = arrow.AddComponent<Rigidbody>();
        grippedRB.useGravity = false;

        arrow.transform.position = 2f * Vector3.up;
    }
  • Related