Capsule.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {
  2. Vector3
  3. } from 'three';
  4. /**
  5. * A capsule is essentially a cylinder with hemispherical caps at both ends.
  6. * It can be thought of as a swept sphere, where a sphere is moved along a line segment.
  7. *
  8. * Capsules are often used as bounding volumes (next to AABBs and bounding spheres).
  9. *
  10. * @three_import import { Capsule } from 'three/addons/math/Capsule.js';
  11. */
  12. class Capsule {
  13. /**
  14. * Constructs a new capsule.
  15. *
  16. * @param {Vector3} [start] - The start vector.
  17. * @param {Vector3} [end] - The end vector.
  18. * @param {number} [radius=1] - The capsule's radius.
  19. */
  20. constructor( start = new Vector3( 0, 0, 0 ), end = new Vector3( 0, 1, 0 ), radius = 1 ) {
  21. /**
  22. * The start vector.
  23. *
  24. * @type {Vector3}
  25. */
  26. this.start = start;
  27. /**
  28. * The end vector.
  29. *
  30. * @type {Vector3}
  31. */
  32. this.end = end;
  33. /**
  34. * The capsule's radius.
  35. *
  36. * @type {number}
  37. * @default 1
  38. */
  39. this.radius = radius;
  40. }
  41. /**
  42. * Returns a new capsule with copied values from this instance.
  43. *
  44. * @return {Capsule} A clone of this instance.
  45. */
  46. clone() {
  47. return new this.constructor().copy( this );
  48. }
  49. /**
  50. * Sets the capsule components to the given values.
  51. * Please note that this method only copies the values from the given objects.
  52. *
  53. * @param {Vector3} start - The start vector.
  54. * @param {Vector3} end - The end vector
  55. * @param {number} radius - The capsule's radius.
  56. * @return {Capsule} A reference to this capsule.
  57. */
  58. set( start, end, radius ) {
  59. this.start.copy( start );
  60. this.end.copy( end );
  61. this.radius = radius;
  62. return this;
  63. }
  64. /**
  65. * Copies the values of the given capsule to this instance.
  66. *
  67. * @param {Capsule} capsule - The capsule to copy.
  68. * @return {Capsule} A reference to this capsule.
  69. */
  70. copy( capsule ) {
  71. this.start.copy( capsule.start );
  72. this.end.copy( capsule.end );
  73. this.radius = capsule.radius;
  74. return this;
  75. }
  76. /**
  77. * Returns the center point of this capsule.
  78. *
  79. * @param {Vector3} target - The target vector that is used to store the method's result.
  80. * @return {Vector3} The center point.
  81. */
  82. getCenter( target ) {
  83. return target.copy( this.end ).add( this.start ).multiplyScalar( 0.5 );
  84. }
  85. /**
  86. * Adds the given offset to this capsule, effectively moving it in 3D space.
  87. *
  88. * @param {Vector3} v - The offset that should be used to translate the capsule.
  89. * @return {Capsule} A reference to this capsule.
  90. */
  91. translate( v ) {
  92. this.start.add( v );
  93. this.end.add( v );
  94. return this;
  95. }
  96. /**
  97. * Returns `true` if the given bounding box intersects with this capsule.
  98. *
  99. * @param {Box3} box - The bounding box to test.
  100. * @return {boolean} Whether the given bounding box intersects with this capsule.
  101. */
  102. intersectsBox( box ) {
  103. return (
  104. checkAABBAxis(
  105. this.start.x, this.start.y, this.end.x, this.end.y,
  106. box.min.x, box.max.x, box.min.y, box.max.y,
  107. this.radius ) &&
  108. checkAABBAxis(
  109. this.start.x, this.start.z, this.end.x, this.end.z,
  110. box.min.x, box.max.x, box.min.z, box.max.z,
  111. this.radius ) &&
  112. checkAABBAxis(
  113. this.start.y, this.start.z, this.end.y, this.end.z,
  114. box.min.y, box.max.y, box.min.z, box.max.z,
  115. this.radius )
  116. );
  117. }
  118. }
  119. function checkAABBAxis( p1x, p1y, p2x, p2y, minx, maxx, miny, maxy, radius ) {
  120. return (
  121. ( minx - p1x < radius || minx - p2x < radius ) &&
  122. ( p1x - maxx < radius || p2x - maxx < radius ) &&
  123. ( miny - p1y < radius || miny - p2y < radius ) &&
  124. ( p1y - maxy < radius || p2y - maxy < radius )
  125. );
  126. }
  127. export { Capsule };