Capsule.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {
  2. Vector3
  3. } from 'three';
  4. class Capsule {
  5. constructor( start = new Vector3( 0, 0, 0 ), end = new Vector3( 0, 1, 0 ), radius = 1 ) {
  6. this.start = start;
  7. this.end = end;
  8. this.radius = radius;
  9. }
  10. clone() {
  11. return new Capsule( this.start.clone(), this.end.clone(), this.radius );
  12. }
  13. set( start, end, radius ) {
  14. this.start.copy( start );
  15. this.end.copy( end );
  16. this.radius = radius;
  17. }
  18. copy( capsule ) {
  19. this.start.copy( capsule.start );
  20. this.end.copy( capsule.end );
  21. this.radius = capsule.radius;
  22. }
  23. getCenter( target ) {
  24. return target.copy( this.end ).add( this.start ).multiplyScalar( 0.5 );
  25. }
  26. translate( v ) {
  27. this.start.add( v );
  28. this.end.add( v );
  29. }
  30. checkAABBAxis( p1x, p1y, p2x, p2y, minx, maxx, miny, maxy, radius ) {
  31. return (
  32. ( minx - p1x < radius || minx - p2x < radius ) &&
  33. ( p1x - maxx < radius || p2x - maxx < radius ) &&
  34. ( miny - p1y < radius || miny - p2y < radius ) &&
  35. ( p1y - maxy < radius || p2y - maxy < radius )
  36. );
  37. }
  38. intersectsBox( box ) {
  39. return (
  40. this.checkAABBAxis(
  41. this.start.x, this.start.y, this.end.x, this.end.y,
  42. box.min.x, box.max.x, box.min.y, box.max.y,
  43. this.radius ) &&
  44. this.checkAABBAxis(
  45. this.start.x, this.start.z, this.end.x, this.end.z,
  46. box.min.x, box.max.x, box.min.z, box.max.z,
  47. this.radius ) &&
  48. this.checkAABBAxis(
  49. this.start.y, this.start.z, this.end.y, this.end.z,
  50. box.min.y, box.max.y, box.min.z, box.max.z,
  51. this.radius )
  52. );
  53. }
  54. }
  55. export { Capsule };