fix regressions with sleeping behavior of kinematic bodies (#885)

* fix kinematic bodies ignoring the wake_up flag when setting velocities

* fix: don’t allow kinematic bodies to fall asleep unless they velocities are at zero exactly.

* feat: add debug example for kinematic bodies sleep

* chore: update changelog

* chore: typo
This commit is contained in:
Sébastien Crozet
2025-10-17 12:22:23 +02:00
committed by GitHub
parent ae1d479857
commit 27b11b9d61
5 changed files with 129 additions and 18 deletions

View File

@@ -159,7 +159,14 @@ impl IslandManager {
let sq_linvel = rb.vels.linvel.norm_squared();
let sq_angvel = rb.vels.angvel.gdot(rb.vels.angvel);
update_energy(length_unit, &mut rb.activation, sq_linvel, sq_angvel, dt);
update_energy(
&mut rb.activation,
rb.body_type,
length_unit,
sq_linvel,
sq_angvel,
dt,
);
if rb.activation.time_since_can_sleep >= rb.activation.time_until_sleep {
// Mark them as sleeping for now. This will
@@ -291,16 +298,28 @@ impl IslandManager {
}
fn update_energy(
length_unit: Real,
activation: &mut RigidBodyActivation,
body_type: RigidBodyType,
length_unit: Real,
sq_linvel: Real,
sq_angvel: Real,
dt: Real,
) {
let linear_threshold = activation.normalized_linear_threshold * length_unit;
if sq_linvel < linear_threshold * linear_threshold.abs()
&& sq_angvel < activation.angular_threshold * activation.angular_threshold.abs()
{
let can_sleep = match body_type {
RigidBodyType::Dynamic => {
let linear_threshold = activation.normalized_linear_threshold * length_unit;
sq_linvel < linear_threshold * linear_threshold.abs()
&& sq_angvel < activation.angular_threshold * activation.angular_threshold.abs()
}
RigidBodyType::KinematicPositionBased | RigidBodyType::KinematicVelocityBased => {
// Platforms only sleep if both velocities are exactly zero. If its not exactly
// zero, then the user really wants them to move.
sq_linvel == 0.0 && sq_angvel == 0.0
}
RigidBodyType::Fixed => true,
};
if can_sleep {
activation.time_since_can_sleep += dt;
} else {
activation.time_since_can_sleep = 0.0;

View File

@@ -769,15 +769,12 @@ impl RigidBody {
pub fn set_linvel(&mut self, linvel: Vector<Real>, wake_up: bool) {
if self.vels.linvel != linvel {
match self.body_type {
RigidBodyType::Dynamic => {
RigidBodyType::Dynamic | RigidBodyType::KinematicVelocityBased => {
self.vels.linvel = linvel;
if wake_up {
self.wake_up(true)
}
}
RigidBodyType::KinematicVelocityBased => {
self.vels.linvel = linvel;
}
RigidBodyType::Fixed | RigidBodyType::KinematicPositionBased => {}
}
}
@@ -791,15 +788,12 @@ impl RigidBody {
pub fn set_angvel(&mut self, angvel: Real, wake_up: bool) {
if self.vels.angvel != angvel {
match self.body_type {
RigidBodyType::Dynamic => {
RigidBodyType::Dynamic | RigidBodyType::KinematicVelocityBased => {
self.vels.angvel = angvel;
if wake_up {
self.wake_up(true)
}
}
RigidBodyType::KinematicVelocityBased => {
self.vels.angvel = angvel;
}
RigidBodyType::Fixed | RigidBodyType::KinematicPositionBased => {}
}
}
@@ -813,15 +807,12 @@ impl RigidBody {
pub fn set_angvel(&mut self, angvel: Vector<Real>, wake_up: bool) {
if self.vels.angvel != angvel {
match self.body_type {
RigidBodyType::Dynamic => {
RigidBodyType::Dynamic | RigidBodyType::KinematicVelocityBased => {
self.vels.angvel = angvel;
if wake_up {
self.wake_up(true)
}
}
RigidBodyType::KinematicVelocityBased => {
self.vels.angvel = angvel;
}
RigidBodyType::Fixed | RigidBodyType::KinematicPositionBased => {}
}
}