Box2.js 5.5 KB

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