Capsule.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {
  2. Vector3
  3. } from 'three';
  4. const _v1 = new Vector3();
  5. const _v2 = new Vector3();
  6. const _v3 = new Vector3();
  7. const EPS = 1e-10;
  8. class Capsule {
  9. constructor( start = new Vector3( 0, 0, 0 ), end = new Vector3( 0, 1, 0 ), radius = 1 ) {
  10. this.start = start;
  11. this.end = end;
  12. this.radius = radius;
  13. }
  14. clone() {
  15. return new Capsule( this.start.clone(), this.end.clone(), this.radius );
  16. }
  17. set( start, end, radius ) {
  18. this.start.copy( start );
  19. this.end.copy( end );
  20. this.radius = radius;
  21. }
  22. copy( capsule ) {
  23. this.start.copy( capsule.start );
  24. this.end.copy( capsule.end );
  25. this.radius = capsule.radius;
  26. }
  27. getCenter( target ) {
  28. return target.copy( this.end ).add( this.start ).multiplyScalar( 0.5 );
  29. }
  30. translate( v ) {
  31. this.start.add( v );
  32. this.end.add( v );
  33. }
  34. checkAABBAxis( p1x, p1y, p2x, p2y, minx, maxx, miny, maxy, radius ) {
  35. return (
  36. ( minx - p1x < radius || minx - p2x < radius ) &&
  37. ( p1x - maxx < radius || p2x - maxx < radius ) &&
  38. ( miny - p1y < radius || miny - p2y < radius ) &&
  39. ( p1y - maxy < radius || p2y - maxy < radius )
  40. );
  41. }
  42. intersectsBox( box ) {
  43. return (
  44. this.checkAABBAxis(
  45. this.start.x, this.start.y, this.end.x, this.end.y,
  46. box.min.x, box.max.x, box.min.y, box.max.y,
  47. this.radius ) &&
  48. this.checkAABBAxis(
  49. this.start.x, this.start.z, this.end.x, this.end.z,
  50. box.min.x, box.max.x, box.min.z, box.max.z,
  51. this.radius ) &&
  52. this.checkAABBAxis(
  53. this.start.y, this.start.z, this.end.y, this.end.z,
  54. box.min.y, box.max.y, box.min.z, box.max.z,
  55. this.radius )
  56. );
  57. }
  58. lineLineMinimumPoints( line1, line2 ) {
  59. const r = _v1.copy( line1.end ).sub( line1.start );
  60. const s = _v2.copy( line2.end ).sub( line2.start );
  61. const w = _v3.copy( line2.start ).sub( line1.start );
  62. const a = r.dot( s ),
  63. b = r.dot( r ),
  64. c = s.dot( s ),
  65. d = s.dot( w ),
  66. e = r.dot( w );
  67. let t1, t2;
  68. const divisor = b * c - a * a;
  69. if ( Math.abs( divisor ) < EPS ) {
  70. const d1 = - d / c;
  71. const d2 = ( a - d ) / c;
  72. if ( Math.abs( d1 - 0.5 ) < Math.abs( d2 - 0.5 ) ) {
  73. t1 = 0;
  74. t2 = d1;
  75. } else {
  76. t1 = 1;
  77. t2 = d2;
  78. }
  79. } else {
  80. t1 = ( d * a + e * c ) / divisor;
  81. t2 = ( t1 * a - d ) / c;
  82. }
  83. t2 = Math.max( 0, Math.min( 1, t2 ) );
  84. t1 = Math.max( 0, Math.min( 1, t1 ) );
  85. const point1 = r.multiplyScalar( t1 ).add( line1.start );
  86. const point2 = s.multiplyScalar( t2 ).add( line2.start );
  87. return [ point1, point2 ];
  88. }
  89. }
  90. export { Capsule };