crcbl_phys/character/lying.rs
1//! Moving a prone body, [`CharacterController::move_lying`] (and
2//! [`CharacterController::move_lying_into`], which records its slide), and
3//! turning one, [`CharacterController::turn_lying`].
4//!
5//! A lying body is a [`LyingCapsule`] whose head is the controller's
6//! position. It moves the way the upright capsule does — the same plane-set
7//! slide, the same skin width, the same slope limit — with two differences,
8//! each forced by the shape:
9//!
10//! * **The whole body is swept**, not the sphere at its head, so the legs stop
11//! at a wall behind or beside the player as the head stops at one ahead. The
12//! sweep turns nothing: the yaw and pitch are what the caller and the last
13//! settle left.
14//! * **It settles along its length.** Where the upright capsule probes once
15//! under its centre, a lying one probes under each end of its core and lies
16//! on the line between the two resting points, pitched to follow the ground
17//! and clamped to the walkable slope; see
18//! [`move_lying`](CharacterController::move_lying) for what happens where
19//! that line would cut through an edge.
20//!
21//! A lying body does not step up, and it is not dug out of what it starts
22//! inside: there is no penetration depth for a lying capsule to push it out
23//! by.
24//!
25//! A turn is not a move. It keeps the head where it is and swings the feet
26//! end about it, stopping where the body would first be inside something —
27//! you cannot turn prone into a wall — and leaves the settle to the next move.
28
29use std::f64::consts::{PI, TAU};
30
31use glam::DVec3;
32
33use super::{Body, CharacterController, GroundProbe, MIN_MOVE, SlideContact, UP, WorldReader};
34use crate::broadphase::Segment;
35use crate::collider::LyingCapsule;
36use crate::world::{ColliderId, PhysicsWorld};
37
38/// The most poses a turn tries — a settle's toward its resting line or a
39/// [`turn_lying`](CharacterController::turn_lying) — each moving an end at
40/// most a radius, before it gives up stepping and bisects what is left.
41const MAX_TURN_STEPS: u32 = 64;
42
43/// The most halvings a turn spends finding where a turning body first
44/// touches something. Each halves the gap left, so this many take any turn
45/// below a floating-point step.
46const MAX_BISECTIONS: u32 = 48;
47
48/// What one [`CharacterController::move_lying`] did.
49#[derive(Debug, Clone, Copy, PartialEq)]
50pub struct LyingMoveOutcome {
51 /// The body where the move left it: the head at the controller's new
52 /// [`position`](CharacterController::position), the yaw it was given, and
53 /// the pitch the ground gave it. **Pass this to the next move**, or to
54 /// [`turn_lying`](CharacterController::turn_lying) first to turn it.
55 pub body: LyingCapsule,
56 /// How far the head moved, the settle included: the requested
57 /// displacement minus whatever the world took away, as
58 /// [`MoveOutcome::motion`](super::MoveOutcome::motion) is.
59 pub motion: DVec3,
60 /// Whether the body lies on walkable ground now. Agrees with
61 /// [`CharacterController::is_grounded`].
62 pub grounded: bool,
63 /// Whether the move was blocked by a surface too steep to stand on and not
64 /// steep enough to be a ceiling.
65 pub hit_wall: bool,
66 /// Whether the move was blocked by a surface facing downward.
67 pub hit_ceiling: bool,
68 /// How many times the move was blocked and redirected. Zero means it went
69 /// the whole way unobstructed.
70 pub slides: u32,
71}
72
73/// What one [`CharacterController::turn_lying`] did.
74#[derive(Debug, Clone, Copy, PartialEq)]
75pub struct LyingTurnOutcome {
76 /// The body turned as far as it could: the head exactly where it was, the
77 /// yaw reached — the one asked for, as given, when nothing stopped it —
78 /// and the pitch the ground under the head gives that yaw. **Pass this to
79 /// the next move**, which settles it.
80 pub body: LyingCapsule,
81 /// How far it turned, in radians, the way the yaw turns: positive is a
82 /// right-handed turn about `+Y`, to the left seen from above. Never more
83 /// than a half turn either way.
84 pub turned: f64,
85 /// The share of the turn asked for that it made, in `[0, 1]`: one when
86 /// nothing stopped it, including a turn of nothing.
87 pub fraction: f64,
88 /// The collider that stopped the turn, or `None` if it went the whole
89 /// way: the one the nearest blocked pose the turn tried is inside, or,
90 /// where that pose is inside several, the one
91 /// [`lying_blocker`](CharacterController::lying_blocker) names.
92 pub blocker: Option<ColliderId>,
93}
94
95/// How far a stepped turn got.
96#[derive(Debug, Clone, Copy)]
97struct TurnStop {
98 /// The last parameter whose pose was clear, or where the turn started if
99 /// none was.
100 reached: f64,
101 /// What the turn stopped short of, or `None` if it went the whole way.
102 blocker: Option<ColliderId>,
103}
104
105/// Which end of a body stays where it is while the body turns toward its
106/// resting line.
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108enum Pivot {
109 /// The head keeps its height and the feet swing: turning from the feet
110 /// raised as far as the slope limit allows, down.
111 Head,
112 /// The feet end keeps its height and the head swings: turning from the
113 /// head raised as far as the slope limit allows, down.
114 Feet,
115}
116
117impl CharacterController {
118 /// Move a body lying as `body` by a **world-space** displacement of its
119 /// head, sliding along whatever gets in the way of any part of it, and
120 /// settle it on the ground along its length.
121 ///
122 /// The move starts from `body`: its head becomes this controller's
123 /// [`position`](Self::position) — a head somewhere else is a teleport, and
124 /// forgets the ground as [`set_position`](Self::set_position) does — and
125 /// its yaw, pitch, radius and length are the shape swept. The controller's
126 /// own [`config`](Self::config) supplies the rest: the skin width, the
127 /// slope limit, the slide budget and the snap distance, but not the shape.
128 /// The [self collider](Self::with_self_collider) and the
129 /// [query mask](Self::with_query_mask) apply as they do to every other
130 /// query, and the self collider is written back as this controller's own
131 /// [`capsule`](Self::capsule) at the head, as a walking move writes it.
132 ///
133 /// # The move
134 ///
135 /// `motion` is a displacement and not a velocity, and a grounded move
136 /// replaces its vertical part with the rise of the ground under the body,
137 /// exactly as [`move_and_slide`](Self::move_and_slide) does. The whole
138 /// capsule is swept along it, and each surface it meets is slid along with
139 /// the same plane-set clip. **The body does not turn while it moves**: a
140 /// change of yaw is [`turn_lying`](Self::turn_lying)'s, made beforehand,
141 /// and a change of pitch is the settle's.
142 ///
143 /// A lying body does not step up. A riser low enough that the round end
144 /// meets its edge on a walkable slope — below about
145 /// `radius · (1 - min_ground_normal_y)` plus a skin width — is slid up and
146 /// over, as a ramp is. A taller one is a wall, and stays one: a grounded
147 /// body meets a wall as though it were upright, as Unreal's
148 /// `SlideAlongSurface` does, so the edge's upward lean does not lift it,
149 /// and the settle never lifts an end onto what it cannot stand on.
150 ///
151 /// # The settle
152 ///
153 /// A sphere of the body's radius is swept straight down under each end of
154 /// the core, and each end's **resting point** is where that sphere would
155 /// sit a [`skin_width`](super::CharacterConfig::skin_width) above what it
156 /// met. The body settles only if one end is within the walking move's own
157 /// ground probe of what is under it; otherwise it is in the air, and falls
158 /// as the caller moves it. Each end looks further than that, as far as the
159 /// slope limit could swing it down with the other end held, so a body lying
160 /// level along a slope finds the ground under its downhill end.
161 ///
162 /// The body lies on the line through the two resting points: its pitch is
163 /// that line's, **clamped to the steepest walkable slope** — the pitch
164 /// sine never exceeds `sqrt(1 - min_ground_normal_y²)` either way — and it
165 /// is placed as low as it can go while neither end sinks below its
166 /// resting point. On even ground within the limit both ends rest; on
167 /// ground steeper than it, the uphill end rests and the downhill end is
168 /// held off the slope. An end with nothing under it as far as it looks
169 /// swings down to the limit, the body resting on the other end — or, where
170 /// that swings it into the ground, turning onto it as an edge is met
171 /// below.
172 ///
173 /// **A line that would cut through an edge** — a curb the head has gone
174 /// over, the crest of a hill, a ridge under the middle — is refused. The
175 /// body turns toward it instead about one end, from that end's other end
176 /// raised as far as the slope limit allows down to where it first touches
177 /// what is in the way; about the head and about the feet are both tried,
178 /// and the one that turns less wins, the head on a tie. Crawling head
179 /// first off a curb, the body therefore rides level with its head out over
180 /// the drop, then tips head down once resting on the head is the smaller
181 /// turn. If neither turn has a clear pose to start from, the body is left
182 /// as the slide left it.
183 ///
184 /// The ground [`ground`](Self::ground) reports is the head's, or the feet
185 /// end's where the head's is not walkable. The body is grounded when
186 /// either is.
187 pub fn move_lying(
188 &mut self,
189 world: &mut PhysicsWorld,
190 body: &LyingCapsule,
191 motion: DVec3,
192 ) -> LyingMoveOutcome {
193 self.move_prone(world, body, motion, None)
194 }
195
196 /// [`move_lying`](Self::move_lying), writing every sweep of the slide
197 /// that met something into `contacts` (which is cleared first), in the
198 /// order the slide met them, as
199 /// [`move_and_slide_into`](Self::move_and_slide_into) does for the
200 /// upright move.
201 ///
202 /// The move itself is the same one, to the bit: recording reads what the
203 /// slide found and never feeds back into it. Only the slide is recorded,
204 /// not the settle: the settle's probes under each end and its turn onto an
205 /// edge are not sweeps the body was blocked on, and what they found is in
206 /// the [`body`](LyingMoveOutcome::body) and [`ground`](Self::ground) the
207 /// move leaves. See [`SlideContact`] for what each entry holds; a grounded
208 /// body records a wall's normal as the sweep met it, leaning, though it
209 /// slid along the wall made upright.
210 pub fn move_lying_into(
211 &mut self,
212 world: &mut PhysicsWorld,
213 body: &LyingCapsule,
214 motion: DVec3,
215 contacts: &mut Vec<SlideContact>,
216 ) -> LyingMoveOutcome {
217 contacts.clear();
218 self.move_prone(world, body, motion, Some(contacts))
219 }
220
221 /// The one lying move, recording into `contacts` when there are any.
222 fn move_prone(
223 &mut self,
224 world: &mut PhysicsWorld,
225 body: &LyingCapsule,
226 motion: DVec3,
227 contacts: Option<&mut Vec<SlideContact>>,
228 ) -> LyingMoveOutcome {
229 if body.head != self.position {
230 self.set_position(body.head);
231 }
232 let start = self.position;
233 let was_grounded = self.ground.is_some();
234
235 let motion = self.ground_adjusted(motion, was_grounded);
236 let report = self.slide(
237 &mut WorldReader::of(world),
238 motion,
239 was_grounded,
240 Body::Lying(*body),
241 contacts,
242 );
243 let slid = LyingCapsule {
244 head: self.position,
245 ..*body
246 };
247 let settled = self.settle_lying(world, slid, self.settle_reach(was_grounded, motion));
248 self.position = settled.head;
249
250 if let Some(collider) = self.self_collider {
251 world.set_capsule(collider, self.capsule());
252 }
253
254 LyingMoveOutcome {
255 body: settled,
256 motion: self.position - start,
257 grounded: self.ground.is_some(),
258 hit_wall: report.hit_wall,
259 hit_ceiling: report.hit_ceiling,
260 slides: report.slides,
261 }
262 }
263
264 /// Turn a body lying as `body` about its head toward `yaw`, stopping at
265 /// the first yaw where it would be inside something.
266 ///
267 /// A prone body cannot turn into a wall. **The turn pivots on the head**,
268 /// where a game's actor origin and first-person camera sit, so a wall
269 /// behind or beside the player limits how far the legs swing and never
270 /// moves the view: the head of the [`body`](LyingTurnOutcome::body)
271 /// returned is exactly `body`'s. The turn is a query, as
272 /// [`lying_blocker`](Self::lying_blocker) is: it moves nothing and records
273 /// nothing, and it sees what that sees, under this controller's
274 /// [self collider](Self::with_self_collider) and
275 /// [query mask](Self::with_query_mask). A pose touching something is
276 /// clear; only a penetration stops the turn.
277 ///
278 /// # Which way, and how far
279 ///
280 /// The turn goes **the shorter way round**, across the `±π` wrap where
281 /// that is shorter — from `3.0` to `-3.0` is a turn of about `+0.28` — and
282 /// a turn of exactly half a circle goes the positive way, which is to the
283 /// left. It is stepped so the feet end moves at most a radius a step, so
284 /// nothing thinner than the body is stepped over, and the first blocked
285 /// step is bisected until the feet end's last step is below half a
286 /// [`skin_width`](super::CharacterConfig::skin_width): a turn into a wall
287 /// stops with the body touching it, less than that short of it.
288 ///
289 /// A turn that nothing stops reaches `yaw` **as given**, not wrapped. One
290 /// that is stopped reports the yaw it reached as `body`'s yaw plus the
291 /// signed [`turned`](LyingTurnOutcome::turned), not wrapped either.
292 ///
293 /// # The pitch
294 ///
295 /// Turned at a fixed pitch, a body lying on a slope would swing its feet
296 /// into the slope — facing up it, its feet are lower than its head, and
297 /// the ground under them rises as they swing round — and the ground would
298 /// stop the turn. So while the body's head is this controller's
299 /// [`position`](Self::position) and the controller is
300 /// [grounded](Self::is_grounded), the pitch follows the plane of the
301 /// [`ground`](Self::ground) through the turn: each pose's pitch is
302 /// `body`'s plus how much that plane's slope along the new facing differs
303 /// from its slope along the old one. On a walkable plane the body lies on
304 /// it at every yaw, and a body the settle pitched off it — over a curb, or
305 /// clamped to the slope limit — keeps the difference. Otherwise the pitch
306 /// is held.
307 ///
308 /// **The turn does not settle.** The head stays put, and the next
309 /// [`move_lying`](Self::move_lying) — a move of nothing, if the game has
310 /// no other — lays the body on the ground under its turned feet, as it
311 /// does after every move.
312 ///
313 /// # From a pose already inside something
314 ///
315 /// A body the game has let into geometry — gone prone against a wall,
316 /// say — is not refused a turn for that, or it could never turn out.
317 /// Every blocked pose from the start is passed over until the first clear
318 /// one, and from there the turn stops at the first blocked pose as any
319 /// other does. A turn that never comes clear goes the whole way. There is
320 /// no penetration depth for a lying capsule, so what it passes over is not
321 /// told apart: a body inside one wall turns through a second one it meets
322 /// before it is clear of the first.
323 ///
324 /// # Panics
325 ///
326 /// Panics if `yaw` or `body`'s yaw is not finite.
327 #[must_use]
328 pub fn turn_lying(
329 &self,
330 world: &mut PhysicsWorld,
331 body: &LyingCapsule,
332 yaw: f64,
333 ) -> LyingTurnOutcome {
334 assert!(
335 yaw.is_finite() && body.yaw.is_finite(),
336 "a lying body turns from a finite yaw to a finite yaw, not {} to {yaw}",
337 body.yaw,
338 );
339 // The shorter way, in `(-π, π]`. The remainder is exact in IEEE
340 // arithmetic and the rest is plain rounding, so this is the same on
341 // every target: no platform maths.
342 let whole = (yaw - body.yaw).rem_euclid(TAU);
343 let delta = if whole > PI { whole - TAU } else { whole };
344
345 let plane = self
346 .ground
347 .filter(|_| body.head == self.position)
348 .map(|ground| ground.normal);
349 // The sine of the slope along a facing of a plane whose upward normal
350 // is `normal`: its rise over its run, turned into a sine.
351 let slope_along = |normal: DVec3, yaw: f64| {
352 let facing = LyingCapsule { yaw, ..*body }.facing();
353 let gradient = -(normal.x * facing.x + normal.z * facing.z) / normal.y;
354 gradient / (1.0 + gradient * gradient).sqrt()
355 };
356 let pose = |turned: f64| {
357 let pitch_sine = plane.map_or(body.pitch_sine, |normal| {
358 let change = slope_along(normal, turned) - slope_along(normal, body.yaw);
359 (body.pitch_sine + change).clamp(-1.0, 1.0)
360 });
361 LyingCapsule {
362 yaw: turned,
363 pitch_sine,
364 ..*body
365 }
366 };
367
368 let full = LyingTurnOutcome {
369 body: LyingCapsule {
370 yaw,
371 ..pose(body.yaw + delta)
372 },
373 turned: delta,
374 fraction: 1.0,
375 blocker: None,
376 };
377 if delta == 0.0 {
378 return full;
379 }
380
381 // Level, the feet end swings `length · Δyaw`; following a plane, up
382 // to `1 / normal.y` times that, as a circle on the plane is an ellipse
383 // seen from above whose short axis is that much shorter.
384 let cosine = plane.map_or(1.0, |normal| normal.y);
385 let from_blocked = self.lying_blocker(world, body).is_some();
386 let stop = self.turn_until_blocked(
387 world,
388 (body.yaw, body.yaw + delta),
389 cosine,
390 from_blocked,
391 pose,
392 );
393 let Some(blocker) = stop.blocker else {
394 return full;
395 };
396 let turned = stop.reached - body.yaw;
397 LyingTurnOutcome {
398 body: pose(stop.reached),
399 turned,
400 fraction: (turned / delta).clamp(0.0, 1.0),
401 blocker: Some(blocker),
402 }
403 }
404
405 /// Lay `body` on the ground under its two ends, looking `reach` down: see
406 /// [`move_lying`](Self::move_lying)'s "The settle".
407 fn settle_lying(
408 &mut self,
409 world: &mut PhysicsWorld,
410 body: LyingCapsule,
411 reach: f64,
412 ) -> LyingCapsule {
413 // Each end looks further than `reach` by as far as the pitch could
414 // still lower it with the other end held — so a body lying level on a
415 // slope finds the ground under its downhill end — but the body only
416 // settles if one end is within `reach`: a body with both ends further
417 // off than that is in the air, and falls as the caller moves it.
418 let feet = body.feet();
419 let limit = self.steepest_pitch_sine();
420 let swing = |sine: f64| reach + body.length * sine.max(0.0);
421 let under_head = self.probe_end(
422 world,
423 body.head,
424 body.radius,
425 swing(limit + body.pitch_sine),
426 );
427 let under_feet = self.probe_end(world, feet, body.radius, swing(limit - body.pitch_sine));
428 self.ground = None;
429 let near = |probe: &GroundProbe| probe.distance <= reach;
430 if !under_head.iter().chain(&under_feet).any(near) {
431 return body;
432 }
433 self.ground = under_head
434 .filter(|probe| probe.walkable)
435 .or(under_feet.filter(|probe| probe.walkable))
436 .map(|probe| probe.contact);
437
438 // Signed, as the walking settle's is: an end nearer walkable ground
439 // than a skin width is lifted back to it, by no more than that width.
440 // Nothing lifts an end off what it cannot stand on, which is how a
441 // body pressed against the edge of a riser would otherwise climb it a
442 // skin width a tick.
443 let skin = self.config.skin_width;
444 let rest = |end: DVec3, probe: GroundProbe| {
445 let lift = if probe.walkable { -skin } else { 0.0 };
446 end.y - (probe.distance - skin).max(lift)
447 };
448 let level = body.length * (1.0 - body.pitch_sine * body.pitch_sine).max(0.0).sqrt();
449 // An end with nothing under it as far as it looks swings to the limit.
450 let pitch_sine = match (under_head, under_feet) {
451 (None, None) => return body,
452 _ if level <= MIN_MOVE => body.pitch_sine,
453 (Some(head), Some(feet_probe)) => {
454 self.resting_pitch(rest(body.head, head), rest(feet, feet_probe), level)
455 }
456 (Some(_), None) => limit,
457 (None, Some(_)) => -limit,
458 };
459 // The pitch turns the body about its head, so the feet end moves
460 // along the ground from where it was probed: its resting height is
461 // carried there along the plane it met, exact on a plane. The body is
462 // then as low as it can lie with neither end below its resting height.
463 let turned = LyingCapsule { pitch_sine, ..body };
464 let rise = turned.head.y - turned.feet().y;
465 let shift = turned.feet() - feet;
466 let on_head = under_head.map(|probe| rest(body.head, probe));
467 let on_feet = under_feet.map(|probe| {
468 let normal = probe.contact.normal;
469 let along = if normal.y > MIN_MOVE {
470 (normal.x * shift.x + normal.z * shift.z) / normal.y
471 } else {
472 0.0
473 };
474 rest(feet, probe) - along + rise
475 });
476 let height = on_head
477 .into_iter()
478 .chain(on_feet)
479 .fold(f64::NEG_INFINITY, f64::max);
480 let resting = LyingCapsule {
481 head: DVec3::new(body.head.x, height, body.head.z),
482 ..turned
483 };
484 if self.lying_blocker(world, &resting).is_none() {
485 return resting;
486 }
487
488 let about_head = self.turn_to_rest(world, &resting, Pivot::Head);
489 let about_feet = self.turn_to_rest(world, &resting, Pivot::Feet);
490 let turn = |pose: &LyingCapsule| (pose.pitch_sine - resting.pitch_sine).abs();
491 match (about_head, about_feet) {
492 (Some(head), Some(feet)) if turn(&feet) < turn(&head) => feet,
493 (Some(head), _) => head,
494 (None, Some(feet)) => feet,
495 (None, None) => body,
496 }
497 }
498
499 /// The pitch sine of the line through two ends' resting heights `level`
500 /// apart horizontally, clamped to the slope limit.
501 fn resting_pitch(&self, head_rest: f64, feet_rest: f64, level: f64) -> f64 {
502 let limit = self.steepest_pitch_sine();
503 let gradient = (head_rest - feet_rest) / level;
504 (gradient / (1.0 + gradient * gradient).sqrt()).clamp(-limit, limit)
505 }
506
507 /// The largest pitch sine a lying body takes, either way: the sine of the
508 /// steepest walkable slope, whose cosine is
509 /// [`min_ground_normal_y`](super::CharacterConfig::min_ground_normal_y).
510 fn steepest_pitch_sine(&self) -> f64 {
511 let cosine = self.config.min_ground_normal_y.clamp(0.0, 1.0);
512 (1.0 - cosine * cosine).sqrt()
513 }
514
515 /// `resting`, which is blocked, turned about `pivot` until it is clear:
516 /// from the other end raised as far as the slope limit allows, down, to
517 /// the last pose short of touching. `None` if even that first pose is
518 /// blocked, or there is no turn to make. Stepped and bisected as
519 /// [`turn_until_blocked`](Self::turn_until_blocked) turns.
520 fn turn_to_rest(
521 &self,
522 world: &mut PhysicsWorld,
523 resting: &LyingCapsule,
524 pivot: Pivot,
525 ) -> Option<LyingCapsule> {
526 let limit = self.steepest_pitch_sine();
527 let feet_height = resting.feet().y;
528 let pose = |pitch_sine: f64| {
529 let head_height = match pivot {
530 Pivot::Head => resting.head.y,
531 Pivot::Feet => feet_height + resting.length * pitch_sine,
532 };
533 LyingCapsule {
534 head: DVec3::new(resting.head.x, head_height, resting.head.z),
535 pitch_sine,
536 ..*resting
537 }
538 };
539 let top = match pivot {
540 Pivot::Head => -limit,
541 Pivot::Feet => limit,
542 };
543 let span = resting.pitch_sine - top;
544 if span.abs() <= MIN_MOVE || self.lying_blocker(world, &pose(top)).is_some() {
545 return None;
546 }
547
548 // An end swings `length · Δθ`, and `Δθ ≤ Δsine / cos θ`, where the
549 // cosine is least at the steeper of the two ends of the turn.
550 let steepest = top.abs().max(resting.pitch_sine.abs());
551 let cosine = (1.0 - steepest * steepest).sqrt();
552 let stop = self.turn_until_blocked(world, (top, resting.pitch_sine), cosine, false, pose);
553 Some(pose(stop.reached))
554 }
555
556 /// Turn a body through the poses `pose` gives from `from` to `to` until
557 /// one would be inside something: the loop both
558 /// [`turn_lying`](Self::turn_lying) and the settle's turn onto an edge
559 /// run. The radius and length are `pose`'s own, and `cosine` bounds how
560 /// far an end travels: no more than `length · |Δ| / cosine` for a change
561 /// `Δ` of the parameter.
562 ///
563 /// The turn is stepped so no end moves more than a radius from one pose to
564 /// the next — so nothing thinner than the body is stepped over — then the
565 /// first blocked step is bisected until `length` times the parameter left
566 /// between it and the last clear one is below half a skin width.
567 ///
568 /// The pose at `from` is not checked: it is clear unless
569 /// `from_blocked` says it is not. From a blocked pose the steps pass over
570 /// every blocked pose until the first clear one, and only a blocked pose
571 /// after that stops the turn.
572 fn turn_until_blocked(
573 &self,
574 world: &mut PhysicsWorld,
575 (from, to): (f64, f64),
576 cosine: f64,
577 from_blocked: bool,
578 pose: impl Fn(f64) -> LyingCapsule,
579 ) -> TurnStop {
580 let shape = pose(from);
581 let span = to - from;
582 let steps = (shape.length * span.abs() / (shape.radius * cosine))
583 .ceil()
584 .clamp(1.0, f64::from(MAX_TURN_STEPS));
585 let (mut clear, mut stuck, mut blocked) = (from, from_blocked, None);
586 for step in 1..=MAX_TURN_STEPS {
587 let share = f64::from(step) / steps;
588 if share > 1.0 {
589 break;
590 }
591 let at = from + span * share;
592 match self.lying_blocker(world, &pose(at)) {
593 Some(collider) if !stuck => {
594 blocked = Some((at, collider));
595 break;
596 }
597 Some(_) => {}
598 None => (clear, stuck) = (at, false),
599 }
600 }
601 let Some((mut at, mut blocker)) = blocked else {
602 return TurnStop {
603 reached: clear,
604 blocker: None,
605 };
606 };
607 for _ in 0..MAX_BISECTIONS {
608 if shape.length * (at - clear).abs() <= 0.5 * self.config.skin_width {
609 break;
610 }
611 let middle = 0.5 * (clear + at);
612 match self.lying_blocker(world, &pose(middle)) {
613 Some(collider) => (at, blocker) = (middle, collider),
614 None => clear = middle,
615 }
616 }
617 TurnStop {
618 reached: clear,
619 blocker: Some(blocker),
620 }
621 }
622
623 /// Sweep a sphere of `radius` straight down `distance` from `centre`, as
624 /// the walking probe sweeps the capsule, and describe what it meets.
625 fn probe_end(
626 &self,
627 world: &mut PhysicsWorld,
628 centre: DVec3,
629 radius: f64,
630 distance: f64,
631 ) -> Option<GroundProbe> {
632 let found = world.sweep_sphere_filtered(
633 &Segment::new(centre, centre - UP * distance),
634 radius,
635 self.filter(),
636 )?;
637 Some(self.ground_probe(found, distance))
638 }
639}