babylon.followCamera.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. var FollowCamera = (function (_super) {
  10. __extends(FollowCamera, _super);
  11. function FollowCamera(name, position, scene) {
  12. _super.call(this, name, position, scene);
  13. this.radius = 12;
  14. this.rotationOffset = 0;
  15. this.heightOffset = 4;
  16. this.cameraAcceleration = 0.05;
  17. this.maxCameraSpeed = 20;
  18. }
  19. FollowCamera.prototype.getRadians = function (degrees) {
  20. return degrees * Math.PI / 180;
  21. };
  22. FollowCamera.prototype.follow = function (cameraTarget) {
  23. if (!cameraTarget)
  24. return;
  25. var yRotation;
  26. if (cameraTarget.rotationQuaternion) {
  27. var rotMatrix = new BABYLON.Matrix();
  28. cameraTarget.rotationQuaternion.toRotationMatrix(rotMatrix);
  29. yRotation = Math.atan2(rotMatrix.m[8], rotMatrix.m[10]);
  30. }
  31. else {
  32. yRotation = cameraTarget.rotation.y;
  33. }
  34. var radians = this.getRadians(this.rotationOffset) + yRotation;
  35. var targetX = cameraTarget.position.x + Math.sin(radians) * this.radius;
  36. var targetZ = cameraTarget.position.z + Math.cos(radians) * this.radius;
  37. var dx = targetX - this.position.x;
  38. var dy = (cameraTarget.position.y + this.heightOffset) - this.position.y;
  39. var dz = (targetZ) - this.position.z;
  40. var vx = dx * this.cameraAcceleration * 2; //this is set to .05
  41. var vy = dy * this.cameraAcceleration;
  42. var vz = dz * this.cameraAcceleration * 2;
  43. if (vx > this.maxCameraSpeed || vx < -this.maxCameraSpeed) {
  44. vx = vx < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  45. }
  46. if (vy > this.maxCameraSpeed || vy < -this.maxCameraSpeed) {
  47. vy = vy < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  48. }
  49. if (vz > this.maxCameraSpeed || vz < -this.maxCameraSpeed) {
  50. vz = vz < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  51. }
  52. this.position = new BABYLON.Vector3(this.position.x + vx, this.position.y + vy, this.position.z + vz);
  53. this.setTarget(cameraTarget.position);
  54. };
  55. FollowCamera.prototype._checkInputs = function () {
  56. _super.prototype._checkInputs.call(this);
  57. this.follow(this.target);
  58. };
  59. return FollowCamera;
  60. })(BABYLON.TargetCamera);
  61. BABYLON.FollowCamera = FollowCamera;
  62. })(BABYLON || (BABYLON = {}));
  63. //# sourceMappingURL=babylon.followCamera.js.map