babylon.boundingInfo.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.minimum = minimum;
  25. this.maximum = maximum;
  26. this.boundingBox = new BABYLON.BoundingBox(minimum, maximum);
  27. this.boundingSphere = new BABYLON.BoundingSphere(minimum, maximum);
  28. }
  29. // Methods
  30. BoundingInfo.prototype._update = function (world) {
  31. this.boundingBox._update(world);
  32. this.boundingSphere._update(world);
  33. };
  34. BoundingInfo.prototype.isInFrustum = function (frustumPlanes) {
  35. if (!this.boundingSphere.isInFrustum(frustumPlanes))
  36. return false;
  37. return this.boundingBox.isInFrustum(frustumPlanes);
  38. };
  39. BoundingInfo.prototype._checkCollision = function (collider) {
  40. return collider._canDoCollision(this.boundingSphere.centerWorld, this.boundingSphere.radiusWorld, this.boundingBox.minimumWorld, this.boundingBox.maximumWorld);
  41. };
  42. BoundingInfo.prototype.intersectsPoint = function (point) {
  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. BoundingInfo.prototype.intersects = function (boundingInfo, precise) {
  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))
  70. return false;
  71. if (!axisOverlap(box0.directions[1], box0, box1))
  72. return false;
  73. if (!axisOverlap(box0.directions[2], box0, box1))
  74. return false;
  75. if (!axisOverlap(box1.directions[0], box0, box1))
  76. return false;
  77. if (!axisOverlap(box1.directions[1], box0, box1))
  78. return false;
  79. if (!axisOverlap(box1.directions[2], box0, box1))
  80. return false;
  81. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[0]), box0, box1))
  82. return false;
  83. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[1]), box0, box1))
  84. return false;
  85. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[2]), box0, box1))
  86. return false;
  87. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[0]), box0, box1))
  88. return false;
  89. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[1]), box0, box1))
  90. return false;
  91. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[2]), box0, box1))
  92. return false;
  93. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[0]), box0, box1))
  94. return false;
  95. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[1]), box0, box1))
  96. return false;
  97. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[2]), box0, box1))
  98. return false;
  99. return true;
  100. };
  101. return BoundingInfo;
  102. })();
  103. BABYLON.BoundingInfo = BoundingInfo;
  104. })(BABYLON || (BABYLON = {}));
  105. //# sourceMappingURL=babylon.boundingInfo.js.map