Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using HTC Vive trackers to control humanoid model, and then detect speeds by bones' positions?

Discussion in 'AR/VR (XR) Discussion' started by ysleungrockman, May 2, 2018.

  1. ysleungrockman

    ysleungrockman

    Joined:
    Mar 16, 2014
    Posts:
    32
    Since I am doing a project on VR fighting game, I used 3 HTC Vive trackers along with the headset and controllers for the player to control a character model. Then for the parts to control the damage of an attack, I simply make the game to detect the speed of bone movement, and then determine the damage by the speed. Here is an example of codes that detect right hand bone's speed:
    Code (CSharp):
    1. rightHandLastPos = rightHandCurrentPos;
    2. rightHandCurrentPos = rightHand.position;
    3. rightHandSpeeds.Enqueue (Mathf.Sqrt ((rightHandCurrentPos.x - rightHandLastPos.x) * (rightHandCurrentPos.x - rightHandLastPos.x) + (rightHandCurrentPos.y - rightHandLastPos.y) * (rightHandCurrentPos.y - rightHandLastPos.y) +                             (rightHandCurrentPos.z - rightHandLastPos.z) * (rightHandCurrentPos.z - rightHandLastPos.z)) / Time.deltaTime);
    4.  
    5. if (rightHandSpeeds.Count > 10)
    6.     rightHandSpeeds.Dequeue ();
    7.  
    8. float maxRightHandSpeed = 0;
    9. for (int i = 0; i < rightHandSpeeds.Count; i++) {
    10.       if (rightHandSpeeds.ElementAt (i) > maxRightHandSpeed) {
    11.           maxRightHandSpeed = rightHandSpeeds.ElementAt (i);
    12.       }
    13.  }
    14.  if (tag == "Player") {
    15.       rightHandSpeed = maxRightHandSpeed * 100;
    16.  } else if (tag == "Opponent") {
    17.       rightHandSpeed = maxRightHandSpeed;
    18.  }
    The speed of player's bone was times 100 because sometimes, the actual speed can be as small as about 0.001. However, what the problem is, the detection is quite unstable that the speed can actually be up to several hundreds (or for actual speed, it will be at the range of 1 to 10), which has a hundred times difference. Have I done anything wrong? Or is the detection unreliable?