pub struct KinematicCharacterController {
pub up: UnitVector<f32>,
pub offset: CharacterLength,
pub slide: bool,
pub autostep: Option<CharacterAutostep>,
pub max_slope_climb_angle: f32,
pub min_slope_slide_angle: f32,
pub snap_to_ground: Option<CharacterLength>,
pub normal_nudge_factor: f32,
}Expand description
A kinematic character controller for player/NPC movement (walking, climbing, sliding).
This provides classic game character movement: walking on floors, sliding on slopes, climbing stairs, and snapping to ground. It’s kinematic (not physics-based), meaning you control movement directly rather than applying forces.
Not suitable for: Ragdolls, vehicles, or physics-driven movement (use dynamic bodies instead).
§How it works
- You provide desired movement (e.g., “move forward 5 units”)
- Controller casts the character shape through the world
- It handles collisions: sliding along walls, stepping up stairs, snapping to ground
- Returns the final movement to apply
§Example
let controller = KinematicCharacterController {
slide: true, // Slide along walls instead of stopping
autostep: Some(CharacterAutostep::default()), // Auto-climb stairs
max_slope_climb_angle: 45.0_f32.to_radians(), // Max climbable slope
..Default::default()
};
// In your game loop:
let desired_movement = vector![input_x, 0.0, input_z] * speed * dt;
let movement = controller.move_shape(
dt,
&query_pipeline,
&character_shape,
&character_pos,
desired_movement,
|_| {} // Collision event callback
);
character_pos.translation.vector += movement.translation;Fields§
§up: UnitVector<f32>The direction that goes “up”. Used to determine where the floor is, and the floor’s angle.
offset: CharacterLengthA small gap to preserve between the character and its surroundings.
This value should not be too large to avoid visual artifacts, but shouldn’t be too small (must not be zero) to improve numerical stability of the character controller.
slide: boolShould the character try to slide against the floor if it hits it?
autostep: Option<CharacterAutostep>Should the character automatically step over small obstacles? (disabled by default)
Note that autostepping is currently a very computationally expensive feature, so it is disabled by default.
max_slope_climb_angle: f32The maximum angle (radians) between the floor’s normal and the up vector that the
character is able to climb.
min_slope_slide_angle: f32The minimum angle (radians) between the floor’s normal and the up vector before the
character starts to slide down automatically.
snap_to_ground: Option<CharacterLength>Should the character be automatically snapped to the ground if the distance between the ground and its feed are smaller than the specified threshold?
normal_nudge_factor: f32Increase this number if your character appears to get stuck when sliding against surfaces.
This is a small distance applied to the movement toward the contact normals of shapes hit by the character controller. This helps shape-casting not getting stuck in an always-penetrating state during the sliding calculation.
This value should remain fairly small since it can introduce artificial “bumps” when sliding along a flat surface.
Implementations§
Source§impl KinematicCharacterController
impl KinematicCharacterController
Sourcepub fn move_shape(
&self,
dt: f32,
queries: &QueryPipeline<'_>,
character_shape: &dyn Shape,
character_pos: &Isometry<f32>,
desired_translation: Vector<f32>,
events: impl FnMut(CharacterCollision),
) -> EffectiveCharacterMovement
pub fn move_shape( &self, dt: f32, queries: &QueryPipeline<'_>, character_shape: &dyn Shape, character_pos: &Isometry<f32>, desired_translation: Vector<f32>, events: impl FnMut(CharacterCollision), ) -> EffectiveCharacterMovement
Computes the possible movement for a shape.
Sourcepub fn solve_character_collision_impulses<'a>(
&self,
dt: f32,
queries: &mut QueryPipelineMut<'_>,
character_shape: &dyn Shape,
character_mass: f32,
collisions: impl IntoIterator<Item = &'a CharacterCollision>,
)
pub fn solve_character_collision_impulses<'a>( &self, dt: f32, queries: &mut QueryPipelineMut<'_>, character_shape: &dyn Shape, character_mass: f32, collisions: impl IntoIterator<Item = &'a CharacterCollision>, )
For the given collisions between a character and its environment, this method will apply impulses to the rigid-bodies surrounding the character shape at the time of the collisions. Note that the impulse calculation is only approximate as it is not based on a global constraints resolution scheme.
Trait Implementations§
Source§impl Clone for KinematicCharacterController
impl Clone for KinematicCharacterController
Source§fn clone(&self) -> KinematicCharacterController
fn clone(&self) -> KinematicCharacterController
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for KinematicCharacterController
impl Debug for KinematicCharacterController
impl Copy for KinematicCharacterController
Auto Trait Implementations§
impl Freeze for KinematicCharacterController
impl RefUnwindSafe for KinematicCharacterController
impl Send for KinematicCharacterController
impl Sync for KinematicCharacterController
impl Unpin for KinematicCharacterController
impl UnwindSafe for KinematicCharacterController
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.