Try to fix false negatives on grounded

This commit is contained in:
Jan Nils Ferner
2023-01-28 00:52:16 +01:00
parent e3fb4f7135
commit f1c8fdc1ec

View File

@@ -193,8 +193,8 @@ impl KinematicCharacterController {
let mut translation_remaining = desired_translation; let mut translation_remaining = desired_translation;
// Apply friction // Check if we are grounded at the initial position.
self.detect_grounded_status_and_apply_friction( let grounded_at_starting_pos = self.detect_grounded_status_and_apply_friction(
dt, dt,
bodies, bodies,
colliders, colliders,
@@ -299,17 +299,18 @@ impl KinematicCharacterController {
} }
// If needed, and if we are not already grounded, snap to the ground. // If needed, and if we are not already grounded, snap to the ground.
self.snap_to_ground( if grounded_at_starting_pos {
bodies, self.snap_to_ground(
colliders, bodies,
queries, colliders,
character_shape, queries,
&(Translation::from(result.translation) * character_pos), character_shape,
&dims, &(Translation::from(result.translation) * character_pos),
filter, &dims,
&mut result, filter,
); &mut result,
);
}
// Return the result. // Return the result.
result result
@@ -381,7 +382,7 @@ impl KinematicCharacterController {
.compute_aabb(character_pos) .compute_aabb(character_pos)
.loosened(prediction); .loosened(prediction);
let mut grounded = false; let mut grounded = true;
queries.colliders_with_aabb_intersecting_aabb(&character_aabb, |handle| { queries.colliders_with_aabb_intersecting_aabb(&character_aabb, |handle| {
if let Some(collider) = colliders.get(*handle) { if let Some(collider) = colliders.get(*handle) {
@@ -409,6 +410,9 @@ impl KinematicCharacterController {
for m in &manifolds { for m in &manifolds {
let normal = -(character_pos * m.local_n1); let normal = -(character_pos * m.local_n1);
if normal.dot(&self.up) <= 1.0e-5 {
grounded = false;
}
if let Some(kinematic_parent) = kinematic_parent { if let Some(kinematic_parent) = kinematic_parent {
let mut num_active_contacts = 0; let mut num_active_contacts = 0;
let mut manifold_center = Point::origin(); let mut manifold_center = Point::origin();
@@ -428,9 +432,6 @@ impl KinematicCharacterController {
*translation_remaining += normal *translation_remaining += normal
* (normal_target_mvt - normal_current_mvt); * (normal_target_mvt - normal_current_mvt);
if normal.dot(&self.up) <= 1.0e-5 {
grounded = true;
}
} }
} }
@@ -458,13 +459,9 @@ impl KinematicCharacterController {
for m in &manifolds { for m in &manifolds {
let normal = character_pos * m.local_n1; let normal = character_pos * m.local_n1;
if normal.dot(&self.up) <= 1.0e-5 { if normal.dot(&self.up) >= -1.0e-5 {
for contact in &m.points { grounded = false;
if contact.dist <= prediction { return false; // We can stop the search early.
grounded = true;
return false; // We can stop the search early.
}
}
} }
} }
} }