pub struct Character {
pub position: Vec3,
pub velocity: Vec3,
pub grounded: bool,
pub ground_normal: Vec3,
pub jump: JumpState,
pub support: Option<Support>,
}Fields§
§position: Vec3§velocity: Vec3Velocity relative to a supporting platform; airborne velocity is in world space.
grounded: bool§ground_normal: Vec3§jump: JumpState§support: Option<Support>Implementations§
Source§impl Character
impl Character
pub fn new(feet: Vec3) -> Character
Sourcepub fn try_set_height<T>(
&self,
world: &T,
shape: &mut CharacterShape,
height: f32,
membership_mask: u32,
max_candidates: usize,
) -> Result<Clearance, ClearanceError>
pub fn try_set_height<T>( &self, world: &T, shape: &mut CharacterShape, height: f32, membership_mask: u32, max_candidates: usize, ) -> Result<Clearance, ClearanceError>
Change the caller-owned shape’s full sole-to-top height at the current
feet position. Capsule radius or Box2 half-width stays exactly unchanged;
Box2 stores half of height in half.y. Shape limits still apply.
Queries the target shape even for shrinking or an unchanged height. Only
a successful complete clearance query commits shape; every error leaves
both shape and motor unchanged. The motor is borrowed immutably: velocity,
jump timers/held state, grounded state, normal and support anchors all stay
intact. No input edge, tick, recovery, carry or snap is performed. Terrain
is tested at its current pose; pending platform carry remains for step.
Pass the accepted shape to later steps. Use the movement settings’ mask
and candidate limit for consistent filtering. This operation permits a
zero candidate limit as documented by check_clearance.
This runnable slice-Terrain recipe keeps support across crouching and a rejected stand, then stands in clear space and receives platform carry:
use ferravex_math::Vec3;
use ferravex_physics::character::{Character, CharacterSettings, CharacterShape,
ClearanceError, Obstacle};
let settings = CharacterSettings::default();
let mut shape = CharacterShape::Capsule { radius: 0.3, height: 1.8 };
let mut platform = Obstacle::cuboid(7, -Vec3::Y * 0.1, Vec3::new(4., 0.1, 4.));
let mut motor = Character::new(Vec3::Y * settings.skin);
motor.teleport(&[platform][..], shape, settings, motor.position)?;
let saved = motor;
motor.try_set_height(&[platform][..], &mut shape, 1.,
settings.membership_mask, settings.max_candidates)?;
let ceiling = Obstacle::cuboid(9, Vec3::Y * 1.5, Vec3::new(2., 0.1, 2.));
assert_eq!(motor.try_set_height(&[platform, ceiling][..], &mut shape, 1.8,
settings.membership_mask, settings.max_candidates),
Err(ClearanceError::Blocked { obstacle: 9 }));
assert_eq!(shape, CharacterShape::Capsule { radius: 0.3, height: 1. });
assert_eq!(motor, saved);
motor.try_set_height(&[platform][..], &mut shape, 1.8,
settings.membership_mask, settings.max_candidates)?;
platform.position.x += 0.1;
let tick = motor.step(&[platform][..], shape, settings, Default::default(), 1. / 120.)?;
assert!((tick.carried.x - 0.1).abs() < 1e-5);Sourcepub fn reset(
&mut self,
shape: CharacterShape,
feet: Vec3,
) -> Result<(), CharacterError>
pub fn reset( &mut self, shape: CharacterShape, feet: Vec3, ) -> Result<(), CharacterError>
Reset to checked feet coordinates without querying terrain. Clears velocity,
support, grounded state and all jump state. Invalid coordinates or shape
leave self unchanged. Use Self::teleport to resolve overlaps immediately.
Hosts must reset both previous/current interpolation poses to self.position
after success, and discard pending gameplay edges on a restart. This method
does not advance time or consume an input edge.
use ferravex_math::{Vec2, Vec3};
use ferravex_physics::character::{Character, CharacterShape, JumpState};
let shape = CharacterShape::Box2 { half: Vec2::new(0.3, 0.8) };
let mut motor = Character::new(Vec3::ZERO);
motor.velocity = Vec3::X * 4.;
motor.reset(shape, Vec3::Y * 2.)?;
let (previous, current) = (motor.position, motor.position);
assert_eq!(previous, current);
assert_eq!(motor.velocity, Vec3::ZERO);
assert_eq!(motor.jump, JumpState::default());Sourcepub fn teleport<T>(
&mut self,
world: &T,
shape: CharacterShape,
settings: CharacterSettings,
feet: Vec3,
) -> Result<Motion, CharacterError>
pub fn teleport<T>( &mut self, world: &T, shape: CharacterShape, settings: CharacterSettings, feet: Vec3, ) -> Result<Motion, CharacterError>
Reset at a destination, recover initial overlaps and probe/snap to walkable ground using current terrain poses. This does not sweep from the old pose. Velocity and jump state are cleared; support is acquired at the destination with fresh anchors. Every error leaves all previous state intact.
The returned translation is relative to feet, not the old position. Set
both host interpolation poses to the resolved self.position after
success. Reset camera history and pending gameplay edges as appropriate for
the host’s restart; no fixed tick is consumed here.
use ferravex_math::Vec3;
use ferravex_physics::character::{Character, CharacterShape, Obstacle};
let shape = CharacterShape::Capsule { radius: 0.3, height: 1.8 };
let floor = Obstacle::cuboid(7, -Vec3::Y * 0.5, Vec3::new(4., 0.5, 4.));
let mut motor = Character::new(Vec3::Y * 10.);
let report = motor.teleport(&[floor][..], shape, Default::default(), -Vec3::Y * 0.1)?;
assert!(report.recovery.distance > 0.1);
assert!(report.grounded);
assert_eq!(report.support.unwrap().id, 7);
let (previous, current) = (motor.position, motor.position);
assert_eq!(previous, current);Sourcepub fn step<T>(
&mut self,
world: &T,
shape: CharacterShape,
settings: CharacterSettings,
input: CharacterInput,
dt: f32,
) -> Result<CharacterStep, CharacterError>
pub fn step<T>( &mut self, world: &T, shape: CharacterShape, settings: CharacterSettings, input: CharacterInput, dt: f32, ) -> Result<CharacterStep, CharacterError>
Advance at a fixed dt in (0, 0.1]. Terrain must already have this tick’s pose. All state updates commit together; invalid input or exhausted queries leave self unchanged. The returned recovery/candidate totals cover the whole tick. Carry is reported separately from the final support, so jumping or walking off a platform does not hide its contribution earlier in the tick.
use ferravex_math::Vec3;
use ferravex_physics::character::{Character, CharacterShape, Obstacle};
let shape = CharacterShape::Capsule { radius: 0.3, height: 1.8 };
let mut platform = Obstacle::cuboid(7, -Vec3::Y * 0.1, Vec3::new(4., 0.1, 4.));
let mut motor = Character::new(Vec3::ZERO);
motor.teleport(&[platform][..], shape, Default::default(), Vec3::Y * 0.002)?;
platform.position.x += 0.1; // Move terrain before advancing the motor.
let tick = motor.step(&[platform][..], shape, Default::default(), Default::default(), 1. / 120.)?;
let carry = tick.motion.support_motion.unwrap();
assert_eq!(carry.id, 7);
assert!((carry.applied.x - 0.1).abs() < 1e-5);
assert!(!carry.blocked);
assert_eq!(tick.carried, carry.applied);