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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::{ClosestPoint, LineSegment, Plane, Ray, Sphere, Triangle};
use mini_math::{NearlyEqual, Point, Vector3};
#[derive(PartialEq, Debug)]
pub struct Contact {
pub point: Point,
pub normal: Vector3,
pub overlap: f32,
}
impl NearlyEqual for &Contact {
fn nearly_equals(self, rhs: Self) -> bool {
self.point.nearly_equals(&rhs.point)
&& self.normal.nearly_equals(&rhs.normal)
&& self.overlap.nearly_equals(rhs.overlap)
}
}
impl Contact {
fn new(point: Point, normal: Vector3, overlap: f32) -> Self {
Self {
point,
normal,
overlap,
}
}
}
pub trait Collision<Rhs> {
fn collides(&self, rhs: &Rhs) -> Option<Contact>;
}
impl Collision<Sphere> for Sphere {
fn collides(&self, sphere: &Sphere) -> Option<Contact> {
let combined_radius = self.radius + sphere.radius;
let diff = self.center - sphere.center;
let distance_squared = diff.magnitude_squared();
if distance_squared > combined_radius * combined_radius {
None
} else {
let distance = distance_squared.sqrt();
let normal = diff / distance;
Some(Contact::new(
sphere.center + normal * sphere.radius,
normal,
combined_radius - distance,
))
}
}
}
impl Collision<Triangle> for Sphere {
fn collides(&self, triangle: &Triangle) -> Option<Contact> {
let plane = Plane::from(triangle);
let p = plane.closest_point(&self.center);
let distance_from_plane_squared = (p - self.center).magnitude_squared();
if distance_from_plane_squared > self.radius * self.radius {
None
} else {
let q = triangle.closest_point(&self.center);
let diff = q - self.center;
let overlap = self.radius - diff.magnitude();
if overlap < 0.0 {
None
} else {
Some(Contact::new(q, plane.normal, overlap))
}
}
}
}
impl Collision<Triangle> for Ray {
fn collides(&self, triangle: &Triangle) -> Option<Contact> {
let plane = Plane::from(triangle);
let n_dot_r = plane.normal.dot(self.direction);
if n_dot_r.abs() < std::f32::EPSILON {
return None;
}
let d = plane.normal.dot(Vector3::from(triangle.a));
let e = plane.normal.dot(Vector3::from(self.origin));
let t = (e + d) / n_dot_r;
if t > 0.0 {
return None;
}
let intersection_point = self.origin + self.direction * -t;
if triangle.coplanar_point_inside(intersection_point) {
Some(Contact::new(intersection_point, plane.normal, 0.0))
} else {
None
}
}
}
impl Collision<Triangle> for LineSegment {
fn collides(&self, triangle: &Triangle) -> Option<Contact> {
let plane = Plane::from(triangle);
let mut direction = self.end - self.start;
let length = direction.magnitude();
direction /= length;
let n_dot_r = plane.normal.dot(direction);
if n_dot_r.abs() < std::f32::EPSILON {
return None;
}
let d = plane.normal.dot(Vector3::from(triangle.a));
let e = plane.normal.dot(Vector3::from(self.start));
let t = (e + d) / n_dot_r;
if t > 0.0 || t < -length {
return None;
}
let intersection_point = self.start + direction * -t;
if triangle.coplanar_point_inside(intersection_point) {
Some(Contact::new(intersection_point, plane.normal, 0.0))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use mini_math::{Point, Vector3};
#[test]
fn test_sphere_sphere_collision() {
let a = Sphere::new(Point::zero(), 1.0);
let b = Sphere::new(Point::new(0.0, 1.5, 0.0), 1.0);
assert_eq!(
b.collides(&a),
Some(Contact::new(
Point::new(0.0, 1.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
0.5
))
);
}
#[test]
fn test_sphere_triangle_collision() {
let a = Triangle::new(
Point::new(-1.0, 0.0, -1.0),
Point::new(1.0, 0.0, -1.0),
Point::new(0.0, 0.0, 1.0),
);
let b = Sphere::new(Point::new(0.0, 0.75, 0.0), 1.0);
assert_eq!(
b.collides(&a),
Some(Contact::new(
Point::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
0.25
))
);
let b = Sphere::new(Point::new(0.0, 1.75, 0.0), 1.0);
assert_eq!(b.collides(&a), None);
let b = Sphere::new(Point::new(0.0, -1.75, 0.0), 1.0);
assert_eq!(b.collides(&a), None);
let b = Sphere::new(Point::new(-3.0, 0.0, -3.0), 1.0);
assert_eq!(b.collides(&a), None);
}
#[test]
fn test_triangle_ray_collision() {
let triangle = Triangle::new(
Point::new(-1.0, 0.0, 0.0),
Point::new(1.0, 0.0, 0.0),
Point::new(0.0, 0.0, 1.0),
);
let ray = Ray::new(Point::new(0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
assert_eq!(ray.collides(&triangle), None);
let ray = Ray::new(Point::new(0.0, 1.0, 0.0), Vector3::new(0.0, 1.0, 0.0));
assert_eq!(ray.collides(&triangle), None);
let ray = Ray::new(Point::new(0.0, -1.0, 0.0), Vector3::new(0.0, -1.0, 0.0));
assert_eq!(ray.collides(&triangle), None);
let ray = Ray::new(Point::new(3.0, 1.0, 3.0), Vector3::new(0.0, -1.0, 0.0));
assert_eq!(ray.collides(&triangle), None);
let ray = Ray::new(Point::new(0.0, 1.0, 0.0), Vector3::new(0.0, -1.0, 0.0));
assert_eq!(
ray.collides(&triangle),
Some(Contact::new(
Point::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
0.0
))
);
let ray = Ray::new(
Point::new(-0.5, -1.0, 0.0),
Vector3::new(0.5, 1.0, 0.0).normalized(),
);
assert_eq!(
ray.collides(&triangle),
Some(Contact::new(
Point::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
0.0
))
);
}
}