Skip to content

Commit

Permalink
add reaply_lost_speed to KinematicCharacterController
Browse files Browse the repository at this point in the history
  • Loading branch information
ntibi committed Dec 17, 2023
1 parent 9ac3503 commit 0c8a7c0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/control/character_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ pub struct KinematicCharacterController {
pub offset: CharacterLength,
/// Should the character try to slide against the floor if it hits it?
pub slide: bool,
/// Should the speed lost because of collisions be applied to the final direction ?
/// Useless if sliding is not enabled
pub reapply_lost_speed: bool,
/// Should the character automatically step over small obstacles?
pub autostep: Option<CharacterAutostep>,
/// The maximum angle (radians) between the floor’s normal and the `up` vector that the
Expand All @@ -124,6 +127,7 @@ impl Default for KinematicCharacterController {
up: Vector::y_axis(),
offset: CharacterLength::Relative(0.01),
slide: true,
reapply_lost_speed: false,
autostep: Some(CharacterAutostep::default()),
max_slope_climb_angle: Real::frac_pi_4(),
min_slope_slide_angle: Real::frac_pi_4(),
Expand Down Expand Up @@ -250,6 +254,7 @@ impl KinematicCharacterController {
toi,
});

let pre_slide_translation_remaining = translation_remaining;
// Try to go up stairs.
if !self.handle_stairs(
bodies,
Expand All @@ -267,6 +272,17 @@ impl KinematicCharacterController {
translation_remaining =
self.handle_slopes(&toi, &translation_remaining, &mut result);
}
if self.slide && self.reapply_lost_speed {
let diff =
pre_slide_translation_remaining.norm() - translation_remaining.norm();
if let Some((normalized_dir, _)) =
// threshold has to be > subtract_hit hardcoded correction, if not, we get made up movement in the direction of the hit normal
UnitVector::try_new_and_get(translation_remaining, 1.0e-4)
{
// reapply the lost speed (but in the corrected direction)
translation_remaining += *normalized_dir * diff;
}
}
} else {
// No interference along the path.
result.translation += translation_remaining;
Expand Down

0 comments on commit 0c8a7c0

Please sign in to comment.