@@ -105,6 +105,9 @@ pub struct KinematicCharacterController {
105105 pub offset : CharacterLength ,
106106 /// Should the character try to slide against the floor if it hits it?
107107 pub slide : bool ,
108+ /// Should the speed lost because of collisions be applied to the final direction ?
109+ /// Useless if sliding is not enabled
110+ pub reapply_lost_speed : bool ,
108111 /// Should the character automatically step over small obstacles?
109112 pub autostep : Option < CharacterAutostep > ,
110113 /// The maximum angle (radians) between the floor’s normal and the `up` vector that the
@@ -124,6 +127,7 @@ impl Default for KinematicCharacterController {
124127 up : Vector :: y_axis ( ) ,
125128 offset : CharacterLength :: Relative ( 0.01 ) ,
126129 slide : true ,
130+ reapply_lost_speed : false ,
127131 autostep : Some ( CharacterAutostep :: default ( ) ) ,
128132 max_slope_climb_angle : Real :: frac_pi_4 ( ) ,
129133 min_slope_slide_angle : Real :: frac_pi_4 ( ) ,
@@ -250,6 +254,7 @@ impl KinematicCharacterController {
250254 toi,
251255 } ) ;
252256
257+ let pre_slide_translation_remaining = translation_remaining;
253258 // Try to go up stairs.
254259 if !self . handle_stairs (
255260 bodies,
@@ -267,6 +272,17 @@ impl KinematicCharacterController {
267272 translation_remaining =
268273 self . handle_slopes ( & toi, & translation_remaining, & mut result) ;
269274 }
275+ if self . slide && self . reapply_lost_speed {
276+ let diff =
277+ pre_slide_translation_remaining. norm ( ) - translation_remaining. norm ( ) ;
278+ if let Some ( ( normalized_dir, _) ) =
279+ // threshold has to be > subtract_hit hardcoded correction, if not, we get made up movement in the direction of the hit normal
280+ UnitVector :: try_new_and_get ( translation_remaining, 1.0e-4 )
281+ {
282+ // reapply the lost speed (but in the corrected direction)
283+ translation_remaining += * normalized_dir * diff;
284+ }
285+ }
270286 } else {
271287 // No interference along the path.
272288 result. translation += translation_remaining;
0 commit comments