Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collide and slide kinematic controller #487

Draft
wants to merge 5 commits into
base: collide-and-slide-wip
Choose a base branch
from

Conversation

UndefinedBHVR
Copy link

Objective

Solution

  • Introduce a basic collide and slide implementation for a new Kinematic Character Controller example entitled kinematic_character_cas_3d.

Changelog

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

Added

  • Introduced a work in progress collide and slide function based on community work in the Avian Physics channel of the Bevy discord

@UndefinedBHVR
Copy link
Author

UndefinedBHVR commented Aug 11, 2024

Current noteworthy issues:

  • [FIXED] Character spawns in ground
  • [FIXED] Character gets put in floor at bottom of steep surfaces, and is unable to jump out
  • Doesn't prevent climbing steep slopes, though gravity does somewhat prevent this.
  • [FIXED] Occasionally invalid values make it to let (velocity_normalized, length) = Dir3::new_and_length(velocity).unwrap();, might be fixed with a close to zero check instead of a zero check
  • [FIXED] Character sometimes bounces when going down slopes.
  • [FIXED] Character slides down non-steep slopes. Needs angle checking with grounding to prevent sliding down.

@Jondolf Jondolf added C-Enhancement New feature or request A-Character-Controller Relates to character controllers labels Aug 11, 2024
@jirisvd
Copy link

jirisvd commented Aug 11, 2024

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 UndefinedBHVR changed the base branch from main to collide-and-slide-wip August 12, 2024 02:40
@UndefinedBHVR
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Character-Controller Relates to character controllers C-Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants