babylon.gamepadCamera.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var __extends = 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) {
  18. _this._onNewGameConnected(gamepad);
  19. });
  20. }
  21. GamepadCamera.prototype._onNewGameConnected = function (gamepad) {
  22. // Only the first gamepad can control the camera
  23. if (gamepad.index === 0) {
  24. this._gamepad = gamepad;
  25. }
  26. };
  27. GamepadCamera.prototype._checkInputs = function () {
  28. if (!this._gamepad) {
  29. return;
  30. }
  31. var LSValues = this._gamepad.leftStick;
  32. var normalizedLX = LSValues.x / this.moveSensibility;
  33. var normalizedLY = LSValues.y / this.moveSensibility;
  34. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  35. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  36. var RSValues = this._gamepad.rightStick;
  37. var normalizedRX = RSValues.x / this.angularSensibility;
  38. var normalizedRY = RSValues.y / this.angularSensibility;
  39. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  40. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  41. ;
  42. var cameraTransform = BABYLON.Matrix.RotationYawPitchRoll(this.rotation.y, this.rotation.x, 0);
  43. var deltaTransform = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(LSValues.x, 0, -LSValues.y), cameraTransform);
  44. this.cameraDirection = this.cameraDirection.add(deltaTransform);
  45. this.cameraRotation = this.cameraRotation.add(new BABYLON.Vector2(RSValues.y, RSValues.x));
  46. };
  47. GamepadCamera.prototype.dispose = function () {
  48. this._gamepads.dispose();
  49. _super.prototype.dispose.call(this);
  50. };
  51. return GamepadCamera;
  52. })(BABYLON.FreeCamera);
  53. BABYLON.GamepadCamera = GamepadCamera;
  54. })(BABYLON || (BABYLON = {}));
  55. //# sourceMappingURL=babylon.gamepadCamera.js.map