Capsule.js 2.7 KB

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