virtualrealitycontrollers.adoc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. = Virtual Reality Controllers Legacy Support
  2. :revnumber: 1.0
  3. :revdate: 2021/12/29
  4. == Where are we, what are we pointing at
  5. Be aware that the controllers positions and rotations are in world coordinates, not relative to the camera
  6. To get the number of controllers:
  7. vrAppState.getVRinput().getTrackedControllerCount(); //very likely to be 2, for the 2 hands but this is not guaranteed
  8. To get the position of a controller:
  9. vrAppState.getVRinput().getFinalObserverPosition(i);
  10. To get the orientation of the controller:
  11. vrAppState.getVRinput().getFinalObserverRotation(i)
  12. It is also possible to get the controller pose, which is a combination of position and rotation.
  13. == Buttons
  14. To get the button press states with the following method:
  15. VRInputAPI vrInput = vrAppState.getVRinput();
  16. boolean grip = vrInput.isButtonDown(i, VRInputType.ViveGripButton); //<--Don't worry about the way it says "Vive", anything that supports SteamVR/OpenVR will work with this
  17. The above assumes you are using SteamVR/OpenVR (aka set your settings as `settings.put(VRConstants.SETTING_VRAPI, VRConstants.SETTING_VRAPI_OPENVR_LWJGL_VALUE)` )
  18. == Feedback
  19. To make the controllers rumble:
  20. VRInputAPI vrInput = vrAppState.getVRinput();
  21. vrInput.triggerHapticPulse(i, 0.3f);
  22. == Sample Application
  23. [source,java]
  24. ----
  25. public class Main extends SimpleApplication{
  26. public static void main(String[] args) {
  27. AppSettings settings = new AppSettings(true);
  28. settings.put(VRConstants.SETTING_VRAPI, VRConstants.SETTING_VRAPI_OPENVR_LWJGL_VALUE);
  29. VREnvironment env = new VREnvironment(settings);
  30. env.initialize();
  31. if (env.isInitialized()){
  32. VRAppState vrAppState = new VRAppState(settings, env);
  33. Main app = new Main(vrAppState);
  34. app.setLostFocusBehavior(LostFocusBehavior.Disabled);
  35. app.setSettings(settings);
  36. app.setShowSettings(false);
  37. app.start();
  38. }
  39. }
  40. public Main(AppState... appStates) {
  41. super(appStates);
  42. }
  43. @Override
  44. public void simpleInitApp() {
  45. Box b = new Box(1, 1, 1);
  46. Geometry geom = new Geometry("Box", b);
  47. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  48. mat.setColor("Color", ColorRGBA.Blue);
  49. geom.setMaterial(mat);
  50. rootNode.attachChild(geom);
  51. }
  52. List<Geometry> handGeometries = new ArrayList<>();
  53. @Override
  54. public void simpleUpdate(float tpf) {
  55. VRAppState vrAppState = getStateManager().getState(VRAppState.class);
  56. int numberOfControllers = vrAppState.getVRinput().getTrackedControllerCount(); //almost certainly 2, one for each hand
  57. //build as many geometries as hands, as markers for the demo (Will only tigger on first loop or if number of controllers changes)
  58. while(handGeometries.size()<numberOfControllers){
  59. Box b = new Box(0.1f, 0.1f, 0.1f);
  60. Geometry handMarker = new Geometry("hand", b);
  61. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  62. mat.setColor("Color", ColorRGBA.Red);
  63. handMarker.setMaterial(mat);
  64. rootNode.attachChild(handMarker);
  65. handGeometries.add(handMarker);
  66. }
  67. VRInputAPI vrInput = vrAppState.getVRinput();
  68. for(int i=0;i<numberOfControllers;i++){
  69. if (vrInput.isInputDeviceTracking(i)){ //might not be active currently, avoid NPE if that's the case
  70. Vector3f position = vrInput.getFinalObserverPosition(i);
  71. Quaternion rotation = vrInput.getFinalObserverRotation(i);
  72. Geometry geometry = handGeometries.get(i);
  73. geometry.setLocalTranslation(position);
  74. geometry.setLocalRotation(rotation);
  75. boolean grip = vrInput.isButtonDown(i, VRInputType.ViveGripButton); //<--Don't worry about the way it says "Vive", anything that supports SteamVR/OpenVR will work with this
  76. if (grip){
  77. geometry.getMaterial().setColor("Color", ColorRGBA.Green);
  78. }else{
  79. geometry.getMaterial().setColor("Color", ColorRGBA.Red);
  80. }
  81. boolean trigger = vrInput.wasButtonPressedSinceLastCall(i, VRInputType.ViveTriggerAxis);
  82. if (trigger){
  83. vrInput.triggerHapticPulse(i, 0.3f);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. ----