babylon.virtualJoysticksCamera.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module BABYLON {
  2. // We're mainly based on the logic defined into the FreeCamera code
  3. export class VirtualJoysticksCamera extends FreeCamera {
  4. private _leftjoystick: BABYLON.VirtualJoystick;
  5. private _rightjoystick: BABYLON.VirtualJoystick;
  6. constructor(name: string, position: Vector3, scene: Scene) {
  7. super(name, position, scene);
  8. this._leftjoystick = new BABYLON.VirtualJoystick(true);
  9. this._leftjoystick.setAxisForUpDown(BABYLON.JoystickAxis.Z);
  10. this._leftjoystick.setAxisForLeftRight(BABYLON.JoystickAxis.X);
  11. this._leftjoystick.setJoystickSensibility(0.15);
  12. this._rightjoystick = new BABYLON.VirtualJoystick(false);
  13. this._rightjoystick.setAxisForUpDown(BABYLON.JoystickAxis.X);
  14. this._rightjoystick.setAxisForLeftRight(BABYLON.JoystickAxis.Y);
  15. this._rightjoystick.reverseUpDown = true;
  16. this._rightjoystick.setJoystickSensibility(0.05);
  17. this._rightjoystick.setJoystickColor("yellow");
  18. }
  19. public _checkInputs(): void {
  20. var cameraTransform = BABYLON.Matrix.RotationYawPitchRoll(this.rotation.y, this.rotation.x, 0);
  21. var deltaTransform = BABYLON.Vector3.TransformCoordinates(this._leftjoystick.deltaPosition, cameraTransform);
  22. this.cameraDirection = this.cameraDirection.add(deltaTransform);
  23. this.cameraRotation = this.cameraRotation.addVector3(this._rightjoystick.deltaPosition);
  24. if (!this._leftjoystick.pressed) {
  25. this._leftjoystick.deltaPosition = this._leftjoystick.deltaPosition.scale(0.9);
  26. }
  27. if (!this._rightjoystick.pressed) {
  28. this._rightjoystick.deltaPosition = this._rightjoystick.deltaPosition.scale(0.9);
  29. }
  30. super._checkInputs();
  31. }
  32. public dispose(): void {
  33. this._leftjoystick.releaseCanvas();
  34. super.dispose();
  35. }
  36. }
  37. }