babylon.freeCameraGamepadInput.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module BABYLON {
  2. export class FreeCameraGamepadInput implements ICameraInput<FreeCamera> {
  3. camera: FreeCamera;
  4. public gamepad: Gamepad;
  5. private _onGamepadConnectedObserver : Observer<Gamepad>;
  6. private _onGamepadDisconnectedObserver : Observer<Gamepad>;
  7. @serialize()
  8. public gamepadAngularSensibility = 200;
  9. @serialize()
  10. public gamepadMoveSensibility = 40;
  11. // private members
  12. private _cameraTransform: Matrix = Matrix.Identity();
  13. private _deltaTransform: Vector3 = Vector3.Zero();
  14. private _vector3: Vector3 = Vector3.Zero();
  15. private _vector2: Vector2 = Vector2.Zero();
  16. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  17. let manager = this.camera.getScene().gamepadManager;
  18. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  19. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  20. // prioritize XBOX gamepads.
  21. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  22. this.gamepad = gamepad;
  23. }
  24. }
  25. });
  26. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad)=> {
  27. if (this.gamepad === gamepad) {
  28. this.gamepad = null;
  29. }
  30. });
  31. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  32. }
  33. detachControl(element: HTMLElement) {
  34. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  35. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  36. this.gamepad = null;
  37. }
  38. checkInputs() {
  39. if (this.gamepad && this.gamepad.leftStick) {
  40. var camera = this.camera;
  41. var LSValues = this.gamepad.leftStick;
  42. var normalizedLX = LSValues.x / this.gamepadMoveSensibility;
  43. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  44. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  45. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  46. var RSValues = this.gamepad.rightStick;
  47. if (RSValues) {
  48. var normalizedRX = RSValues.x / this.gamepadAngularSensibility;
  49. var normalizedRY = RSValues.y / this.gamepadAngularSensibility;
  50. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  51. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  52. }
  53. else {
  54. RSValues = {x:0, y:0};
  55. }
  56. if (!camera.rotationQuaternion) {
  57. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, this._cameraTransform);
  58. } else {
  59. camera.rotationQuaternion.toRotationMatrix(this._cameraTransform);
  60. }
  61. var speed = camera._computeLocalCameraSpeed() * 50.0;
  62. this._vector3.copyFromFloats(LSValues.x * speed, 0, -LSValues.y * speed);
  63. Vector3.TransformCoordinatesToRef(this._vector3, this._cameraTransform, this._deltaTransform);
  64. camera.cameraDirection.addInPlace(this._deltaTransform);
  65. this._vector2.copyFromFloats(RSValues.y, RSValues.x)
  66. camera.cameraRotation.addInPlace(this._vector2);
  67. }
  68. }
  69. getClassName(): string {
  70. return "FreeCameraGamepadInput";
  71. }
  72. getSimpleName() {
  73. return "gamepad";
  74. }
  75. }
  76. CameraInputTypes["FreeCameraGamepadInput"] = FreeCameraGamepadInput;
  77. }