Skip to content

Allow Forces to wake or not wake up bodies#783

Merged
Jondolf merged 7 commits into
mainfrom
force-waking
Jul 19, 2025
Merged

Allow Forces to wake or not wake up bodies#783
Jondolf merged 7 commits into
mainfrom
force-waking

Conversation

@Jondolf

@Jondolf Jondolf commented Jul 19, 2025

Copy link
Copy Markdown
Member

Objective

#770 added a Forces helper QueryData for applying forces, impulses, and accelerations to dynamic bodies. The intent was that forces would wake up bodies by default, but this was actually not the case, as Forces has some components that sleeping bodies don't.

However, we should also have a way to allow forces not to wake up bodies. This can be important for optimization in some cases.

Solution

Rework Forces to wake up bodies by default when applying non-zero forces, impulses, or accelerations. Additionally, add a ForcesItem::non_waking method that returns a NonWakingForcesItem. The two force types share a RigidBodyForces trait and the same API, but the former wakes up bodies while the latter doesn't.

The API looks like this:

// This does not wake up the body.
forces.non_waking().apply_force(force);

// This wakes up the body if it is sleeping.
forces.apply_force(force);

Many existing physics engines instead take a boolean argument in all force methods to control whether the force should wake the body up. However, I am of the opinion that sleeping should primarily be transparent to the user and just an internal optimization, and that intuitively you would expect a force or impulse to always have an effect on a body by default. Disabling waking for a force should be a more advanced use case, so it should not affect the common API.

Game engines like Unity and Godot actually seem to always wake up bodies for non-zero forces, with no option to configure this. I took the middle route, defaulting to waking up, but having an additional non_waking method to opt in to the non-waking behavior.

I considered some component-driven approaches like a ForceWakingMode component, but I think this needs to be easily configurable per force and not a persisted per-entity setting.

@Jondolf Jondolf added this to the 0.4 milestone Jul 19, 2025
@Jondolf Jondolf added C-Feature A new feature, making something new possible A-Dynamics Relates to rigid body dynamics: motion, mass, constraint solving, joints, CCD, and so on C-Usability A quality-of-life improvement that makes Avian easier to use labels Jul 19, 2025
@Jondolf Jondolf enabled auto-merge (squash) July 19, 2025 21:16
@Jondolf Jondolf merged commit 18648b1 into main Jul 19, 2025
6 checks passed
@Jondolf Jondolf deleted the force-waking branch July 21, 2025 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Dynamics Relates to rigid body dynamics: motion, mass, constraint solving, joints, CCD, and so on C-Feature A new feature, making something new possible C-Usability A quality-of-life improvement that makes Avian easier to use

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant