babylon.boundingSphere.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var BoundingSphere = (function () {
  4. function BoundingSphere(minimum, maximum) {
  5. this.minimum = minimum;
  6. this.maximum = maximum;
  7. this._tempRadiusVector = BABYLON.Vector3.Zero();
  8. var distance = BABYLON.Vector3.Distance(minimum, maximum);
  9. this.center = BABYLON.Vector3.Lerp(minimum, maximum, 0.5);
  10. ;
  11. this.radius = distance * 0.5;
  12. this.centerWorld = BABYLON.Vector3.Zero();
  13. this._update(BABYLON.Matrix.Identity());
  14. }
  15. // Methods
  16. BoundingSphere.prototype._update = function (world) {
  17. BABYLON.Vector3.TransformCoordinatesToRef(this.center, world, this.centerWorld);
  18. BABYLON.Vector3.TransformNormalFromFloatsToRef(1.0, 0, 0, world, this._tempRadiusVector);
  19. this.radiusWorld = this._tempRadiusVector.length() * this.radius;
  20. };
  21. BoundingSphere.prototype.isInFrustum = function (frustumPlanes) {
  22. for (var i = 0; i < 6; i++) {
  23. if (frustumPlanes[i].dotCoordinate(this.centerWorld) <= -this.radiusWorld)
  24. return false;
  25. }
  26. return true;
  27. };
  28. BoundingSphere.prototype.intersectsPoint = function (point) {
  29. var x = this.centerWorld.x - point.x;
  30. var y = this.centerWorld.y - point.y;
  31. var z = this.centerWorld.z - point.z;
  32. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  33. if (this.radiusWorld < distance)
  34. return false;
  35. return true;
  36. };
  37. // Statics
  38. BoundingSphere.Intersects = function (sphere0, sphere1) {
  39. var x = sphere0.centerWorld.x - sphere1.centerWorld.x;
  40. var y = sphere0.centerWorld.y - sphere1.centerWorld.y;
  41. var z = sphere0.centerWorld.z - sphere1.centerWorld.z;
  42. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  43. if (sphere0.radiusWorld + sphere1.radiusWorld < distance)
  44. return false;
  45. return true;
  46. };
  47. return BoundingSphere;
  48. })();
  49. BABYLON.BoundingSphere = BoundingSphere;
  50. })(BABYLON || (BABYLON = {}));
  51. //# sourceMappingURL=babylon.boundingSphere.js.map