freeCameraVirtualJoystickInput.ts 4.5 KB

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