Sleep System

*เนื้อหานี้จะพร้อมใช้งานในภาษาที่คุณเลือกในเร็วๆ นี้

Each assembly in the Roblox engine corresponds to a single rigid body. The position and velocity of each rigid body describe where it's located and how fast it's moving, and one of the primary engine tasks is to update the positions and velocities of each assembly.

Assemblies can be connected together with mechanical constraints and mover constraints to form mechanisms such as cars or airplanes. As the number of assemblies and constraints in a mechanism increases, the time required to simulate the mechanism also increases. Fortunately, this increase is offset when the sleep system determines that the engine can skip simulation of non‑moving assemblies.

Sleep States

Each assembly can be in one of three states: awake, sleep‑checking, or sleeping.

Awake

An awake assembly is moving or accelerating, and is therefore simulated. Assemblies enter this state from situations outlined in sleep‑checking and sleeping, as well as additional wake situations.

Sleep-Checking

A non-moving assembly that shares a constraint with at least one awake neighboring assembly is put into the sleep-checking state and is not simulated. On each worldstep, a sleep-checking assembly checks whether:

If either of these conditions is true, or under any of the additional wake situations, the sleep-checking assembly enters the awake state.

Sleeping

A sleeping assembly is neither moving nor accelerating, and is therefore not simulated.

An assembly is determined to be non-moving by checking its position deviation, calculated as the maximum deviation from the average position of the point furthest from its center of mass over the most recent set of worldsteps. If this deviation is greater than the Displacement threshold, the assembly enters the awake state.

In some cases, simply checking for non-movement would cause an assembly to incorrectly enter the sleeping state. For example, if a ball is thrown straight up, its position barely changes for a number of worldsteps as it approaches its maximum height, making it a candidate to sleep and never fall back down. To handle such cases, the engine monitors whether the product of the assembly's acceleration and current timestep size exceeds the Linear Velocity and/or Angular Velocity threshold.

Threshold Reference

The following table provides the various displacement and velocity thresholds used to determine if an assembly is moving or accelerating.

ThresholdValueState Change
Neighbor Displacement0.01 studssleep-checkingawake
Neighbor Linear Velocity0.2 studs/ssleep-checkingawake
Neighbor Angular Velocity0.2 radians/ssleep-checkingawake
Displacement0.001 studssleepingawake
Linear Velocity0.1 studs/ssleepingawake
Angular Velocity0.1 radians/ssleepingawake

Additional Wake Situations

In addition to situations outlined in sleep‑checking and sleeping, an assembly enters the awake state when:

Debugging Visualization

During playtesting, you can visualize assembly sleep states by toggling on Awake parts from the Visualization Options widget in the upper‑right corner of the 3D viewport.

A close up view of the 3D viewport with the Visualization Options button indicated in the upper-right corner.

Once enabled, simulated parts will be outlined by their current sleep state, with awake parts outlined in red, sleep‑checking parts outlined in orange, and sleeping parts un‑outlined.

Simulated parts outlined by the color representing their current sleep state