Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.55k stars 120 forks source link

Add collide and slide kinematic controller #487

Open UndefinedBHVR opened 3 months ago

UndefinedBHVR commented 3 months ago

Objective

Solution


Changelog

Note: You can run the new example with cargo run --example kinematic_character_cas_3d

Added

UndefinedBHVR commented 3 months ago

Current noteworthy issues:

jirisvd commented 3 months ago

Character slides down non-steep slopes.

This is because right now, apply_gravity applies downward velocity even when standing on the ground. This is problematic both because of this issue and because when the character walks off a cliff it immediately starts falling down at a very high velocity instead of a gradually increasing gravitational force.

When a Grounded check is added to apply_gravity like this:

fn apply_gravity(
    time: Res<Time>,
    mut controllers: Query<(&ControllerGravity, &mut CharacterController, Option<&Grounded>)>,
) {
    for (gravity, mut character_controller, grounded) in &mut controllers {
        if grounded.is_some() && character_controller.velocity.y < 0.0 {
            character_controller.velocity.y = 0.0;
        } else {
            character_controller.velocity += gravity.0 * time.delta_seconds();
        }
    }
}

And the ShapeCaster ground checking component's with_max_time_of_impact(0.2) is changed to 0.05 in order to have a more reasonable gap between the character capsule and the ground, this no longer happens.

Since Grounded is only added when standing on a slope that doesn't exceed max_slope_angle, this retains the sliding down on slopes that are too steep.

The downside of this is that now when walking down a slope, the character doesn't stick to it. But that can be solved by adding ground snapping logic to the collide and slide algorithm.

UndefinedBHVR commented 3 months ago

Accidentally pushed commit from an experiment branch, force pushed to undo it.