1
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ Use mesh.position to find it's position - check if it changes, if not - it's still. \$\endgroup\$ Oct 6, 2015 at 11:22

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Just now got to test it, works well! \$\endgroup\$
    – eddyuk
    Oct 17, 2015 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .