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 reapply_lost_speed to KinematicCharacterController #569

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/control/character_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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? (disabled by default)
///
/// Note that autostepping is currently a very computationally expensive feature, so it
Expand Down Expand Up @@ -160,6 +163,7 @@ impl Default for KinematicCharacterController {
offset: CharacterLength::Relative(0.01),
slide: true,
autostep: None,
reapply_lost_speed: false,
max_slope_climb_angle: Real::frac_pi_4(),
min_slope_slide_angle: Real::frac_pi_4(),
snap_to_ground: Some(CharacterLength::Relative(0.2)),
Expand Down Expand Up @@ -290,7 +294,7 @@ impl KinematicCharacterController {
});

let hit_info = self.compute_hit_info(hit);

let pre_slide_translation_remaining = translation_remaining;
// Try to go upstairs.
if !self.handle_stairs(
bodies,
Expand All @@ -314,6 +318,17 @@ impl KinematicCharacterController {
&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
Loading