Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sleeping #32

Merged
merged 17 commits into from
Jun 9, 2023
Merged

Implement sleeping #32

merged 17 commits into from
Jun 9, 2023

Conversation

Jondolf
Copy link
Owner

@Jondolf Jondolf commented Jun 8, 2023

Depends on #31.

Description

Implements sleeping to improve performance and reduce small jitter.

Bodies with the Sleeping component are not simulated by the physics engine. Bodies are marked with the component when their linear and angular velocities are below the values of the SleepingThreshold resource for a duration indicated by the DeactivationTime resource.

The TimeSleeping component is used to track the time that the velocity has been below the threshold, and it is reset to 0 whenever the threshold is exceeded.

Bodies are woken up when they interact with active dynamic or kinematic bodies or joints, or when gravity changes, or when the body's position, rotation, velocity, or external forces are changed.

Sleeping can be disabled for all bodies with a negative sleeping threshold, and for specific entities with the SleepingDisabled component.

Examples

Below is the current behaviour in a couple examples. The red color indicates that a body is sleeping and isn't being simulated.

First, the cubes example. At the end I change the velocity of the boxes. Sleeping doesn't help keep the cube stack stable unless a really high sleeping threshold is specified, but it does help keep them still on the ground.

2023-06-08.13-59-25.mp4

move_marbles example:

2023-06-08.14-38-15.mp4

Considerations and future improvements

  • As can be seen in the cubes example, sleeping doesn't fix jittery collisions (Unstable collisions #2). However, it does help keep slowly drifting bodies still, so in many cases it might be enough until a proper fix for unstable collisions is found.
  • The default SleepingThreshold should perhaps be scaled depending on the world scale, e.g. in 2D, objects might be hundreds of pixels wide, so a threshold of 0.1 would be tiny. Maybe we should introduce a PhysicsScale resource, similar to rapier?
  • Sleeping can cause unrealistic behaviour in some cases, e.g. removing the floor under a sleeping body will leave the body floating. But AFAIK this is a thing in other engines as well, and it should probably just be mentioned in the docs somewhere.
  • Currently, sleeping is done per body. Many physics engines seem to use an island architecture where entities are grouped into islands, and sleeping controls the entire islands. This might have some benefits that are worth considering later.

Todo

  • Wake up bodies when their position or rotation is changed (constraints waking up bodies was bugged, now things work)
  • Figure out better place for sleeping systems than the sync step
    • I added a separate step and plugin for sleeping since I couldn't come up with another logical place.
  • Allow disabling sleeping for specific entities using a component
  • Allow separate sleeping threshold for linear and angular velocity
  • Add some more documentation

@Jondolf Jondolf added the C-Enhancement New feature or request label Jun 8, 2023
@Jondolf
Copy link
Owner Author

Jondolf commented Jun 9, 2023

Everything should be working and documented now. I'll merge this, feel free to open issues if you encounter any problems.

@Jondolf Jondolf merged commit eaacdfa into main Jun 9, 2023
@Jondolf Jondolf deleted the sleeping branch June 9, 2023 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant