pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber
https://cannon.pmnd.rs
2.76k stars 154 forks source link

Add 'shouldInvalidate' To Allow Pausing #226

Closed aunyks closed 3 years ago

aunyks commented 3 years ago

Addressing #212, this lets the developer control when the simulation steps. By default, this doesn't change the behavior of <Physics/>, but with shouldInvalidate set to false a new step is triggered when props change or when invalidate() is called somewhere in the tree.

stockhuman commented 3 years ago

Thanks for this, though unless I'm misunderstanding what you're describing the intended effect to be, this does not pause the physics simulation.

aunyks commented 3 years ago

Maybe I don't yet understand how useFrame works. It's my understanding that in Provider.tsx, the loop function is called on every frame tick managed by Fiber. Then, on Worker response the Provider requests / triggers a new frame by calling invalidate, assuming the Canvas frameloop prop is set to 'demand'.

Assuming that's true, shouldInvalidate being false will prevent that invalidate() call from automatically happening, which pauses steps in the Physics simulation until another component calls invalidate().

Is that correct? Thanks for the response, by the way! I'm new to this ecosystem.

stockhuman commented 3 years ago

Thanks @aunyks, and no, it was I who was mistaken as to what you were implementing, and I see now why you named it shouldInvalidate over something relating to explicit pausing. I wonder if someone may somehow want to invalidate but not step physics, but we can cross that bridge when we get there.

If you were building something and have a cool demo to show off, you're more than welcome to add that to this, else, we'll merge and I'll integrate this functionality into one of the more feature-lacking demos.