babylon.gamepadCamera.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. // We're mainly based on the logic defined into the FreeCamera code
  10. var GamepadCamera = (function (_super) {
  11. __extends(GamepadCamera, _super);
  12. function GamepadCamera(name, position, scene) {
  13. var _this = this;
  14. _super.call(this, name, position, scene);
  15. this.angularSensibility = 200;
  16. this.moveSensibility = 75;
  17. this._gamepads = new BABYLON.Gamepads(function (gamepad) { _this._onNewGameConnected(gamepad); });
  18. }
  19. GamepadCamera.prototype._onNewGameConnected = function (gamepad) {
  20. // Only the first gamepad can control the camera
  21. if (gamepad.index === 0) {
  22. this._gamepad = gamepad;
  23. }
  24. };
  25. GamepadCamera.prototype._checkInputs = function () {
  26. if (this._gamepad) {
  27. var LSValues = this._gamepad.leftStick;
  28. var normalizedLX = LSValues.x / this.moveSensibility;
  29. var normalizedLY = LSValues.y / this.moveSensibility;
  30. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  31. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  32. var RSValues = this._gamepad.rightStick;
  33. var normalizedRX = RSValues.x / this.angularSensibility;
  34. var normalizedRY = RSValues.y / this.angularSensibility;
  35. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  36. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  37. var cameraTransform = BABYLON.Matrix.RotationYawPitchRoll(this.rotation.y, this.rotation.x, 0);
  38. var speed = this._computeLocalCameraSpeed() * 50.0;
  39. var deltaTransform = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(LSValues.x * speed, 0, -LSValues.y * speed), cameraTransform);
  40. this.cameraDirection = this.cameraDirection.add(deltaTransform);
  41. this.cameraRotation = this.cameraRotation.add(new BABYLON.Vector2(RSValues.y, RSValues.x));
  42. }
  43. _super.prototype._checkInputs.call(this);
  44. };
  45. GamepadCamera.prototype.dispose = function () {
  46. this._gamepads.dispose();
  47. _super.prototype.dispose.call(this);
  48. };
  49. return GamepadCamera;
  50. })(BABYLON.FreeCamera);
  51. BABYLON.GamepadCamera = GamepadCamera;
  52. })(BABYLON || (BABYLON = {}));
  53. //# sourceMappingURL=babylon.gamepadCamera.js.map