Capsule.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {
  2. Vector3
  3. } from '../../../build/three.module.js';
  4. var Capsule = ( function () {
  5. var _v1 = new Vector3();
  6. var _v2 = new Vector3();
  7. var _v3 = new Vector3();
  8. var EPS = 1e-10;
  9. function Capsule( start, end, radius ) {
  10. this.start = start == undefined ? new Vector3( 0, 0, 0 ) : start;
  11. this.end = end == undefined ? new Vector3( 0, 1, 0 ) : end;
  12. this.radius = radius == undefined ? 1 : radius;
  13. }
  14. Object.assign( Capsule.prototype, {
  15. clone: function () {
  16. return new Capsule( this.start.clone(), this.end.clone(), this.radius );
  17. },
  18. set: function ( start, end, radius ) {
  19. this.start.copy( start );
  20. this.end.copy( end );
  21. this.radius = radius;
  22. },
  23. copy: function ( capsule ) {
  24. this.start.copy( capsule.start );
  25. this.end.copy( capsule.end );
  26. this.radius = capsule.radius;
  27. },
  28. getCenter: function ( target ) {
  29. return target.copy( this.end ).add( this.start ).multiplyScalar( 0.5 );
  30. },
  31. translate: function ( v ) {
  32. this.start.add( v );
  33. this.end.add( v );
  34. },
  35. checkAABBAxis: function ( p1x, p1y, p2x, p2y, minx, maxx, miny, maxy, radius ) {
  36. return (
  37. ( minx - p1x < radius || minx - p2x < radius ) &&
  38. ( p1x - maxx < radius || p2x - maxx < radius ) &&
  39. ( miny - p1y < radius || miny - p2y < radius ) &&
  40. ( p1y - maxy < radius || p2y - maxy < radius )
  41. );
  42. },
  43. intersectsBox: function ( box ) {
  44. return (
  45. this.checkAABBAxis(
  46. this.start.x, this.start.y, this.end.x, this.end.y,
  47. box.min.x, box.max.x, box.min.y, box.max.y,
  48. this.radius ) &&
  49. this.checkAABBAxis(
  50. this.start.x, this.start.z, this.end.x, this.end.z,
  51. box.min.x, box.max.x, box.min.z, box.max.z,
  52. this.radius ) &&
  53. this.checkAABBAxis(
  54. this.start.y, this.start.z, this.end.y, this.end.z,
  55. box.min.y, box.max.y, box.min.z, box.max.z,
  56. this.radius )
  57. );
  58. },
  59. lineLineMinimumPoints: function ( line1, line2 ) {
  60. var r = _v1.copy( line1.end ).sub( line1.start );
  61. var s = _v2.copy( line2.end ).sub( line2.start );
  62. var w = _v3.copy( line2.start ).sub( line1.start );
  63. var a = r.dot( s ),
  64. b = r.dot( r ),
  65. c = s.dot( s ),
  66. d = s.dot( w ),
  67. e = r.dot( w );
  68. var t1, t2, divisor = b * c - a * a;
  69. if ( Math.abs( divisor ) < EPS ) {
  70. var d1 = - d / c;
  71. var 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. var point1 = r.multiplyScalar( t1 ).add( line1.start );
  86. var point2 = s.multiplyScalar( t2 ).add( line2.start );
  87. return [ point1, point2 ];
  88. }
  89. } );
  90. return Capsule;
  91. } )();
  92. export { Capsule };