by Julio Jerez » Mon Jan 10, 2022 10:18 am
Bodies always go to sleep as a result of calculation by the solver.
When a body is at rest, meaning net acceleration and net veloc is below the sleep threshold.
But this condition alone is not enought to put a body to sleep. Because it will put bodies in unstable equlibrium to sleep as well.
There are three type of equilibrium in mechanic.
Stable, unstable and indifferent.
Stable is when a small perturbation of the position causes the body to gain potential energy, for us this means the center of mass will go up. Think of a ball rolling on a valley, any movement will cause the ball go uphill.
Unstable is when a small perturbation causes the body to lose potential energy, ex a ball on the top of a mountain, any movement will make the ball goes down hill
Indifferent when the small perturbation does not affect the potential energy. Like a ball rolling on a flat plane.
What we want, is for bodies to go to sleep when they are in stable equilibrium.
But the conditions for stable and unstable equilibrium, are identical if we only consider one instant in time.
The key word is the small perturbation, which translate to time. We need to some how introduce the small perturbation to discriminate between stable and unstable equibrium.
Now adding a small perturbation, is not as eassy as it sounds, this implies solving the system one instance in time with the same inputs. But that is prohibitive.
Imagine a cube on an edge with the center of mass falling exactly on the edge. That's a typical case of unstable equilibrium.
This is possible in nature but very unliketly, but in a simulation, the acceleration and velocity are not tested against zero, they are checked against a threshold, so the likelihood of unstable equibrium is far higher and that has to be avoid it.
So what we want is to discriminate from stable, and unstable equilibrium.
For that, we introduce the concepts of islands.
An Island is a group of connected bodies by joints.
Now with this, we can store time Information in the island,
And we can represent the small perturbation, by simply counting how many frames the same island is in equilibrium.
Only island that are in stable equilibrium will survived for a few frames.
So now the condition for going to sleep are.
Acceleration bellow some threshold
Velocity below some threshold,
And the same condition met for certain number of frames.
If all thre are met, then the Intire island goes to sleep.
And now we can add some heuristic, like big islands relax the condition more than small islands.
You also has an interface to set or rest the flag if individual bodies.
So let us say you have a game play object, and you want the to be active even if it is in equilibrium.
This is why you can see bodies in a large array can go to sleep but the island does not.
Them there is the auto sleep flag, which is very simple.
When the final sleep flag for a body is determined.
It does not just set that flag, it set that flag via an and operator with auto sleep flag.
So what this mean is that the body can only sleep when
Auto sleep is true.