@@ -2,74 +2,138 @@ import {
22 Vector3
33} from 'three' ;
44
5+ /**
6+ * A capsule is essentially a cylinder with hemispherical caps at both ends.
7+ * It can be thought of as a swept sphere, where a sphere is moved along a line segment.
8+ *
9+ * Capsules are often used as bounding volumes (next to AABBs and bounding spheres).
10+ */
511class Capsule {
612
13+ /**
14+ * Constructs a new capsule.
15+ *
16+ * @param {Vector3 } [start] - The start vector.
17+ * @param {Vector3 } [end] - The end vector.
18+ * @param {number } [radius=1] - The capsule's radius.
19+ */
720 constructor ( start = new Vector3 ( 0 , 0 , 0 ) , end = new Vector3 ( 0 , 1 , 0 ) , radius = 1 ) {
821
22+ /**
23+ * The start vector.
24+ *
25+ * @type {Vector3 }
26+ */
927 this . start = start ;
28+
29+ /**
30+ * The end vector.
31+ *
32+ * @type {Vector3 }
33+ */
1034 this . end = end ;
35+
36+ /**
37+ * The capsule's radius.
38+ *
39+ * @type {number }
40+ * @default 1
41+ */
1142 this . radius = radius ;
1243
1344 }
1445
46+ /**
47+ * Returns a new capsule with copied values from this instance.
48+ *
49+ * @return {Capsule } A clone of this instance.
50+ */
1551 clone ( ) {
1652
17- return new Capsule ( this . start . clone ( ) , this . end . clone ( ) , this . radius ) ;
53+ return new this . constructor ( ) . copy ( this ) ;
1854
1955 }
2056
57+ /**
58+ * Sets the capsule components to the given values.
59+ * Please note that this method only copies the values from the given objects.
60+ *
61+ * @param {Vector3 } start - The start vector.
62+ * @param {Vector3 } end - The end vector
63+ * @param {number } radius - The capsule's radius.
64+ * @return {Box2 } A reference to this bounding box.
65+ */
2166 set ( start , end , radius ) {
2267
2368 this . start . copy ( start ) ;
2469 this . end . copy ( end ) ;
2570 this . radius = radius ;
2671
72+ return this ;
73+
2774 }
2875
76+ /**
77+ * Copies the values of the given capsule to this instance.
78+ *
79+ * @param {Capsule } capsule - The capsule to copy.
80+ * @return {Capsule } A reference to this capsule.
81+ */
2982 copy ( capsule ) {
3083
3184 this . start . copy ( capsule . start ) ;
3285 this . end . copy ( capsule . end ) ;
3386 this . radius = capsule . radius ;
3487
88+ return this ;
89+
3590 }
3691
92+ /**
93+ * Returns the center point of this capsule.
94+ *
95+ * @param {Vector3 } target - The target vector that is used to store the method's result.
96+ * @return {Vector3 } The center point.
97+ */
3798 getCenter ( target ) {
3899
39100 return target . copy ( this . end ) . add ( this . start ) . multiplyScalar ( 0.5 ) ;
40101
41102 }
42103
104+ /**
105+ * Adds the given offset to this capsule, effectively moving it in 3D space.
106+ *
107+ * @param {Vector3 } v - The offset that should be used to translate the capsule.
108+ * @return {Capsule } A reference to this capsule.
109+ */
43110 translate ( v ) {
44111
45112 this . start . add ( v ) ;
46113 this . end . add ( v ) ;
47114
48- }
49-
50- checkAABBAxis ( p1x , p1y , p2x , p2y , minx , maxx , miny , maxy , radius ) {
51-
52- return (
53- ( minx - p1x < radius || minx - p2x < radius ) &&
54- ( p1x - maxx < radius || p2x - maxx < radius ) &&
55- ( miny - p1y < radius || miny - p2y < radius ) &&
56- ( p1y - maxy < radius || p2y - maxy < radius )
57- ) ;
115+ return this ;
58116
59117 }
60118
119+ /**
120+ * Returns `true` if the given bounding box intersects with this capsule.
121+ *
122+ * @param {Box3 } box - The bounding box to test.
123+ * @return {boolean } Whether the given bounding box intersects with this capsule.
124+ */
61125 intersectsBox ( box ) {
62126
63127 return (
64- this . checkAABBAxis (
128+ checkAABBAxis (
65129 this . start . x , this . start . y , this . end . x , this . end . y ,
66130 box . min . x , box . max . x , box . min . y , box . max . y ,
67131 this . radius ) &&
68- this . checkAABBAxis (
132+ checkAABBAxis (
69133 this . start . x , this . start . z , this . end . x , this . end . z ,
70134 box . min . x , box . max . x , box . min . z , box . max . z ,
71135 this . radius ) &&
72- this . checkAABBAxis (
136+ checkAABBAxis (
73137 this . start . y , this . start . z , this . end . y , this . end . z ,
74138 box . min . y , box . max . y , box . min . z , box . max . z ,
75139 this . radius )
@@ -79,4 +143,15 @@ class Capsule {
79143
80144}
81145
146+ function checkAABBAxis ( p1x , p1y , p2x , p2y , minx , maxx , miny , maxy , radius ) {
147+
148+ return (
149+ ( minx - p1x < radius || minx - p2x < radius ) &&
150+ ( p1x - maxx < radius || p2x - maxx < radius ) &&
151+ ( miny - p1y < radius || miny - p2y < radius ) &&
152+ ( p1y - maxy < radius || p2y - maxy < radius )
153+ ) ;
154+
155+ }
156+
82157export { Capsule } ;
0 commit comments