Search Unity

Two-hand grabbing of objects in Virtual Reality

Discussion in 'Editor & General Support' started by Nesse_M, Feb 5, 2019.

  1. Nesse_M

    Nesse_M

    Joined:
    Jun 8, 2017
    Posts:
    7
    Hi,

    I am currently working on a room-scaled VR game where the Player uses (Oculus- or HTC Vive) controllers in both hands to grab differently-shaped blocks. The blocks can only be grabbed when both hands are at one of the sides of the block and the buttons of both controllers are pressed. The intention is to then base the orientation of the grabbed block on the orientation of both the left and right controller. The block needs to move and rotate based on how the left and right controller are moving and rotating.

    To achieve this, I have made a (C#) script that during runtime makes sure that a sphere (let’s call it the MiddlePoint Sphere) (1) keeps right in the middle of the two controllers position-wise and (2) bases its rotation (orientation) on both the left and right controller. While the former works well, the latter only works partially: the MiddlePoint Sphere rotates correctly until the hands reach a certain angle, whereupon it completely flips its axes.

    Since my knowledge on Quaternions and Slerping is extremely limited, I based a part of my code on recommendations from other coders. This unfortunately leaves me scratching my head when it comes to explaining the incorrect flipping.

    My code, in order of execution (for every frame):

    Code (CSharp):
    1. public void Calculate_MiddlePoint_GlobalPosition () {
    2.  
    3. // - Add X, Y and Z of Left and Right controller positions -
    4. // (OS._Preset is a different script containing the Left- and Right Controller Transforms)
    5.  
    6. float X_LeftAndRight = OS._Preset.Left_Hand_Transform.position.x + OS._Preset.Right_Hand_Transform.position.x;
    7. float Y_LeftAndRight = OS._Preset.Left_Hand_Transform.position.y + OS._Preset.Right_Hand_Transform.position.y;
    8. float Z_LeftAndRight = OS._Preset.Left_Hand_Transform.position.z + OS._Preset.Right_Hand_Transform.position.z;
    9.  
    10. // - Create new Vector3 based on (sum of Left and Right coordinates) divided by (2) -
    11.  
    12. Vector3 CenterPosition = new Vector3 (X_LeftAndRight, Y_LeftAndRight, Z_LeftAndRight) / 2;
    13.  
    14. // - Change position of MiddlePoint Sphere -
    15.  
    16. ThisObject_Transform.position = CenterPosition;
    17.  
    18. }
    19.  
    20. public void Calculate_MiddlePoint_GlobalRotation () {
    21.  
    22. // - Create direction of the MiddlePoint Sphere by taking the <Right Controller position> and subtracting the (normalized) <Left Controller position> -
    23. // - Is this what is causing the incorrect flip? Should it be <Left Controller minus Right Controller> when a certain axis is reached? -
    24.  
    25. Vector3 Direction = (OS._Preset.Right_Hand_Transform.position - OS._Preset.Left_Hand_Transform.position).normalized;
    26.  
    27. // - My lack of knowledge on Quaternions prevents me from describing what happens here -
    28.  
    29. Quaternion _LookRotation = Quaternion.LookRotation (Direction);
    30.  
    31. // - Ditto - not yet knowledgable enough on Slerping either -
    32.  
    33. ThisObject_Transform.rotation = Quaternion.Slerp (ThisObject_Transform.rotation, _LookRotation, 0.5f);
    34.  
    35. }
    I’ve recorded three separate videos where you can see (1) how the MiddlePoint Sphere positions and rotates (correctly and incorrectly) based on the Left and Right controller, (2) what happens when you grab a block (including the incorrect flipping) and (3) how the MiddlePoint Sphere rotates when you specifically rotate your hands forward and backwards.

    Video 1: https://tinyurl.com/yb454l4g
    Video 2: https://tinyurl.com/y9lujulv
    Video 3: https://tinyurl.com/y77qt6bb

    Does anyone know if my current code is along the right lines and what is necessary to make it work entirely correctly? Or, if I am doing the hand orientation wrong: how should I be doing it?

    Any help is greatly appreciated - thanks in advance!

    TL;DR:

    Trying to make a VR application where the user uses both controllers to grab objects. Orientation of the grabbed object is not working properly as it flips its axes when reaching a certain angle. Would appreciate any help on it!
     
  2. BelieveXiaoShuai

    BelieveXiaoShuai

    Joined:
    Nov 6, 2017
    Posts:
    21
    Do u get solution?