Capsule.js 3.4 KB

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