Toggle Matter physics on a specific object

I’m trying to figure out how you are supposed to enable/disable Matter on a physics-enabled object.

So far I’ve tried using sprite.body.enable = false, which does not appear to have any effect.

Does anyone know how you are supposed to do this?

Can you tell us more about the problem? Specifically, what happens when you try sprite.body.enable = false vs sprite.body.enable = true? If nothing happens can you turn on debug? If still nothing happens then tell us.

The behavior is identical whether enable is true or false. The object behaves as though physics is enabled.

I have turned on debug and I can see that the object still shows its physics body even though sprite.body.enable is set to false. I have confirmed that its value remains at false by printing out the value in the update loop.

I am getting the same behavior. I will try to see if I can get anything else to work.

Just to make sure, you want to be able to toggle on and off collision but still have the display show the gameObject?

If that’s true you can use:

// toggle off
this.sprite.setCollisionCategory(null);
// toggle on
this.sprite.setCollisionCategory(1);

Assuming you haven’t changed the collision category of any of your objects.

Edit:
Here is the example I wrote.

I want all physics disabled, not just collisions.

Let me explain what I’m trying to do in case this is an XY problem. I’m trying to make it so that the user can click on an object and drag it with their mouse. At the moment, this doesn’t work properly, because as soon as they stop moving their mouse physics takes over and the object immediately falls to the ground.

I don’t want to use mouseSpring because then the object preserves things like angular velocity, etc. I essentially just want to turn the object into a kinematic body which I have full control over (and then be able to toggle it back when they release the mouse).

It sounds like your solution will purely disable collision on the object. I don’t think that’s enough in my case because of gravity. I could try and disable gravity on this specific object but I think it would be easier to just disable physics for it altogether.

I would just toggle gravity too. It might not be the best way to do it, but it will work. If somebody else knows a better way to do this, I invite them to answer this, meaning you shouldn’t mark this as solved.

sprite.setIgnoreGravity(true);

Thanks for all your help. This is the code I’m now using, in case someone comes along later looking for a workaround.

function setPhysicsOn(sprite, val = true) {
    sprite.body.enable = val;
    sprite.setCollisionCategory(val ? 1 : null);
    sprite.setIgnoreGravity(!val);
    if(!val) {
        sprite.setAngularVelocity(0);
        sprite.setVelocity(0, 0);
    }
}

this.input.on('dragstart', (pointer, gameObject) => {
    setPhysicsOn(gameObject, false);
});
this.input.on('drag', (pointer, gameObject, dragX, dragY) => {
    gameObject.x = dragX;
    gameObject.y = dragY;
    setPhysicsOn(gameObject, false);
});

this.input.on('dragend', (pointer, gameObject) => {
    setPhysicsOn(gameObject, true);
});

It’s definitely not the cleanest code I’ve written but it behaves more or less the way I would want.

2 Likes