babylon.boundingInfo.js 4.9 KB

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