Box2.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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(center, size)
  31. {
  32. var v1 = new Vector2();
  33. var halfSize = v1.copy(size).multiplyScalar(0.5);
  34. this.min.copy(center).sub(halfSize);
  35. this.max.copy(center).add(halfSize);
  36. return this;
  37. },
  38. clone: function()
  39. {
  40. var box = new Box2();
  41. box.copy(this);
  42. return box;
  43. },
  44. copy: function(box)
  45. {
  46. this.min.copy(box.min);
  47. this.max.copy(box.max);
  48. return this;
  49. },
  50. isEmpty: function()
  51. {
  52. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  53. return (this.max.x < this.min.x) || (this.max.y < this.min.y);
  54. },
  55. getCenter: function(target)
  56. {
  57. return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  58. },
  59. getSize: function(target)
  60. {
  61. return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  62. },
  63. expandByPoint: function(point)
  64. {
  65. this.min.min(point);
  66. this.max.max(point);
  67. return this;
  68. },
  69. expandByVector: function(vector)
  70. {
  71. this.min.sub(vector);
  72. this.max.add(vector);
  73. return this;
  74. },
  75. expandByScalar: function(scalar)
  76. {
  77. this.min.addScalar(-scalar);
  78. this.max.addScalar(scalar);
  79. return this;
  80. },
  81. containsPoint: function(point)
  82. {
  83. return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
  84. },
  85. containsBox: function(box)
  86. {
  87. 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;
  88. },
  89. getParameter: function(point, target)
  90. {
  91. // This can potentially have a divide by zero if the box
  92. // has a size dimension of 0.
  93. return target.set(
  94. (point.x - this.min.x) / (this.max.x - this.min.x),
  95. (point.y - this.min.y) / (this.max.y - this.min.y)
  96. );
  97. },
  98. intersectsBox: function(box)
  99. {
  100. // using 4 splitting planes to rule out intersections
  101. 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;
  102. },
  103. clampPoint: function(point, target)
  104. {
  105. return target.copy(point).clamp(this.min, this.max);
  106. },
  107. distanceToPoint: function(point)
  108. {
  109. var v = new Vector2();
  110. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  111. return clampedPoint.sub(point).length();
  112. },
  113. intersect: function(box)
  114. {
  115. this.min.max(box.min);
  116. this.max.min(box.max);
  117. return this;
  118. },
  119. union: function(box)
  120. {
  121. this.min.min(box.min);
  122. this.max.max(box.max);
  123. return this;
  124. },
  125. translate: function(offset)
  126. {
  127. this.min.add(offset);
  128. this.max.add(offset);
  129. return this;
  130. },
  131. equals: function(box)
  132. {
  133. return box.min.equals(this.min) && box.max.equals(this.max);
  134. }
  135. });
  136. //export {Box2};