pub struct CharacterController { /* private fields */ }Expand description
A capsule that walks.
Drive it one displacement at a time, at the fixed server timestep:
use crcbl_phys::{BoxCollider, CharacterConfig, CharacterController, PhysicsWorld};
use glam::DVec3;
let mut world = PhysicsWorld::new();
world.add_box(BoxCollider::new(
DVec3::new(0.0, -1.0, 0.0),
DVec3::new(50.0, 1.0, 50.0),
));
let config = CharacterConfig::default();
// Feet on the floor: the capsule centre is a radius plus a half-height up.
let feet = config.radius + config.half_height;
let mut character = CharacterController::new(config, DVec3::new(0.0, feet, 0.0));
let dt = 1.0 / 60.0;
// The caller owns the camera, so the caller owns this direction.
let intent = DVec3::new(1.0, 0.0, 0.0);
let gravity = DVec3::new(0.0, -9.81 * dt * dt, 0.0);
for _ in 0..30 {
let outcome = character.move_and_slide(&mut world, intent * 4.0 * dt + gravity);
assert!(outcome.grounded);
}
assert!(character.position().x > 1.0);Implementations§
Source§impl CharacterController
impl CharacterController
Sourcepub fn move_lying(
&mut self,
world: &mut PhysicsWorld,
body: &LyingCapsule,
motion: DVec3,
) -> LyingMoveOutcome
pub fn move_lying( &mut self, world: &mut PhysicsWorld, body: &LyingCapsule, motion: DVec3, ) -> LyingMoveOutcome
Move a body lying as body by a world-space displacement of its
head, sliding along whatever gets in the way of any part of it, and
settle it on the ground along its length.
The move starts from body: its head becomes this controller’s
position — a head somewhere else is a teleport, and
forgets the ground as set_position does — and
its yaw, pitch, radius and length are the shape swept. The controller’s
own config supplies the rest: the skin width, the
slope limit, the slide budget and the snap distance, but not the shape.
The self collider and the
query mask apply as they do to every other
query, and the self collider is written back as this controller’s own
capsule at the head, as a walking move writes it.
§The move
motion is a displacement and not a velocity, and a grounded move
replaces its vertical part with the rise of the ground under the body,
exactly as move_and_slide does. The whole
capsule is swept along it, and each surface it meets is slid along with
the same plane-set clip. The body does not turn while it moves: a
change of yaw is turn_lying’s, made beforehand,
and a change of pitch is the settle’s.
A lying body does not step up. A riser low enough that the round end
meets its edge on a walkable slope — below about
radius · (1 - min_ground_normal_y) plus a skin width — is slid up and
over, as a ramp is. A taller one is a wall, and stays one: a grounded
body meets a wall as though it were upright, as Unreal’s
SlideAlongSurface does, so the edge’s upward lean does not lift it,
and the settle never lifts an end onto what it cannot stand on.
§The settle
A sphere of the body’s radius is swept straight down under each end of
the core, and each end’s resting point is where that sphere would
sit a skin_width above what it
met. The body settles only if one end is within the walking move’s own
ground probe of what is under it; otherwise it is in the air, and falls
as the caller moves it. Each end looks further than that, as far as the
slope limit could swing it down with the other end held, so a body lying
level along a slope finds the ground under its downhill end.
The body lies on the line through the two resting points: its pitch is
that line’s, clamped to the steepest walkable slope — the pitch
sine never exceeds sqrt(1 - min_ground_normal_y²) either way — and it
is placed as low as it can go while neither end sinks below its
resting point. On even ground within the limit both ends rest; on
ground steeper than it, the uphill end rests and the downhill end is
held off the slope. An end with nothing under it as far as it looks
swings down to the limit, the body resting on the other end — or, where
that swings it into the ground, turning onto it as an edge is met
below.
A line that would cut through an edge — a curb the head has gone over, the crest of a hill, a ridge under the middle — is refused. The body turns toward it instead about one end, from that end’s other end raised as far as the slope limit allows down to where it first touches what is in the way; about the head and about the feet are both tried, and the one that turns less wins, the head on a tie. Crawling head first off a curb, the body therefore rides level with its head out over the drop, then tips head down once resting on the head is the smaller turn. If neither turn has a clear pose to start from, the body is left as the slide left it.
The ground ground reports is the head’s, or the feet
end’s where the head’s is not walkable. The body is grounded when
either is.
Sourcepub fn move_lying_into(
&mut self,
world: &mut PhysicsWorld,
body: &LyingCapsule,
motion: DVec3,
contacts: &mut Vec<SlideContact>,
) -> LyingMoveOutcome
pub fn move_lying_into( &mut self, world: &mut PhysicsWorld, body: &LyingCapsule, motion: DVec3, contacts: &mut Vec<SlideContact>, ) -> LyingMoveOutcome
move_lying, writing every sweep of the slide
that met something into contacts (which is cleared first), in the
order the slide met them, as
move_and_slide_into does for the
upright move.
The move itself is the same one, to the bit: recording reads what the
slide found and never feeds back into it. Only the slide is recorded,
not the settle: the settle’s probes under each end and its turn onto an
edge are not sweeps the body was blocked on, and what they found is in
the body and ground the
move leaves. See SlideContact for what each entry holds; a grounded
body records a wall’s normal as the sweep met it, leaning, though it
slid along the wall made upright.
Sourcepub fn turn_lying(
&self,
world: &mut PhysicsWorld,
body: &LyingCapsule,
yaw: f64,
) -> LyingTurnOutcome
pub fn turn_lying( &self, world: &mut PhysicsWorld, body: &LyingCapsule, yaw: f64, ) -> LyingTurnOutcome
Turn a body lying as body about its head toward yaw, stopping at
the first yaw where it would be inside something.
A prone body cannot turn into a wall. The turn pivots on the head,
where a game’s actor origin and first-person camera sit, so a wall
behind or beside the player limits how far the legs swing and never
moves the view: the head of the body
returned is exactly body’s. The turn is a query, as
lying_blocker is: it moves nothing and records
nothing, and it sees what that sees, under this controller’s
self collider and
query mask. A pose touching something is
clear; only a penetration stops the turn.
§Which way, and how far
The turn goes the shorter way round, across the ±π wrap where
that is shorter — from 3.0 to -3.0 is a turn of about +0.28 — and
a turn of exactly half a circle goes the positive way, which is to the
left. It is stepped so the feet end moves at most a radius a step, so
nothing thinner than the body is stepped over, and the first blocked
step is bisected until the feet end’s last step is below half a
skin_width: a turn into a wall
stops with the body touching it, less than that short of it.
A turn that nothing stops reaches yaw as given, not wrapped. One
that is stopped reports the yaw it reached as body’s yaw plus the
signed turned, not wrapped either.
§The pitch
Turned at a fixed pitch, a body lying on a slope would swing its feet
into the slope — facing up it, its feet are lower than its head, and
the ground under them rises as they swing round — and the ground would
stop the turn. So while the body’s head is this controller’s
position and the controller is
grounded, the pitch follows the plane of the
ground through the turn: each pose’s pitch is
body’s plus how much that plane’s slope along the new facing differs
from its slope along the old one. On a walkable plane the body lies on
it at every yaw, and a body the settle pitched off it — over a curb, or
clamped to the slope limit — keeps the difference. Otherwise the pitch
is held.
The turn does not settle. The head stays put, and the next
move_lying — a move of nothing, if the game has
no other — lays the body on the ground under its turned feet, as it
does after every move.
§From a pose already inside something
A body the game has let into geometry — gone prone against a wall, say — is not refused a turn for that, or it could never turn out. Every blocked pose from the start is passed over until the first clear one, and from there the turn stops at the first blocked pose as any other does. A turn that never comes clear goes the whole way. There is no penetration depth for a lying capsule, so what it passes over is not told apart: a body inside one wall turns through a second one it meets before it is clear of the first.
§Panics
Panics if yaw or body’s yaw is not finite.
Source§impl CharacterController
impl CharacterController
Sourcepub fn preview_upright(
&self,
world: OverlapQueries<'_>,
scratch: &mut QueryScratch,
motion: DVec3,
) -> UprightPreview
pub fn preview_upright( &self, world: OverlapQueries<'_>, scratch: &mut QueryScratch, motion: DVec3, ) -> UprightPreview
Where move_and_slide_into by motion
would leave this character, without moving it or anything else.
§It touches nothing, and the signature is what says so
The controller is borrowed shared, so its position, its ground and
every other field are what they were. The world is reached only
through an OverlapQueries view, which cannot write it: the
controller’s self collider stays where it
is, as does every other collider, and the broadphase is neither
refitted nor rebuilt — building the view is what brings a stale tree
up to date, as the first query after any change would. Calling this
any number of times changes no later query or move. scratch is only the buffers the view’s queries
work in; its contents mean nothing between calls and never change an
answer.
§It is the move’s own solve
The move is depenetration, the slide with its step-up, and the ground
probe, then a write to the self collider. This runs the first three on
a copy of the controller and skips the write, so it answers with the
same self-exclusion, query mask, skin width, step and ground rules —
the same code, not a second account of it. A move made afterwards from
the same state, against the same world, returns this preview’s
outcome and contacts exactly.
§Previewing another shape
The capsule previewed is this controller’s own. To ask about a different one — a contact capsule a game grows by a margin of its choosing — preview from a controller built with that shape, bound to the same collider and mask:
use crcbl_phys::{
BoxCollider, CharacterConfig, CharacterController, PhysicsWorld, QueryScratch,
};
use glam::DVec3;
let mut world = PhysicsWorld::new();
world.add_box(BoxCollider::new(DVec3::new(0.0, -1.0, 0.0), DVec3::new(50.0, 1.0, 50.0)));
let config = CharacterConfig::default();
let body = CharacterController::new(config, DVec3::new(0.0, 2.0, 0.0));
let live = world.add_capsule(body.capsule());
let body = body.with_self_collider(live);
let grown = CharacterConfig { radius: config.radius + config.skin_width, ..config };
let probe = CharacterController::new(grown, body.position())
.with_query_mask(body.query_mask())
.with_self_collider(live);
let mut scratch = QueryScratch::new();
let preview = probe.preview_upright(world.overlap_queries(), &mut scratch, DVec3::new(0.0, -1.5, 0.0));
assert!(preview.outcome.grounded);
assert_eq!(preview.capsule.radius, grown.radius);
assert_eq!(world.aabb_of(live), Some(body.capsule().aabb()));A controller built with CharacterController::new starts
unsupported, as every new controller does, which suits a preview of
motion through the air. A preview from the live controller itself
starts from the ground it stands on.
Source§impl CharacterController
impl CharacterController
Sourcepub fn new(config: CharacterConfig, position: DVec3) -> Self
pub fn new(config: CharacterConfig, position: DVec3) -> Self
Place a character at position, which is the centre of its capsule
— a character whose feet are on y = 0 starts at
radius + half_height.
It starts ungrounded whatever is beneath it; the first
move_and_slide, even with a zero
displacement, is what finds the floor.
Sourcepub fn with_self_collider(self, collider: ColliderId) -> Self
pub fn with_self_collider(self, collider: ColliderId) -> Self
Tell the controller which collider in the world is its own body, so its sweeps do not find it.
A character that other things have to collide with is registered in the
world like anything else, and then sits on the start of every segment it
sweeps — see PhysicsWorld::sweep_capsule_excluding. Given this, each
move also writes the capsule’s new position back to that collider, so
the world’s copy is never a tick behind.
A character nothing else collides with does not need it.
Sourcepub fn set_self_collider(&mut self, collider: Option<ColliderId>)
pub fn set_self_collider(&mut self, collider: Option<ColliderId>)
Change the world-collider binding without changing movement or ground state.
Sourcepub fn with_query_mask(self, mask: u32) -> Self
pub fn with_query_mask(self, mask: u32) -> Self
Look only at the world’s colliders on a layer in mask: see
PhysicsWorld::set_layers.
Every query the controller makes — the move’s sweeps, the step-up, the
ground probe that ends a move, probe_ground_at
and depenetration — applies it together with the
self collider exclusion, so a collider
masked out is one the character walks through, stands on nothing of,
and is never pushed out of. The default is
ALL_LAYERS: every collider is solid.
Sourcepub fn set_query_mask(&mut self, mask: u32)
pub fn set_query_mask(&mut self, mask: u32)
Change the query mask without changing movement or ground state: see
with_query_mask.
Sourcepub fn query_mask(&self) -> u32
pub fn query_mask(&self) -> u32
The layers this controller’s queries look at.
Sourcepub fn set_position(&mut self, position: DVec3)
pub fn set_position(&mut self, position: DVec3)
Teleport. Discards the ground, because the ground under the old position says nothing about the new one; the next move finds it again.
Sourcepub fn config(&self) -> &CharacterConfig
pub fn config(&self) -> &CharacterConfig
The limits this character moves under.
Sourcepub fn ground(&self) -> Option<&GroundContact>
pub fn ground(&self) -> Option<&GroundContact>
The ground the character is standing on, if it is standing on any.
Sourcepub fn is_grounded(&self) -> bool
pub fn is_grounded(&self) -> bool
Whether the character is standing on walkable ground.
Sourcepub fn probe_ground(
&self,
world: &mut PhysicsWorld,
distance: f64,
) -> Option<GroundProbe>
pub fn probe_ground( &self, world: &mut PhysicsWorld, distance: f64, ) -> Option<GroundProbe>
Ask the world, now, what is under the capsule where it stands.
probe_ground_at at the current
position; see there.
Sourcepub fn probe_ground_at(
&self,
world: &mut PhysicsWorld,
position: DVec3,
distance: f64,
) -> Option<GroundProbe>
pub fn probe_ground_at( &self, world: &mut PhysicsWorld, position: DVec3, distance: f64, ) -> Option<GroundProbe>
Ask the world, now, what is under this controller’s capsule if it were
centred at position, looking at most distance straight down.
Nothing moves and nothing is recorded: the controller keeps its
position and its ground, and the world is only read.
The sweep is the one move_and_slide ends
with — the same capsule, the same self collider
left out of it, the same query mask, the same
is_walkable verdict —
but it reports a surface too steep to stand on as well, with
GroundProbe::walkable false, where a move treats that as no ground.
This is not ground. That is what the last move
found and keeps saying so until the next one, however the world changed
in between: remove the floor and ground() still names it. This sweeps
the world as it is at the call, which is what an action that must start
from real support — a revive, a vault, a mantle — should ask.
distance is how far below the capsule to look. A move leaves a
grounded character one skin_width
above its floor, so a probe for “standing on something” needs more than
that; twice the skin width is the slack the move’s own probe carries.
The capsule’s shape is this controller’s config, so a
caller that swaps configurations for a crouch probes the crouched shape.
§Panics
If distance is negative or not finite. A negative distance would
sweep upward and answer a different question.
Sourcepub fn lying_blocker(
&self,
world: &mut PhysicsWorld,
capsule: &LyingCapsule,
) -> Option<ColliderId>
pub fn lying_blocker( &self, world: &mut PhysicsWorld, capsule: &LyingCapsule, ) -> Option<ColliderId>
A solid collider a body lying as capsule would be inside, or None
if it fits there: PhysicsWorld::lying_capsule_blocker under this
controller’s self collider exclusion and
query mask, so it sees exactly what the
controller’s own moves see.
Like probe_ground_at it moves nothing and
records nothing. The shape is the caller’s and not this controller’s
config: a prone body is a different shape from the
one the controller walks with, and this is the check to make before
letting a character lie down; turn_lying makes it
along a turn.
A capsule whose head is this controller’s
settled position with its radius sits a
skin_width above the floor, and fits;
only a penetration blocks, never a touch.
Sourcepub fn is_walkable(&self, normal: DVec3) -> bool
pub fn is_walkable(&self, normal: DVec3) -> bool
Whether a surface with this normal is ground the character can stand on.
Upward-facing is part of the question and not an assumption: a
min_ground_normal_y of zero
would otherwise make a vertical wall walkable, and then the ramp
adjustment would divide by its zero rise.
Sourcepub fn is_ceiling(&self, normal: DVec3) -> bool
pub fn is_ceiling(&self, normal: DVec3) -> bool
Whether a surface with this normal faces down enough to be a ceiling.
The mirror of is_walkable through the horizontal,
so every normal is ground, ceiling or wall and nothing is two of them.
Sourcepub fn move_and_slide(
&mut self,
world: &mut PhysicsWorld,
motion: DVec3,
) -> MoveOutcome
pub fn move_and_slide( &mut self, world: &mut PhysicsWorld, motion: DVec3, ) -> MoveOutcome
Move by a world-space displacement, sliding along whatever gets in the way.
motion is a displacement for this tick and not a velocity: the caller
has already multiplied by its own timestep, so nothing here reads a
clock and two callers stepping at different rates get the same answer
for the same displacement.
§What a grounded move does with the vertical part
While the character is standing on walkable ground and motion does not
point upward, the vertical part of motion is replaced by the rise
or fall the ground itself imposes — Unreal’s
ComputeGroundMovementDelta. Walking up a ramp therefore covers the
same horizontal distance as walking on the flat, and the gravity a
caller keeps applying does not dig the capsule into the slope. What
holds the character down instead is the ground probe at the end of the
move.
The moment the ground stops being walkable, or the caller asks to go
up, motion is used as given and gravity does what gravity does.
Sourcepub fn move_and_slide_into(
&mut self,
world: &mut PhysicsWorld,
motion: DVec3,
contacts: &mut Vec<SlideContact>,
) -> MoveOutcome
pub fn move_and_slide_into( &mut self, world: &mut PhysicsWorld, motion: DVec3, contacts: &mut Vec<SlideContact>, ) -> MoveOutcome
move_and_slide, writing every sweep that met
something into contacts (which is cleared first), in the order the
slide met them.
The move itself is the same one, to the bit: recording reads what the
slide found and never feeds back into it. See SlideContact for what
each entry holds, and why its fraction is a distance along one straight
sweep rather than a time.
contacts is the caller’s so a game recording every tick reuses one
buffer rather than allocating a list per move; the slide never records
more than max_slides of them.