pmndrs / react-three-rapier

🤺 Rapier physics in React
https://react-three-rapier.pmnd.rs
MIT License
1.08k stars 60 forks source link

Physics event to know "steady state" #620

Open rmrbytes opened 9 months ago

rmrbytes commented 9 months ago

Hello:

I wish to know if there are events related to the Physics API?

The game that I am creating is: permitting the user to move a ball (using Leva controls) to some position and then releasing it. During movement of the ball, physics is paused and once placed the user releases the ball (causing the physics to unpause). I need to know when all the rigid bodies in the scene have completed their "movements" so that the user can once again move the ball to another position.

For the same I was hoping to have an event that perhaps can "call" that all objects are stationary. Using useframe hampers smoothness.

Thanks.

isaac-mason commented 5 months ago

Internally @react-three/rapier uses world.forEachActiveRigidBody to invalidate frames when there are active / non-sleeping bodies.

Maybe this is better:

let allBodiesSleeping = true;
for (const body of world.bodies.getAll()) {
  if (!body.isSleeping()) {
    allBodiesSleeping = false;
    break;
  }
}

https://rapier.rs/docs/user_guides/javascript/rigid_bodies/#sleeping