babylon.keyboardMoveController.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.KeyboardMoveController = function (scene, target) {
  5. BABYLON.InputController.call(this, scene, target);
  6. this._keys = [];
  7. this.keysUp = [38];
  8. this.keysDown = [40];
  9. this.keysLeft = [37];
  10. this.keysRight = [39];
  11. this._currentSpeed = new BABYLON.Vector3(0, 0, 0);
  12. this._lastFrameSpeed = new BABYLON.Vector3(0, 0, 0);
  13. this._currentAcceleration = new BABYLON.Vector3(0, 0, 0);
  14. this._tempSpeed = new BABYLON.Vector3(0, 0, 0);
  15. this._tempSpeed2 = new BABYLON.Vector3(0, 0, 0);
  16. this.maxAbsoluteSpeed = 2; // 2 meters per second
  17. this.maxAbsoluteAcceleration = 5; // 2 meters per second²
  18. this._targetSpeed = new BABYLON.Vector3(0, 0, 0);
  19. };
  20. BABYLON.KeyboardMoveController.prototype = Object.create(BABYLON.InputController.prototype);
  21. BABYLON.KeyboardMoveController.prototype.attachToCanvas = function (canvas) {
  22. var that = this;
  23. this._canvas = canvas;
  24. this._onKeyDown = function (evt) {
  25. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  26. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  27. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  28. that.keysRight.indexOf(evt.keyCode) !== -1) {
  29. var index = that._keys.indexOf(evt.keyCode);
  30. if (index === -1) {
  31. that._keys.push(evt.keyCode);
  32. }
  33. }
  34. };
  35. this._onKeyUp = function (evt) {
  36. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  37. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  38. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  39. that.keysRight.indexOf(evt.keyCode) !== -1) {
  40. var index = that._keys.indexOf(evt.keyCode);
  41. if (index >= 0) {
  42. that._keys.splice(index, 1);
  43. }
  44. }
  45. };
  46. this._onLostFocus = function () {
  47. that._keys = [];
  48. };
  49. window.addEventListener("keydown", this._onKeyDown, false);
  50. window.addEventListener("keyup", this._onKeyUp, false);
  51. window.addEventListener("blur", this._onLostFocus, false);
  52. };
  53. BABYLON.KeyboardMoveController.prototype.detachFromCanvas = function (canvas) {
  54. window.removeEventListener("keydown", this._onKeyDown, false);
  55. window.removeEventListener("keyup", this._onKeyUp, false);
  56. window.removeEventListener("blur", this._onLostFocus, false);
  57. };
  58. BABYLON.KeyboardMoveController.prototype.updateCurrentSpeed = function () {
  59. this._lastFrameSpeed.x = this._currentSpeed.x;
  60. this._lastFrameSpeed.y = this._currentSpeed.y;
  61. this._lastFrameSpeed.z = this._currentSpeed.z;
  62. if (this._currentSpeed.equals(this._targetSpeed)) {
  63. this._currentAcceleration.x = 0;
  64. this._currentAcceleration.y = 0;
  65. this._currentAcceleration.z = 0;
  66. return;
  67. }
  68. var dt = BABYLON.Tools.GetDeltaTime()/1000.0;
  69. var dv = this._tempSpeed;
  70. this._targetSpeed.subtractToRef(this._lastFrameSpeed, dv);
  71. var absoluteAccToTarget = dv.length() / dt;
  72. if (absoluteAccToTarget < this.maxAbsoluteAcceleration) {
  73. this._currentSpeed.x = this._targetSpeed.x;
  74. this._currentSpeed.y = this._targetSpeed.y;
  75. this._currentSpeed.z = this._targetSpeed.z;
  76. dv.normalize();
  77. dv.scaleToRef(absoluteAccToTarget, this._currentAcceleration);
  78. } else {
  79. dv.normalize();
  80. dv.scaleToRef(this.maxAbsoluteAcceleration, this._currentAcceleration);
  81. dv.scaleInPlace(this.maxAbsoluteAcceleration * dt);
  82. this._currentSpeed.addInPlace(dv);
  83. }
  84. };
  85. BABYLON.KeyboardMoveController.prototype.update = function () {
  86. this._targetSpeed.x = 0;
  87. this._targetSpeed.y = 0;
  88. this._targetSpeed.z = 0;
  89. // update target speed from input
  90. for (var index = 0; index < this._keys.length; index++) {
  91. var keyCode = this._keys[index];
  92. if (this.keysLeft.indexOf(keyCode) !== -1) {
  93. this._targetSpeed.x -= 1;
  94. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  95. this._targetSpeed.z += 1;
  96. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  97. this._targetSpeed.x += 1;
  98. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  99. this._targetSpeed.z -= 1;
  100. }
  101. }
  102. if (this._targetSpeed.x != 0 || this._targetSpeed.z != 0) {
  103. this._targetSpeed.normalize();
  104. this._targetSpeed.scaleInPlace(this.maxAbsoluteSpeed);
  105. }
  106. this.updateCurrentSpeed();
  107. if (this._lastFrameSpeed.x == 0 && this._lastFrameSpeed.z == 0 && this._currentAcceleration.x == 0 && this._currentAcceleration.z == 0) {
  108. return;
  109. }
  110. // dv = (dt * v0) + 1/2 * dt² * a
  111. var dt = BABYLON.Tools.GetDeltaTime() / 1000.0;
  112. this._lastFrameSpeed.scaleToRef(dt, this._tempSpeed);
  113. this._currentAcceleration.scaleToRef(dt * dt * 0.5, this._tempSpeed2);
  114. this._tempSpeed.addInPlace(this._tempSpeed2);
  115. if (this._tempSpeed.x != 0 || this._tempSpeed.z != 0) {
  116. this.target.moveRelative(this._tempSpeed);
  117. }
  118. };
  119. })();