Box2.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. /**
  3. * Box is described by a minimum and maximum points.
  4. *
  5. * Can be used for collision detection with points and other boxes.
  6. */
  7. function Box2(min, max)
  8. {
  9. this.min = (min !== undefined) ? min : new Vector2();
  10. this.max = (max !== undefined) ? max : new Vector2();
  11. }
  12. Object.assign(Box2.prototype,
  13. {
  14. set: function(min, max)
  15. {
  16. this.min.copy(min);
  17. this.max.copy(max);
  18. return this;
  19. },
  20. setFromPoints: function(points)
  21. {
  22. this.min = new Vector2(+Infinity, +Infinity);
  23. this.max = new Vector2(-Infinity, -Infinity);
  24. for(var i = 0, il = points.length; i < il; i++)
  25. {
  26. this.expandByPoint(points[i]);
  27. }
  28. return this;
  29. },
  30. setFromCenterAndSize: function()
  31. {
  32. var v1 = new Vector2();
  33. return function setFromCenterAndSize(center, size)
  34. {
  35. var halfSize = v1.copy(size).multiplyScalar(0.5);
  36. this.min.copy(center).sub(halfSize);
  37. this.max.copy(center).add(halfSize);
  38. return this;
  39. };
  40. }(),
  41. clone: function()
  42. {
  43. var box = new Box2();
  44. box.copy(this);
  45. return box;
  46. },
  47. copy: function(box)
  48. {
  49. this.min.copy(box.min);
  50. this.max.copy(box.max);
  51. return this;
  52. },
  53. isEmpty: function()
  54. {
  55. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  56. return (this.max.x < this.min.x) || (this.max.y < this.min.y);
  57. },
  58. getCenter: function(target)
  59. {
  60. return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  61. },
  62. getSize: function(target)
  63. {
  64. return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  65. },
  66. expandByPoint: function(point)
  67. {
  68. this.min.min(point);
  69. this.max.max(point);
  70. return this;
  71. },
  72. expandByVector: function(vector)
  73. {
  74. this.min.sub(vector);
  75. this.max.add(vector);
  76. return this;
  77. },
  78. expandByScalar: function(scalar)
  79. {
  80. this.min.addScalar(-scalar);
  81. this.max.addScalar(scalar);
  82. return this;
  83. },
  84. containsPoint: function(point)
  85. {
  86. return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
  87. },
  88. containsBox: function(box)
  89. {
  90. return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y;
  91. },
  92. getParameter: function(point, target)
  93. {
  94. // This can potentially have a divide by zero if the box
  95. // has a size dimension of 0.
  96. return target.set(
  97. (point.x - this.min.x) / (this.max.x - this.min.x),
  98. (point.y - this.min.y) / (this.max.y - this.min.y)
  99. );
  100. },
  101. intersectsBox: function(box)
  102. {
  103. // using 4 splitting planes to rule out intersections
  104. return box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
  105. },
  106. clampPoint: function(point, target)
  107. {
  108. return target.copy(point).clamp(this.min, this.max);
  109. },
  110. distanceToPoint: function()
  111. {
  112. var v1 = new Vector2();
  113. return function distanceToPoint(point)
  114. {
  115. var clampedPoint = v1.copy(point).clamp(this.min, this.max);
  116. return clampedPoint.sub(point).length();
  117. };
  118. }(),
  119. intersect: function(box)
  120. {
  121. this.min.max(box.min);
  122. this.max.min(box.max);
  123. return this;
  124. },
  125. union: function(box)
  126. {
  127. this.min.min(box.min);
  128. this.max.max(box.max);
  129. return this;
  130. },
  131. translate: function(offset)
  132. {
  133. this.min.add(offset);
  134. this.max.add(offset);
  135. return this;
  136. },
  137. equals: function(box)
  138. {
  139. return box.min.equals(this.min) && box.max.equals(this.max);
  140. }
  141. });
  142. //export {Box2};