babylon.boundingInfo.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module BABYLON {
  2. var computeBoxExtents = (axis: Vector3, box: BoundingBox) => {
  3. var p = Vector3.Dot(box.center, axis);
  4. var r0 = Math.abs(Vector3.Dot(box.directions[0], axis)) * box.extendSize.x;
  5. var r1 = Math.abs(Vector3.Dot(box.directions[1], axis)) * box.extendSize.y;
  6. var r2 = Math.abs(Vector3.Dot(box.directions[2], axis)) * box.extendSize.z;
  7. var r = r0 + r1 + r2;
  8. return {
  9. min: p - r,
  10. max: p + r
  11. };
  12. }
  13. var extentsOverlap = (min0: number, max0: number, min1: number, max1: number): boolean => !(min0 > max1 || min1 > max0);
  14. var axisOverlap = (axis: Vector3, box0: BoundingBox, box1: BoundingBox): boolean => {
  15. var result0 = computeBoxExtents(axis, box0);
  16. var result1 = computeBoxExtents(axis, box1);
  17. return extentsOverlap(result0.min, result0.max, result1.min, result1.max);
  18. }
  19. export class BoundingInfo {
  20. public boundingBox: BoundingBox;
  21. public boundingSphere: BoundingSphere;
  22. constructor(public minimum: Vector3, public maximum: Vector3) {
  23. this.boundingBox = new BABYLON.BoundingBox(minimum, maximum);
  24. this.boundingSphere = new BABYLON.BoundingSphere(minimum, maximum);
  25. }
  26. // Methods
  27. public _update(world: Matrix) {
  28. this.boundingBox._update(world);
  29. this.boundingSphere._update(world);
  30. }
  31. public isInFrustum(frustumPlanes: Plane[]): boolean {
  32. if (!this.boundingSphere.isInFrustum(frustumPlanes))
  33. return false;
  34. return this.boundingBox.isInFrustum(frustumPlanes);
  35. }
  36. public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
  37. return this.boundingBox.isCompletelyInFrustum(frustumPlanes);
  38. }
  39. public _checkCollision(collider: Collider): boolean {
  40. return collider._canDoCollision(this.boundingSphere.centerWorld, this.boundingSphere.radiusWorld, this.boundingBox.minimumWorld, this.boundingBox.maximumWorld);
  41. }
  42. public intersectsPoint(point: Vector3): boolean {
  43. if (!this.boundingSphere.centerWorld) {
  44. return false;
  45. }
  46. if (!this.boundingSphere.intersectsPoint(point)) {
  47. return false;
  48. }
  49. if (!this.boundingBox.intersectsPoint(point)) {
  50. return false;
  51. }
  52. return true;
  53. }
  54. public intersects(boundingInfo: BoundingInfo, precise: boolean): boolean {
  55. if (!this.boundingSphere.centerWorld || !boundingInfo.boundingSphere.centerWorld) {
  56. return false;
  57. }
  58. if (!BABYLON.BoundingSphere.Intersects(this.boundingSphere, boundingInfo.boundingSphere)) {
  59. return false;
  60. }
  61. if (!BABYLON.BoundingBox.Intersects(this.boundingBox, boundingInfo.boundingBox)) {
  62. return false;
  63. }
  64. if (!precise) {
  65. return true;
  66. }
  67. var box0 = this.boundingBox;
  68. var box1 = boundingInfo.boundingBox;
  69. if (!axisOverlap(box0.directions[0], box0, box1)) return false;
  70. if (!axisOverlap(box0.directions[1], box0, box1)) return false;
  71. if (!axisOverlap(box0.directions[2], box0, box1)) return false;
  72. if (!axisOverlap(box1.directions[0], box0, box1)) return false;
  73. if (!axisOverlap(box1.directions[1], box0, box1)) return false;
  74. if (!axisOverlap(box1.directions[2], box0, box1)) return false;
  75. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[0]), box0, box1)) return false;
  76. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[1]), box0, box1)) return false;
  77. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[2]), box0, box1)) return false;
  78. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[0]), box0, box1)) return false;
  79. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[1]), box0, box1)) return false;
  80. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[2]), box0, box1)) return false;
  81. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[0]), box0, box1)) return false;
  82. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[1]), box0, box1)) return false;
  83. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[2]), box0, box1)) return false;
  84. return true;
  85. }
  86. }
  87. }