Search code examples
javascriptcanvasgame-physicsphysicsmatter.js

Matter JS Pause physics but still interact with objects


I'm looking to pause matter.js but still be able to manipulate objects. When I toggle runner.enabled, it disables all mouse constraints. I want to be able to pause the engine and move objects, then turn the physics back on. Any suggestions?


Solution

  • You'll want to toggle IsStatic on your bodies.

    To pause all objects you can run a for loop to set all your bodies.body.isStatic properties to True.

    Then, on click event mouseDown you can do this.body.isStatic = false; and on mouseUp you can do this.body.isStatic = true;

    This will allow you to manipulate your bodies and move them wherever you like until you do something like bodies.forEach(body) body.isStatic = false to allow your bodies to interact with your world again.

    Keep in mind this WILL retain physics properties, so you'll also want to reset the bodies velocity and momentum, etc, properties to what they were prior to the pause at some point. Either on unPause or on mouseUp.