babylon.freeCameraVirtualJoystickInput.ts 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. module BABYLON {
  2. /**
  3. * Manage the Virtual Joystick inputs to control the movement of a free camera.
  4. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  5. */
  6. export class FreeCameraVirtualJoystickInput implements ICameraInput<FreeCamera> {
  7. /**
  8. * Defines the camera the input is attached to.
  9. */
  10. public camera: FreeCamera;
  11. private _leftjoystick: VirtualJoystick;
  12. private _rightjoystick: VirtualJoystick;
  13. /**
  14. * Gets the left stick of the virtual joystick.
  15. * @returns The virtual Joystick
  16. */
  17. public getLeftJoystick(): VirtualJoystick {
  18. return this._leftjoystick;
  19. }
  20. /**
  21. * Gets the right stick of the virtual joystick.
  22. * @returns The virtual Joystick
  23. */
  24. public getRightJoystick(): VirtualJoystick {
  25. return this._rightjoystick;
  26. }
  27. /**
  28. * Update the current camera state depending on the inputs that have been used this frame.
  29. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  30. */
  31. public checkInputs() {
  32. if (this._leftjoystick){
  33. var camera = this.camera;
  34. var speed = camera._computeLocalCameraSpeed() * 50;
  35. var cameraTransform = Matrix.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, 0);
  36. var deltaTransform = Vector3.TransformCoordinates(new Vector3(this._leftjoystick.deltaPosition.x * speed, this._leftjoystick.deltaPosition.y * speed, this._leftjoystick.deltaPosition.z * speed), cameraTransform);
  37. camera.cameraDirection = camera.cameraDirection.add(deltaTransform);
  38. camera.cameraRotation = camera.cameraRotation.addVector3(this._rightjoystick.deltaPosition);
  39. if (!this._leftjoystick.pressed) {
  40. this._leftjoystick.deltaPosition = this._leftjoystick.deltaPosition.scale(0.9);
  41. }
  42. if (!this._rightjoystick.pressed) {
  43. this._rightjoystick.deltaPosition = this._rightjoystick.deltaPosition.scale(0.9);
  44. }
  45. }
  46. }
  47. /**
  48. * Attach the input controls to a specific dom element to get the input from.
  49. * @param element Defines the element the controls should be listened from
  50. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  51. */
  52. public attachControl(element : HTMLElement, noPreventDefault?: boolean): void {
  53. this._leftjoystick = new VirtualJoystick(true);
  54. this._leftjoystick.setAxisForUpDown(JoystickAxis.Z);
  55. this._leftjoystick.setAxisForLeftRight(JoystickAxis.X);
  56. this._leftjoystick.setJoystickSensibility(0.15);
  57. this._rightjoystick = new VirtualJoystick(false);
  58. this._rightjoystick.setAxisForUpDown(JoystickAxis.X);
  59. this._rightjoystick.setAxisForLeftRight(JoystickAxis.Y);
  60. this._rightjoystick.reverseUpDown = true;
  61. this._rightjoystick.setJoystickSensibility(0.05);
  62. this._rightjoystick.setJoystickColor("yellow");
  63. }
  64. /**
  65. * Detach the current controls from the specified dom element.
  66. * @param element Defines the element to stop listening the inputs from
  67. */
  68. public detachControl(element: Nullable<HTMLElement>): void {
  69. this._leftjoystick.releaseCanvas();
  70. this._rightjoystick.releaseCanvas();
  71. }
  72. /**
  73. * Gets the class name of the current intput.
  74. * @returns the class name
  75. */
  76. public getClassName(): string {
  77. return "FreeCameraVirtualJoystickInput";
  78. }
  79. /**
  80. * Get the friendly name associated with the input class.
  81. * @returns the input friendly name
  82. */
  83. public getSimpleName(): string {
  84. return "virtualJoystick";
  85. }
  86. }
  87. (<any>CameraInputTypes)["FreeCameraVirtualJoystickInput"] = FreeCameraVirtualJoystickInput;
  88. }