crcbl_phys/character/preview.rs
1//! Asking where an upright move would leave a character without making it:
2//! [`CharacterController::preview_upright`] and the [`UprightPreview`] it
3//! returns.
4
5use glam::DVec3;
6
7use super::{CharacterController, GroundContact, MoveOutcome, SlideContact, WorldReader};
8use crate::collider::Capsule;
9use crate::world::{OverlapQueries, QueryScratch};
10
11/// Where a [`move_and_slide_into`] would leave a character, and what it would
12/// meet on the way, from [`CharacterController::preview_upright`].
13///
14/// Every field is what the move itself would report or leave behind from the
15/// same state, to the bit: the preview runs the move's own solve and only
16/// skips writing the result anywhere.
17///
18/// [`move_and_slide_into`]: CharacterController::move_and_slide_into
19#[derive(Debug, Clone, PartialEq)]
20pub struct UprightPreview {
21 /// What the move would return: the motion it would apply, the
22 /// depenetration it would make first, and the walls, ceilings, step and
23 /// slides it would meet.
24 pub outcome: MoveOutcome,
25 /// The capsule where the move would leave it: the controller's
26 /// [`capsule`](CharacterController::capsule) after the move, which is
27 /// also what the move would write to its
28 /// [self collider](CharacterController::with_self_collider).
29 pub capsule: Capsule,
30 /// The ground the move would leave the character standing on, which the
31 /// controller's [`ground`](CharacterController::ground) would then report.
32 /// `None` when it would end the move unsupported — off a ledge, in the
33 /// air, or asking to go up.
34 pub ground: Option<GroundContact>,
35 /// Every sweep of the slide that would meet something, in the order it
36 /// would meet them: the list
37 /// [`move_and_slide_into`](CharacterController::move_and_slide_into)
38 /// would write, holding exactly
39 /// [`outcome.slides`](MoveOutcome::slides) entries. See [`SlideContact`].
40 pub contacts: Vec<SlideContact>,
41}
42
43impl CharacterController {
44 /// Where [`move_and_slide_into`](Self::move_and_slide_into) by `motion`
45 /// would leave this character, **without moving it or anything else**.
46 ///
47 /// # It touches nothing, and the signature is what says so
48 ///
49 /// The controller is borrowed shared, so its position, its ground and
50 /// every other field are what they were. The world is reached only
51 /// through an [`OverlapQueries`] view, which cannot write it: the
52 /// controller's [self collider](Self::with_self_collider) stays where it
53 /// is, as does every other collider, and the broadphase is neither
54 /// refitted nor rebuilt — building the view is what brings a stale tree
55 /// up to date, as the first query after any change would. Calling this
56 /// any number of times changes no later query or move. `scratch` is only the buffers the view's queries
57 /// work in; its contents mean nothing between calls and never change an
58 /// answer.
59 ///
60 /// # It is the move's own solve
61 ///
62 /// The move is depenetration, the slide with its step-up, and the ground
63 /// probe, then a write to the self collider. This runs the first three on
64 /// a copy of the controller and skips the write, so it answers with the
65 /// same self-exclusion, query mask, skin width, step and ground rules —
66 /// the same code, not a second account of it. A move made afterwards from
67 /// the same state, against the same world, returns this preview's
68 /// [`outcome`](UprightPreview::outcome) and contacts exactly.
69 ///
70 /// # Previewing another shape
71 ///
72 /// The capsule previewed is this controller's own. To ask about a
73 /// different one — a contact capsule a game grows by a margin of its
74 /// choosing — preview from a controller built with that shape, bound to
75 /// the same collider and mask:
76 ///
77 /// ```
78 /// use crcbl_phys::{
79 /// BoxCollider, CharacterConfig, CharacterController, PhysicsWorld, QueryScratch,
80 /// };
81 /// use glam::DVec3;
82 ///
83 /// let mut world = PhysicsWorld::new();
84 /// world.add_box(BoxCollider::new(DVec3::new(0.0, -1.0, 0.0), DVec3::new(50.0, 1.0, 50.0)));
85 /// let config = CharacterConfig::default();
86 /// let body = CharacterController::new(config, DVec3::new(0.0, 2.0, 0.0));
87 /// let live = world.add_capsule(body.capsule());
88 /// let body = body.with_self_collider(live);
89 ///
90 /// let grown = CharacterConfig { radius: config.radius + config.skin_width, ..config };
91 /// let probe = CharacterController::new(grown, body.position())
92 /// .with_query_mask(body.query_mask())
93 /// .with_self_collider(live);
94 /// let mut scratch = QueryScratch::new();
95 /// let preview = probe.preview_upright(world.overlap_queries(), &mut scratch, DVec3::new(0.0, -1.5, 0.0));
96 ///
97 /// assert!(preview.outcome.grounded);
98 /// assert_eq!(preview.capsule.radius, grown.radius);
99 /// assert_eq!(world.aabb_of(live), Some(body.capsule().aabb()));
100 /// ```
101 ///
102 /// A controller built with [`CharacterController::new`] starts
103 /// unsupported, as every new controller does, which suits a preview of
104 /// motion through the air. A preview from the live controller itself
105 /// starts from the ground it stands on.
106 #[must_use]
107 pub fn preview_upright(
108 &self,
109 world: OverlapQueries<'_>,
110 scratch: &mut QueryScratch,
111 motion: DVec3,
112 ) -> UprightPreview {
113 // Every field but the depenetration buffer, which holds nothing
114 // between moves and is not worth copying.
115 let mut solver = Self {
116 config: self.config,
117 position: self.position,
118 ground: self.ground,
119 self_collider: self.self_collider,
120 query_mask: self.query_mask,
121 contacts: Vec::new(),
122 };
123 let mut contacts = Vec::new();
124 let outcome = solver.solve_upright(
125 &mut WorldReader {
126 view: world,
127 scratch,
128 },
129 motion,
130 Some(&mut contacts),
131 );
132 UprightPreview {
133 outcome,
134 capsule: solver.capsule(),
135 ground: solver.ground,
136 contacts,
137 }
138 }
139}