babylon.boundingSphere.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. module BABYLON {
  2. export class BoundingSphere {
  3. public center: Vector3;
  4. public radius: number;
  5. public centerWorld: Vector3;
  6. public radiusWorld: number;
  7. private _tempRadiusVector = Vector3.Zero();
  8. constructor(public minimum: Vector3, public maximum: Vector3) {
  9. var distance = BABYLON.Vector3.Distance(minimum, maximum);
  10. this.center = BABYLON.Vector3.Lerp(minimum, maximum, 0.5);
  11. this.radius = distance * 0.5;
  12. this.centerWorld = BABYLON.Vector3.Zero();
  13. this._update(BABYLON.Matrix.Identity());
  14. }
  15. // Methods
  16. public _update(world: Matrix): void {
  17. BABYLON.Vector3.TransformCoordinatesToRef(this.center, world, this.centerWorld);
  18. BABYLON.Vector3.TransformNormalFromFloatsToRef(1.0, 1.0, 1.0, world, this._tempRadiusVector);
  19. this.radiusWorld = Math.max(Math.abs(this._tempRadiusVector.x), Math.abs(this._tempRadiusVector.y), Math.abs(this._tempRadiusVector.z)) * this.radius;
  20. }
  21. public isInFrustum(frustumPlanes: Plane[]): boolean {
  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. public intersectsPoint(point: Vector3): boolean {
  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 (Math.abs(this.radiusWorld - distance) < Engine.Epsilon)
  34. return false;
  35. return true;
  36. }
  37. // Statics
  38. public static Intersects(sphere0: BoundingSphere, sphere1: BoundingSphere): boolean {
  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. }
  48. }