Struct Character

Source
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: Vec3

Velocity 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

Source

pub fn new(feet: Vec3) -> Character

Source

pub fn try_set_height<T>( &self, world: &T, shape: &mut CharacterShape, height: f32, membership_mask: u32, max_candidates: usize, ) -> Result<Clearance, ClearanceError>
where T: Terrain + ?Sized,

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);
Source

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());
Source

pub fn teleport<T>( &mut self, world: &T, shape: CharacterShape, settings: CharacterSettings, feet: Vec3, ) -> Result<Motion, CharacterError>
where T: Terrain + ?Sized,

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);
Source

pub fn step<T>( &mut self, world: &T, shape: CharacterShape, settings: CharacterSettings, input: CharacterInput, dt: f32, ) -> Result<CharacterStep, CharacterError>
where T: Terrain + ?Sized,

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);

Trait Implementations§

Source§

impl Clone for Character

Source§

fn clone(&self) -> Character

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Character

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Character

Source§

fn eq(&self, other: &Character) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Character

Source§

impl StructuralPartialEq for Character

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.