1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::geometry::{Ray, RayIntersection, AABB};
use crate::math::{Isometry, Point, Rotation, Vector};
use approx::AbsDiffEq;
use na::Unit;
use ncollide::query::{algorithms::VoronoiSimplex, PointProjection, PointQuery, RayCast};
use ncollide::shape::{FeatureId, Segment, SupportMap};
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct Capsule {
pub segment: Segment<f32>,
pub radius: f32,
}
impl Capsule {
pub fn new_x(half_height: f32, radius: f32) -> Self {
let b = Point::from(Vector::x() * half_height);
Self::new(-b, b, radius)
}
pub fn new_y(half_height: f32, radius: f32) -> Self {
let b = Point::from(Vector::y() * half_height);
Self::new(-b, b, radius)
}
#[cfg(feature = "dim3")]
pub fn new_z(half_height: f32, radius: f32) -> Self {
let b = Point::from(Vector::z() * half_height);
Self::new(-b, b, radius)
}
pub fn new(a: Point<f32>, b: Point<f32>, radius: f32) -> Self {
let segment = Segment::new(a, b);
Self { segment, radius }
}
pub fn aabb(&self, pos: &Isometry<f32>) -> AABB {
let a = pos * self.segment.a;
let b = pos * self.segment.b;
let mins = a.coords.inf(&b.coords) - Vector::repeat(self.radius);
let maxs = a.coords.sup(&b.coords) + Vector::repeat(self.radius);
AABB::new(mins.into(), maxs.into())
}
pub fn height(&self) -> f32 {
(self.segment.b - self.segment.a).norm()
}
pub fn half_height(&self) -> f32 {
self.height() / 2.0
}
pub fn center(&self) -> Point<f32> {
na::center(&self.segment.a, &self.segment.b)
}
pub fn transform_by(&self, pos: &Isometry<f32>) -> Self {
Self::new(pos * self.segment.a, pos * self.segment.b, self.radius)
}
pub fn rotation_wrt_y(&self) -> Rotation<f32> {
let mut dir = self.segment.b - self.segment.a;
if dir.y < 0.0 {
dir = -dir;
}
#[cfg(feature = "dim2")]
{
Rotation::rotation_between(&Vector::y(), &dir)
}
#[cfg(feature = "dim3")]
{
Rotation::rotation_between(&Vector::y(), &dir).unwrap_or(Rotation::identity())
}
}
pub fn transform_wrt_y(&self) -> Isometry<f32> {
let rot = self.rotation_wrt_y();
Isometry::from_parts(self.center().coords.into(), rot)
}
}
impl SupportMap<f32> for Capsule {
fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32> {
let dir = Unit::try_new(*dir, 0.0).unwrap_or(Vector::y_axis());
self.local_support_point_toward(&dir)
}
fn local_support_point_toward(&self, dir: &Unit<Vector<f32>>) -> Point<f32> {
if dir.dot(&self.segment.a.coords) > dir.dot(&self.segment.b.coords) {
self.segment.a + **dir * self.radius
} else {
self.segment.b + **dir * self.radius
}
}
}
impl RayCast<f32> for Capsule {
fn toi_and_normal_with_ray(
&self,
m: &Isometry<f32>,
ray: &Ray,
max_toi: f32,
solid: bool,
) -> Option<RayIntersection> {
let ls_ray = ray.inverse_transform_by(m);
ncollide::query::ray_intersection_with_support_map_with_params(
&Isometry::identity(),
self,
&mut VoronoiSimplex::new(),
&ls_ray,
max_toi,
solid,
)
.map(|mut res| {
res.normal = m * res.normal;
res
})
}
}
impl PointQuery<f32> for Capsule {
#[inline]
fn project_point(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
) -> PointProjection<f32> {
let seg = Segment::new(self.segment.a, self.segment.b);
let proj = seg.project_point(m, pt, solid);
let dproj = *pt - proj.point;
if let Some((dir, dist)) = Unit::try_new_and_get(dproj, f32::default_epsilon()) {
let inside = dist <= self.radius;
if solid && inside {
return PointProjection::new(true, *pt);
} else {
return PointProjection::new(inside, proj.point + dir.into_inner() * self.radius);
}
} else if solid {
return PointProjection::new(true, *pt);
}
#[cfg(feature = "dim2")]
if let Some(dir) = seg.normal() {
let dir = m * *dir;
PointProjection::new(true, proj.point + dir * self.radius)
} else {
PointProjection::new(true, proj.point + Vector::ith(1, self.radius))
}
#[cfg(feature = "dim3")]
if let Some(dir) = seg.direction() {
use crate::utils::WBasis;
let dir = m * dir.orthonormal_basis()[0];
PointProjection::new(true, proj.point + dir * self.radius)
} else {
PointProjection::new(true, proj.point + Vector::ith(1, self.radius))
}
}
#[inline]
fn project_point_with_feature(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
) -> (PointProjection<f32>, FeatureId) {
(self.project_point(m, pt, false), FeatureId::Face(0))
}
}