2
0

virtualrealitycontrollers.adoc 4.6 KB

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