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
//! The first collider a capsule meets along a constant-acceleration path, and
//! when: [`PhysicsWorld::sweep_capsule_arc`].
//!
//! A straight sweep answers a share of a segment, and its first hit is the
//! first along that segment. A caller moving a body under gravity or braking
//! in the air wants the time on the curved path instead — EW's airborne
//! controller bisected forecasts of straight sweeps to recover it, and had to
//! widen its probes by the chord's error to see a wall the path reaches and
//! turns back from. This sweeps the path itself, by the shape-level search in
//! [`crate::query`]'s arc module, through the same broadphase, filter and
//! hit order as the closest straight sweeps.
use glam::DVec3;
use crate::collider::Aabb;
use crate::query::{
AcceleratedPath, ArcHit, arc_capsule_vs_box, arc_capsule_vs_sphere,
arc_capsule_vs_turned_capsule,
};
use super::entry::Primitive;
use super::{
ColliderId, OverlapQueries, PhysicsWorld, QueryFilter, QueryScratch, ResolvedFilter, SweptHit,
keep_closest, swept_hits_core,
};
impl PhysicsWorld {
/// The first solid collider a Y-aligned capsule of `radius` and
/// `half_height` touches while its centre follows `path`, and when.
///
/// The closest straight sweep's rules, on a curved path: triggers are
/// skipped, `filter`'s mask and excluded collider are left out in the
/// narrow phase — a character's own registered capsule is excluded here,
/// not found inside itself — and of two colliders met at the same time
/// the one in the lower [`ColliderId::index`] wins, then the lower part
/// of a compound. A triangle mesh is one collider, answered by the first
/// of its triangles touched, from either side.
///
/// # Time, not a share
///
/// [`ArcHit::time`] is a time in `[0, path.duration]`, in the path's own
/// units: what a caller integrating velocity and acceleration over the
/// same interval needs to split it at the contact. It is never later than
/// the first touch; how close to it the search stops is
/// [`ARC_TIME_TOLERANCE`](crate::ARC_TIME_TOLERANCE)'s, and how many
/// steps it may take [`ARC_MAX_ITERATIONS`](crate::ARC_MAX_ITERATIONS)'s.
/// The search itself is conservative advancement against the plane that
/// bounds each shape, described in [`crate::query`]'s arc module.
/// A path that reaches a surface and turns back from it before its end is
/// a contact, though the chord between its ends may miss the surface
/// entirely.
///
/// # Against the straight sweep
///
/// With no acceleration the path is a segment, and the arc meets a box's
/// face, a sphere, a capsule and a mesh where and when
/// [`sweep_capsule_filtered`](Self::sweep_capsule_filtered) along that
/// segment does. An unturned box's edge or corner is the exception: the
/// straight sweep grows the box by the radius with square corners, and
/// meets that corner before the round capsule reaches the box, while
/// this measures the round capsule. A turned box or capsule is swept
/// straight by a conservative advancement that stops a little short.
///
/// # Starting overlap
///
/// A collider the capsule begins touching or inside is met at time zero,
/// with [`ArcHit::started_inside`] set, whichever way the path heads —
/// the answer [`sweep_capsule_filtered`](Self::sweep_capsule_filtered)
/// gives — and so it comes first. A caller that keeps its capsule clear
/// of the surfaces it slides along never sees one; one that wants what
/// lies past a collider it starts in excludes that collider.
///
/// # What it does not cover
///
/// The colliders are where they are now: geometry that moves during the
/// interval is not swept. The acceleration is constant over the whole
/// interval; a caller whose acceleration changes partway splits the
/// interval there. Its validation covers flat walls, ceilings and floors,
/// round shapes, and EW's planar braking case; tilted and curved walls,
/// moving geometry and projectile flight are not yet validated against
/// game fixtures.
pub fn sweep_capsule_arc(
&mut self,
path: &AcceleratedPath,
radius: f64,
half_height: f64,
filter: QueryFilter,
) -> Option<(ColliderId, ArcHit)> {
self.sweep_capsule_arc_where(path, radius, half_height, filter, |_, _| true)
}
/// The earliest arc contact accepted by `accept`, with the same filters,
/// times and tie breaking as [`Self::sweep_capsule_arc`].
///
/// Each compound part and each mesh triangle is considered independently:
/// rejecting a departing floor does not hide a ceiling or wall in the same
/// collider. Starting contacts are offered too, so an incoming overlap can
/// be accepted. The predicate sees world-space points and normals and times
/// measured from the original path start. It must not depend on visit order.
///
/// After rejection, the search continues after separation from that
/// primitive, so a departing support can be met again on the way back.
/// Separations shorter than [`crate::ARC_TIME_TOLERANCE`] may be treated
/// as continuous contact, as may gaps within the rejected contact's
/// conservative distance and coordinate roundoff. Acceptance is evaluated
/// at contact entries, not continuously while overlapping.
pub fn sweep_capsule_arc_where(
&mut self,
path: &AcceleratedPath,
radius: f64,
half_height: f64,
filter: QueryFilter,
accept: impl Fn(ColliderId, &ArcHit) -> bool,
) -> Option<(ColliderId, ArcHit)> {
let mut scratch = core::mem::take(&mut self.scratch);
let hit = self.overlap_queries().sweep_capsule_arc_where(
path,
radius,
half_height,
filter,
&mut scratch,
accept,
);
self.scratch = scratch;
hit
}
}
impl OverlapQueries<'_> {
/// [`PhysicsWorld::sweep_capsule_arc`] under a shared borrow, working in
/// `scratch` instead of the world's own buffers. The two forms are one
/// implementation.
#[must_use]
pub fn sweep_capsule_arc(
&self,
path: &AcceleratedPath,
radius: f64,
half_height: f64,
filter: QueryFilter,
scratch: &mut QueryScratch,
) -> Option<(ColliderId, ArcHit)> {
self.sweep_capsule_arc_where(path, radius, half_height, filter, scratch, |_, _| true)
}
/// [`PhysicsWorld::sweep_capsule_arc_where`] under a shared borrow, using
/// the caller's scratch buffers. No collider or broadphase state is changed.
#[must_use]
pub fn sweep_capsule_arc_where(
&self,
path: &AcceleratedPath,
radius: f64,
half_height: f64,
filter: QueryFilter,
scratch: &mut QueryScratch,
accept: impl Fn(ColliderId, &ArcHit) -> bool,
) -> Option<(ColliderId, ArcHit)> {
let filter = ResolvedFilter::solid(self.colliders, self.generations, filter);
// Everything within the capsule's reach of the box round the whole
// path, its turning points included, and not only round its ends.
let reach = DVec3::new(radius, radius + half_height, radius);
let bounds = path.bounds();
filter.candidates(
self.bvh,
&Aabb::new(bounds.min - reach, bounds.max + reach),
scratch,
);
let mut closest = None;
swept_hits_core(
self.colliders,
self.generations,
&scratch.candidates,
filter,
|id, part, shape| {
let accepted = |hit: &ArcHit| accept(id, &hit.with_part(part));
match shape {
Primitive::Sphere(s) => {
arc_capsule_vs_sphere(path, radius, half_height, s, accepted)
}
Primitive::Box(b) => arc_capsule_vs_box(path, radius, half_height, b, accepted),
Primitive::Capsule(c) => {
arc_capsule_vs_turned_capsule(path, radius, half_height, c, accepted)
}
Primitive::Mesh(m) => m.sweep_arc_where(
path,
radius,
DVec3::Y * half_height,
&mut scratch.mesh,
accepted,
),
}
},
|id, hit| keep_closest(&mut closest, id, hit),
);
closest
}
}
impl SweptHit for ArcHit {
fn t(&self) -> f64 {
self.time
}
fn part_index(&self) -> usize {
self.part
}
fn with_part(self, part: usize) -> Self {
Self { part, ..self }
}
}