babylon.boundingSphere.js 1.9 KB

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