From dd195a47203ec4d9540f1a656ac53859372d353f Mon Sep 17 00:00:00 2001 From: Jan Hohenheim Date: Thu, 9 Feb 2023 00:56:16 +0100 Subject: Fix horizontal jittering along walls in 3D --- src/control/character_controller.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/control') diff --git a/src/control/character_controller.rs b/src/control/character_controller.rs index 66d2d11..0de897b 100644 --- a/src/control/character_controller.rs +++ b/src/control/character_controller.rs @@ -259,12 +259,12 @@ impl KinematicCharacterController { ) { // No stairs, try to move along slopes. if let Some(translation_on_slope) = - self.handle_slopes(&toi, &translation_remaining, offset) + self.handle_slopes(&toi, &translation_remaining) { translation_remaining = translation_on_slope; } else if allowed_dist.abs() < 1.0e5 { // No slopes or stairs ahead, but we didn't move yet; try to move along obstacle. - let allowed_translation = subtract_hit(translation_remaining, &toi, offset); + let allowed_translation = subtract_hit(translation_remaining, &toi); result.translation += allowed_translation; translation_remaining -= allowed_translation; } @@ -485,13 +485,12 @@ impl KinematicCharacterController { &self, hit: &TOI, translation_remaining: &Vector, - offset: Real, ) -> Option> { let [vertical_translation, horizontal_translation] = self.split_into_components(translation_remaining); let [vertical_slope_translation, horizontal_slope_translation] = [vertical_translation, horizontal_translation] - .map(|remaining| subtract_hit(remaining, hit, offset)); + .map(|remaining| subtract_hit(remaining, hit)); let slope_translation = horizontal_slope_translation + vertical_slope_translation; // Check if there is a slope to climb. @@ -618,7 +617,7 @@ impl KinematicCharacterController { ) { let [vertical_slope_translation, horizontal_slope_translation] = self .split_into_components(translation_remaining) - .map(|remaining| subtract_hit(remaining, &hit, offset)); + .map(|remaining| subtract_hit(remaining, &hit)); let slope_translation = horizontal_slope_translation + vertical_slope_translation; @@ -745,8 +744,9 @@ impl KinematicCharacterController { } } -fn subtract_hit(translation: Vector, hit: &TOI, offset: Real) -> Vector { - let surface_correction = translation.dot(&hit.normal1).abs(); - let surface_correction = surface_correction + offset; +fn subtract_hit(translation: Vector, hit: &TOI) -> Vector { + let surface_correction = (-translation).dot(&hit.normal1).max(0.0); + // This fixes some instances of moving through walls + let surface_correction = surface_correction * (1.0 + 1.0e-5); translation + *hit.normal1 * surface_correction } -- cgit