Disallow goin up/down upside-down stairs

This commit is contained in:
Jan Nils Ferner
2023-01-26 21:46:29 +01:00
parent 9a0757aeae
commit d5946f623b

View File

@@ -247,9 +247,8 @@ impl KinematicCharacterController {
toi, toi,
}); });
if ![self.up, -self.up] // Try to move up or down stairs // Try to go up stairs.
.iter() if !self.handle_stairs(
.any(|up| self.handle_stairs(
bodies, bodies,
colliders, colliders,
queries, queries,
@@ -260,15 +259,14 @@ impl KinematicCharacterController {
handle, handle,
&mut translation_remaining, &mut translation_remaining,
&mut result, &mut result,
up ) {
)) {
// No stairs, try to move along slopes. // No stairs, try to move along slopes.
if let Some(translation_on_slope) = if let Some(translation_on_slope) =
self.handle_slopes(&toi, &mut translation_remaining, offset) self.handle_slopes(&toi, &mut translation_remaining, offset)
{ {
translation_remaining = translation_on_slope; translation_remaining = translation_on_slope;
} else { } else {
// No slopes or stairs ahead; try to move along obstacles. // No slopes or stairs ahead; try to move along obstacle.
let allowed_translation = subtract_hit(translation_remaining, &toi, offset); let allowed_translation = subtract_hit(translation_remaining, &toi, offset);
result.translation += allowed_translation; result.translation += allowed_translation;
translation_remaining -= allowed_translation; translation_remaining -= allowed_translation;
@@ -525,7 +523,6 @@ impl KinematicCharacterController {
stair_handle: ColliderHandle, stair_handle: ColliderHandle,
translation_remaining: &mut Vector<Real>, translation_remaining: &mut Vector<Real>,
result: &mut EffectiveCharacterMovement, result: &mut EffectiveCharacterMovement,
stair_dir: &Vector<Real>,
) -> bool { ) -> bool {
let autostep = match self.autostep { let autostep = match self.autostep {
Some(autostep) => autostep, Some(autostep) => autostep,
@@ -549,9 +546,9 @@ impl KinematicCharacterController {
filter.flags |= QueryFilterFlags::EXCLUDE_DYNAMIC; filter.flags |= QueryFilterFlags::EXCLUDE_DYNAMIC;
} }
let shifted_character_pos = Translation::from(*stair_dir * max_height) * character_pos; let shifted_character_pos = Translation::from(*self.up * max_height) * character_pos;
let horizontal_dir = match (*translation_remaining - *stair_dir * translation_remaining.dot(&stair_dir)) let horizontal_dir = match (*translation_remaining - *self.up * translation_remaining.dot(&self.up))
.try_normalize(1.0e-5) { .try_normalize(1.0e-5) {
Some(dir) => dir, Some(dir) => dir,
None => return false, None => return false,
@@ -562,7 +559,7 @@ impl KinematicCharacterController {
bodies, bodies,
colliders, colliders,
character_pos, character_pos,
&stair_dir, &self.up,
character_shape, character_shape,
max_height, max_height,
false, false,
@@ -598,7 +595,7 @@ impl KinematicCharacterController {
bodies, bodies,
colliders, colliders,
&(Translation::from(horizontal_dir * min_width) * shifted_character_pos), &(Translation::from(horizontal_dir * min_width) * shifted_character_pos),
&-stair_dir, &-self.up,
character_shape, character_shape,
max_height, max_height,
false, false,
@@ -609,8 +606,8 @@ impl KinematicCharacterController {
.map(|remaining| subtract_hit(remaining, &hit, offset)); .map(|remaining| subtract_hit(remaining, &hit, offset));
let angle_with_floor = stair_dir.angle(&hit.normal1); let angle_with_floor = self.up.angle(&hit.normal1);
let climbing = stair_dir.dot(&horizontal_slope_translation) >= 0.0; let climbing = self.up.dot(&horizontal_slope_translation) >= 0.0;
if climbing && angle_with_floor > self.max_slope_climb_angle { if climbing && angle_with_floor > self.max_slope_climb_angle {
return false; // The target ramp is too steep. return false; // The target ramp is too steep.
@@ -625,7 +622,7 @@ impl KinematicCharacterController {
colliders, colliders,
&(Translation::from(horizontal_dir * min_width) &(Translation::from(horizontal_dir * min_width)
* shifted_character_pos), * shifted_character_pos),
&-stair_dir, &-self.up,
character_shape, character_shape,
max_height, max_height,
false, false,
@@ -636,7 +633,7 @@ impl KinematicCharacterController {
// Remove the step height from the vertical part of the self. // Remove the step height from the vertical part of the self.
*translation_remaining -= *translation_remaining -=
*stair_dir * ((translation_remaining.dot(&stair_dir)).clamp(0.0, step_height) + offset); *self.up * ((translation_remaining.dot(&self.up)).clamp(0.0, step_height) + offset);
// Advance the collider on the step horizontally, to make sure further // Advance the collider on the step horizontally, to make sure further
// movement wont just get stuck on its edge. // movement wont just get stuck on its edge.
@@ -644,7 +641,7 @@ impl KinematicCharacterController {
horizontal_dir * min_width.min(horizontal_dir.dot(translation_remaining)); horizontal_dir * min_width.min(horizontal_dir.dot(translation_remaining));
*translation_remaining -= horizontal_nudge; *translation_remaining -= horizontal_nudge;
result.translation += *stair_dir * step_height + horizontal_nudge; result.translation += *self.up * step_height + horizontal_nudge;
return true; return true;
} }