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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! Wedge (Triangular Prism) Collider
//!
//! Wedge collider with arbitrary orientation, supporting GJK/EPA collision detection.
//!
//! # Features
//!
//! - **Wedge**: Triangular prism with 6 vertices
//! - **GJK Support**: Implements `Support` trait by iterating all vertices
//! - **Fast AABB**: Compute world-space AABB from all vertices
//!
//! # Geometry
//!
//! The wedge is a triangular prism defined by width (X), height (Y), and
//! depth (Z) dimensions. In local space the triangular cross-section lies
//! in the XY plane:
//!
//! ```text
//! (0, +height/2) ---- apex
//! / \
//! (-width/2, -height/2) -- (+width/2, -height/2)
//! ```
//!
//! The prism extends from `-depth/2` to `+depth/2` along the Z axis.
//!
//! Author: Moroya Sakamoto
use crate::collider::{Support, AABB};
use crate::math::{Fix128, QuatFix, Vec3Fix};
/// Wedge (triangular prism) collider
///
/// A triangular prism defined by center, width, height, depth, and rotation.
/// The triangular cross-section is in the local XY plane, extruded along Z.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Wedge {
/// Center position in world space
pub center: Vec3Fix,
/// Full width along local X axis
pub width: Fix128,
/// Full height along local Y axis
pub height: Fix128,
/// Full depth along local Z axis (extrusion direction)
pub depth: Fix128,
/// Orientation quaternion
pub rotation: QuatFix,
}
impl Wedge {
/// Create a new axis-aligned wedge
#[inline]
#[must_use]
pub const fn new(center: Vec3Fix, width: Fix128, height: Fix128, depth: Fix128) -> Self {
Self {
center,
width,
height,
depth,
rotation: QuatFix::IDENTITY,
}
}
/// Create a new oriented wedge
#[inline]
#[must_use]
pub const fn with_rotation(
center: Vec3Fix,
width: Fix128,
height: Fix128,
depth: Fix128,
rotation: QuatFix,
) -> Self {
Self {
center,
width,
height,
depth,
rotation,
}
}
/// Get all 6 vertices of the wedge in world space
///
/// Local vertices (triangular cross-section extruded along Z):
/// - Front triangle (z = -depth/2):
/// v0 = (-width/2, -height/2, -depth/2)
/// v1 = (+width/2, -height/2, -depth/2)
/// v2 = (0, +height/2, -depth/2)
/// - Back triangle (z = +depth/2):
/// v3 = (-width/2, -height/2, +depth/2)
/// v4 = (+width/2, -height/2, +depth/2)
/// v5 = (0, +height/2, +depth/2)
#[must_use]
pub fn vertices(&self) -> [Vec3Fix; 6] {
let hw = self.width.half();
let hh = self.height.half();
let hd = self.depth.half();
let local_verts = [
Vec3Fix::new(-hw, -hh, -hd),
Vec3Fix::new(hw, -hh, -hd),
Vec3Fix::new(Fix128::ZERO, hh, -hd),
Vec3Fix::new(-hw, -hh, hd),
Vec3Fix::new(hw, -hh, hd),
Vec3Fix::new(Fix128::ZERO, hh, hd),
];
let mut result = [Vec3Fix::ZERO; 6];
for (i, lv) in local_verts.iter().enumerate() {
result[i] = self.center + self.rotation.rotate_vec(*lv);
}
result
}
/// Compute world-space AABB enclosing this wedge
#[must_use]
pub fn aabb(&self) -> AABB {
let verts = self.vertices();
let mut min = verts[0];
let mut max = verts[0];
for v in &verts[1..] {
if v.x < min.x {
min.x = v.x;
}
if v.y < min.y {
min.y = v.y;
}
if v.z < min.z {
min.z = v.z;
}
if v.x > max.x {
max.x = v.x;
}
if v.y > max.y {
max.y = v.y;
}
if v.z > max.z {
max.z = v.z;
}
}
AABB::new(min, max)
}
/// Volume of the wedge: (1/2) * width * height * depth
#[inline]
#[must_use]
pub fn volume(&self) -> Fix128 {
let two = Fix128::from_int(2);
self.width * self.height * self.depth / two
}
/// Compute inertia tensor (diagonal) for given mass
///
/// Approximation treating the wedge as a triangular prism.
/// For a uniform triangular prism:
/// - Ixx = m/18 * (h^2) + m/12 * d^2
/// - Iyy = m/18 * (w^2) + m/12 * d^2
/// - Izz = m/18 * (w^2 + h^2)
#[must_use]
pub fn inertia_diagonal(&self, mass: Fix128) -> Vec3Fix {
let eighteen = Fix128::from_int(18);
let twelve = Fix128::from_int(12);
let w2 = self.width * self.width;
let h2 = self.height * self.height;
let d2 = self.depth * self.depth;
Vec3Fix::new(
mass * h2 / eighteen + mass * d2 / twelve,
mass * w2 / eighteen + mass * d2 / twelve,
mass * (w2 + h2) / eighteen,
)
}
}
impl Support for Wedge {
fn support(&self, direction: Vec3Fix) -> Vec3Fix {
// For a convex polytope, the support is the vertex with the max
// dot product with the direction. Since vertices are already in
// world space from `vertices()`, we iterate and pick the best.
let verts = self.vertices();
let mut best = verts[0];
let mut best_dot = best.dot(direction);
for v in &verts[1..] {
let d = v.dot(direction);
if d > best_dot {
best = *v;
best_dot = d;
}
}
best
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wedge_new() {
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(4),
Fix128::from_int(3),
Fix128::from_int(2),
);
assert_eq!(w.width.hi, 4);
assert_eq!(w.height.hi, 3);
assert_eq!(w.depth.hi, 2);
}
#[test]
fn test_wedge_vertices_count() {
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(2),
Fix128::from_int(2),
Fix128::from_int(2),
);
let verts = w.vertices();
assert_eq!(verts.len(), 6);
}
#[test]
fn test_wedge_vertices_positions() {
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(4),
Fix128::from_int(6),
Fix128::from_int(2),
);
let verts = w.vertices();
// v0 = (-2, -3, -1)
assert_eq!(verts[0].x.hi, -2);
assert_eq!(verts[0].y.hi, -3);
assert_eq!(verts[0].z.hi, -1);
// v1 = (2, -3, -1)
assert_eq!(verts[1].x.hi, 2);
assert_eq!(verts[1].y.hi, -3);
// v2 = (0, 3, -1) — apex
assert_eq!(verts[2].x.hi, 0);
assert_eq!(verts[2].y.hi, 3);
}
#[test]
fn test_wedge_support_up() {
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(4),
Fix128::from_int(6),
Fix128::from_int(2),
);
let s = w.support(Vec3Fix::UNIT_Y);
// Max dot with +Y: apex vertices at y=3, so support y=3
assert_eq!(s.y.hi, 3);
}
#[test]
fn test_wedge_support_down() {
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(4),
Fix128::from_int(6),
Fix128::from_int(2),
);
let s = w.support(-Vec3Fix::UNIT_Y);
// Max dot with -Y: base vertices at y=-3
assert_eq!(s.y.hi, -3);
}
#[test]
fn test_wedge_aabb() {
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(4),
Fix128::from_int(6),
Fix128::from_int(2),
);
let aabb = w.aabb();
assert_eq!(aabb.min.x.hi, -2);
assert_eq!(aabb.max.x.hi, 2);
assert_eq!(aabb.min.y.hi, -3);
assert_eq!(aabb.max.y.hi, 3);
assert_eq!(aabb.min.z.hi, -1);
assert_eq!(aabb.max.z.hi, 1);
}
#[test]
fn test_wedge_volume() {
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(4),
Fix128::from_int(6),
Fix128::from_int(2),
);
// V = (1/2) * 4 * 6 * 2 = 24
assert_eq!(w.volume().hi, 24);
}
#[test]
fn test_wedge_gjk_collision() {
use crate::collider::{gjk, Sphere};
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(4),
Fix128::from_int(4),
Fix128::from_int(4),
);
let sphere = Sphere::new(Vec3Fix::ZERO, Fix128::ONE);
let result = gjk(&w, &sphere);
assert!(
result.colliding,
"Overlapping wedge and sphere should collide"
);
}
#[test]
fn test_wedge_gjk_no_collision() {
use crate::collider::{gjk, Sphere};
let w = Wedge::new(
Vec3Fix::ZERO,
Fix128::from_int(2),
Fix128::from_int(2),
Fix128::from_int(2),
);
let sphere = Sphere::new(Vec3Fix::from_int(20, 0, 0), Fix128::ONE);
let result = gjk(&w, &sphere);
assert!(
!result.colliding,
"Separated wedge and sphere should not collide"
);
}
#[test]
fn test_wedge_offset_center() {
let w = Wedge::new(
Vec3Fix::from_int(10, 20, 30),
Fix128::from_int(2),
Fix128::from_int(2),
Fix128::from_int(2),
);
let verts = w.vertices();
// v0 = (10-1, 20-1, 30-1) = (9, 19, 29)
assert_eq!(verts[0].x.hi, 9);
assert_eq!(verts[0].y.hi, 19);
assert_eq!(verts[0].z.hi, 29);
}
}