babylon.boundingSphere.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. };
  11. // Methods
  12. BABYLON.BoundingSphere.prototype._update = function (world, scale) {
  13. BABYLON.Vector3.TransformCoordinatesToRef(this.center, world, this.centerWorld);
  14. this.radiusWorld = this.radius * scale;
  15. };
  16. BABYLON.BoundingSphere.prototype.isInFrustrum = function (frustumPlanes) {
  17. for (var i = 0; i < 6; i++) {
  18. if (frustumPlanes[i].dotCoordinate(this.centerWorld) <= -this.radiusWorld)
  19. return false;
  20. }
  21. return true;
  22. };
  23. BABYLON.BoundingSphere.prototype.intersectsPoint = function(point) {
  24. var x = this.centerWorld.x - point.x;
  25. var y = this.centerWorld.y - point.y;
  26. var z = this.centerWorld.z - point.z;
  27. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  28. if (this.radiusWorld < distance)
  29. return false;
  30. return true;
  31. };
  32. // Statics
  33. BABYLON.BoundingSphere.intersects = function (sphere0, sphere1) {
  34. var x = sphere0.centerWorld.x - sphere1.centerWorld.x;
  35. var y = sphere0.centerWorld.y - sphere1.centerWorld.y;
  36. var z = sphere0.centerWorld.z - sphere1.centerWorld.z;
  37. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  38. if (sphere0.radiusWorld + sphere1.radiusWorld < distance)
  39. return false;
  40. return true;
  41. };
  42. })();