babylon.followCamera.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. 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 radians = this.getRadians(this.rotationOffset) + cameraTarget.rotation.y;
  26. var targetX = cameraTarget.position.x + Math.sin(radians) * this.radius;
  27. var targetZ = cameraTarget.position.z + Math.cos(radians) * this.radius;
  28. var dx = targetX - this.position.x;
  29. var dy = (cameraTarget.position.y + this.heightOffset) - this.position.y;
  30. var dz = (targetZ) - this.position.z;
  31. var vx = dx * this.cameraAcceleration * 2; //this is set to .05
  32. var vy = dy * this.cameraAcceleration;
  33. var vz = dz * this.cameraAcceleration * 2;
  34. if (vx > this.maxCameraSpeed || vx < -this.maxCameraSpeed) {
  35. vx = vx < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  36. }
  37. if (vy > this.maxCameraSpeed || vy < -this.maxCameraSpeed) {
  38. vy = vy < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  39. }
  40. if (vz > this.maxCameraSpeed || vz < -this.maxCameraSpeed) {
  41. vz = vz < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  42. }
  43. this.position = new BABYLON.Vector3(this.position.x + vx, this.position.y + vy, this.position.z + vz);
  44. this.setTarget(cameraTarget.position);
  45. };
  46. FollowCamera.prototype._update = function () {
  47. _super.prototype._update.call(this);
  48. this.follow(this.target);
  49. };
  50. return FollowCamera;
  51. })(BABYLON.TargetCamera);
  52. BABYLON.FollowCamera = FollowCamera;
  53. })(BABYLON || (BABYLON = {}));
  54. //# sourceMappingURL=babylon.followCamera.js.map