Box2.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. import {Vector2} from "./Vector2.js";
  3. /**
  4. * Box is described by a minimum and maximum points.
  5. *
  6. * Can be used for collision detection with points and other boxes.
  7. *
  8. * @class
  9. */
  10. function Box2(min, max)
  11. {
  12. this.min = (min !== undefined) ? min : new Vector2();
  13. this.max = (max !== undefined) ? max : new Vector2();
  14. }
  15. /**
  16. * Set the box values.
  17. */
  18. Box2.prototype.set = function(min, max)
  19. {
  20. this.min.copy(min);
  21. this.max.copy(max);
  22. return this;
  23. };
  24. /**
  25. * Set the box from a list of Vector2 points.
  26. */
  27. Box2.prototype.setFromPoints = function(points)
  28. {
  29. this.min = new Vector2(+Infinity, +Infinity);
  30. this.max = new Vector2(-Infinity, -Infinity);
  31. for(var i = 0, il = points.length; i < il; i++)
  32. {
  33. this.expandByPoint(points[i]);
  34. }
  35. return this;
  36. };
  37. /**
  38. * Set the box minimum and maximum from center point and size.
  39. */
  40. Box2.prototype.setFromCenterAndSize = function(center, size)
  41. {
  42. var v1 = new Vector2();
  43. var halfSize = v1.copy(size).multiplyScalar(0.5);
  44. this.min.copy(center).sub(halfSize);
  45. this.max.copy(center).add(halfSize);
  46. return this;
  47. };
  48. /**
  49. * Clone the box into a new object.
  50. */
  51. Box2.prototype.clone = function()
  52. {
  53. var box = new Box2();
  54. box.copy(this);
  55. return box;
  56. };
  57. /**
  58. * Copy the box value from another box.
  59. */
  60. Box2.prototype.copy = function(box)
  61. {
  62. this.min.copy(box.min);
  63. this.max.copy(box.max);
  64. };
  65. /**
  66. * Check if the box is empty (size equals zero or is negative).
  67. *
  68. * The box size is condireded valid on two negative axis.
  69. */
  70. Box2.prototype.isEmpty = function()
  71. {
  72. return (this.max.x < this.min.x) || (this.max.y < this.min.y);
  73. };
  74. /**
  75. * Calculate the center point of the box.
  76. */
  77. Box2.prototype.getCenter = function(target)
  78. {
  79. return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  80. };
  81. /**
  82. * Get the size of the box.
  83. */
  84. Box2.prototype.getSize = function(target)
  85. {
  86. return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  87. };
  88. /**
  89. * Expand the box to contain a new point.
  90. */
  91. Box2.prototype.expandByPoint = function(point)
  92. {
  93. this.min.min(point);
  94. this.max.max(point);
  95. return this;
  96. };
  97. /**
  98. * Expand the box by adding a border with the vector size.
  99. *
  100. * Vector is subtracted from min and added to the max points.
  101. */
  102. Box2.prototype.expandByVector = function(vector)
  103. {
  104. this.min.sub(vector);
  105. this.max.add(vector);
  106. };
  107. /**
  108. * Expand the box by adding a border with the scalar value.
  109. */
  110. Box2.prototype.expandByScalar = function(scalar)
  111. {
  112. this.min.addScalar(-scalar);
  113. this.max.addScalar(scalar);
  114. };
  115. /**
  116. * Check if the box contains a point inside.
  117. *
  118. * @param {Vector2} point
  119. * @return {boolean} True if the box contains point.
  120. */
  121. Box2.prototype.containsPoint = function(point)
  122. {
  123. return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
  124. };
  125. /**
  126. * Check if the box fully contains another box inside (different from intersects box).
  127. *
  128. * Only returns true if the box is fully contained.
  129. *
  130. * @param {Box2} box
  131. * @return {boolean} True if the box contains box.
  132. */
  133. Box2.prototype.containsBox = function(box)
  134. {
  135. 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;
  136. };
  137. /**
  138. * Check if two boxes intersect each other, using 4 splitting planes to rule out intersections.
  139. *
  140. * @param {Box2} box
  141. * @return {boolean} True if the boxes intersect each other.
  142. */
  143. Box2.prototype.intersectsBox = function(box)
  144. {
  145. 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;
  146. };
  147. Box2.prototype.clampPoint = function(point, target)
  148. {
  149. return target.copy(point).clamp(this.min, this.max);
  150. };
  151. /**
  152. * Calculate the distance to a point.
  153. *
  154. * @param {Vector2} point
  155. */
  156. Box2.prototype.distanceToPoint = function(point)
  157. {
  158. var v = new Vector2();
  159. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  160. return clampedPoint.sub(point).length();
  161. };
  162. /**
  163. * Make a intersection between this box and another box.
  164. *
  165. * Store the result in this object.
  166. *
  167. * @param {Box2} box
  168. */
  169. Box2.prototype.intersect = function(box)
  170. {
  171. this.min.max(box.min);
  172. this.max.min(box.max);
  173. };
  174. /**
  175. * Make a union between this box and another box.
  176. *
  177. * Store the result in this object.
  178. *
  179. * @param {Box2} box
  180. */
  181. Box2.prototype.union = function(box)
  182. {
  183. this.min.min(box.min);
  184. this.max.max(box.max);
  185. };
  186. /**
  187. * Translate the box by a offset value, adds the offset to booth min and max.
  188. *
  189. * @param {Vector2} offset
  190. */
  191. Box2.prototype.translate = function(offset)
  192. {
  193. this.min.add(offset);
  194. this.max.add(offset);
  195. };
  196. /**
  197. * Checks if two boxes are equal.
  198. *
  199. * @param {Box2} box
  200. * @return {boolean} True if the two boxes are equal.
  201. */
  202. Box2.prototype.equals = function(box)
  203. {
  204. return box.min.equals(this.min) && box.max.equals(this.max);
  205. };
  206. export {Box2};