Search Unity

Vive Controller Joystick Like Mechanics

Discussion in 'AR/VR (XR) Discussion' started by Beloudest, Apr 28, 2016.

  1. Beloudest

    Beloudest

    Joined:
    Mar 13, 2015
    Posts:
    247
    Hi, I'm pretty new to the controllers and just got my inputs all setup and need some advice on where to go next for my first VR game.

    I wish to be able to make one of my controllers work similar to that of a traditional joystick using a normalised x & y value from 0 - 1 or -1 - 1.

    Does anyone have some great advice on how to do this? How can I get the rotation simulating a joystick?

    I realise that the controllers transforms change within world space but I'm not sure how to lock them into a 2d like space.

    Thanks.
     
  2. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    In general the localRotation is in a quaternion I would think if you wanted the orientation of a particular axis (x, y, or z) to get normalized into 0-1 I would convert to Euler Angle (usually Unity likes to keep between -180 and 180) and then divide by 180. You might need to account for if Unity doesn't auto keep the angle in that range to make sure it stays there yourself. For example if you have an angle of 270 degrees you would want to change that to -90 degrees so that your math for normalizing could be simple.
     
    Beloudest likes this.
  3. Beloudest

    Beloudest

    Joined:
    Mar 13, 2015
    Posts:
    247
    Hi thanks for chipping in, I actually dared to try it myself yesterday and did exactly as you said. I indeed got a pole like object rotating in line with the controller. I then decided to try and minimise/restrict the amount the pole/joystick could rotate and this is when problems started. As you mentioned I indeed had trouble with the way eulerAngles respond in Unity, the values jump around and make no sense to a human like me. I literally just cant seem to understand what you're meant to do with eulerAngles if you want accurate values, especially when your dealing with all 3 axis of rotations coming from the controller. I tried using conditions and clamps but they are no good as the values jump suddenly due to Gimble Locking math, it took me a while to realise also. :mad:

    I would love a solution if you can think of one? Essentially I want to visually stop the pole/joystick from moving past a particular angle in all directions from it's centre, like in a plane. Then worry about getting my normalised value. This seems like quite a complex mathematical scenario.

    Maybe I should use a position based method with rigidbody and a joint, I kind feel like that loses its excitement and dexterity.

    Cheers ;)
     
    Last edited: Apr 29, 2016
  4. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    As far as keeping it in the -180 to 180 range it should be straightfoward. If the value goes above 180 subtract 360 and if it goes below -180 add 360.

    When it comes to clamping as long as you keep it within the above range and make the adjustments first, it's usually not difficult to then clamp it further like say 90 degrees or whatever suits your game.