freeCameraVirtualJoystickInput.ts 4.2 KB

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