babylon.boundingSphere.js 1.8 KB

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