babylon.boundingSphere.js 2.4 KB

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