I have a cylinder that simulates a flipping coin. I am using Physijs. The coin might bounce off the surface multiple times before stopping. How can I know when the coin has completely stopped moving?
1 Answer
On each update event, simply check that the coin's linear and angular velocities are very small.
var epsilon = 0.0001; // or any small enough value for your purposes
if ( coin.getLinearVelocity().lengthSq() < epsilon &&
coin.getAngularVelocity().lengthSq() < epsilon ) {
// stopped moving
} else {
// still moving
}
This approximation is often necessary because the velocities may not quite reach absolute 0
, due to simulation instability or floating-point errors, or it may take a long time to get there.
I used .lengthSq()
rather than .length()
to avoid a costly square root operation. When comparing to a small value, the difference in meaning is unimportant.