@@ -2,74 +2,138 @@ import {
2
2
Vector3
3
3
} from 'three' ;
4
4
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
+ */
5
11
class Capsule {
6
12
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
+ */
7
20
constructor ( start = new Vector3 ( 0 , 0 , 0 ) , end = new Vector3 ( 0 , 1 , 0 ) , radius = 1 ) {
8
21
22
+ /**
23
+ * The start vector.
24
+ *
25
+ * @type {Vector3 }
26
+ */
9
27
this . start = start ;
28
+
29
+ /**
30
+ * The end vector.
31
+ *
32
+ * @type {Vector3 }
33
+ */
10
34
this . end = end ;
35
+
36
+ /**
37
+ * The capsule's radius.
38
+ *
39
+ * @type {number }
40
+ * @default 1
41
+ */
11
42
this . radius = radius ;
12
43
13
44
}
14
45
46
+ /**
47
+ * Returns a new capsule with copied values from this instance.
48
+ *
49
+ * @return {Capsule } A clone of this instance.
50
+ */
15
51
clone ( ) {
16
52
17
- return new Capsule ( this . start . clone ( ) , this . end . clone ( ) , this . radius ) ;
53
+ return new this . constructor ( ) . copy ( this ) ;
18
54
19
55
}
20
56
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
+ */
21
66
set ( start , end , radius ) {
22
67
23
68
this . start . copy ( start ) ;
24
69
this . end . copy ( end ) ;
25
70
this . radius = radius ;
26
71
72
+ return this ;
73
+
27
74
}
28
75
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
+ */
29
82
copy ( capsule ) {
30
83
31
84
this . start . copy ( capsule . start ) ;
32
85
this . end . copy ( capsule . end ) ;
33
86
this . radius = capsule . radius ;
34
87
88
+ return this ;
89
+
35
90
}
36
91
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
+ */
37
98
getCenter ( target ) {
38
99
39
100
return target . copy ( this . end ) . add ( this . start ) . multiplyScalar ( 0.5 ) ;
40
101
41
102
}
42
103
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
+ */
43
110
translate ( v ) {
44
111
45
112
this . start . add ( v ) ;
46
113
this . end . add ( v ) ;
47
114
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 ;
58
116
59
117
}
60
118
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
+ */
61
125
intersectsBox ( box ) {
62
126
63
127
return (
64
- this . checkAABBAxis (
128
+ checkAABBAxis (
65
129
this . start . x , this . start . y , this . end . x , this . end . y ,
66
130
box . min . x , box . max . x , box . min . y , box . max . y ,
67
131
this . radius ) &&
68
- this . checkAABBAxis (
132
+ checkAABBAxis (
69
133
this . start . x , this . start . z , this . end . x , this . end . z ,
70
134
box . min . x , box . max . x , box . min . z , box . max . z ,
71
135
this . radius ) &&
72
- this . checkAABBAxis (
136
+ checkAABBAxis (
73
137
this . start . y , this . start . z , this . end . y , this . end . z ,
74
138
box . min . y , box . max . y , box . min . z , box . max . z ,
75
139
this . radius )
@@ -79,4 +143,15 @@ class Capsule {
79
143
80
144
}
81
145
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
+
82
157
export { Capsule } ;
0 commit comments