Matter.js, change body shape

Hello,

I’m using matter js as my physics engine instead of arcade and I’m creating my matter js sprite like so:

sprite = game.matter.add.sprite(x, y, 'player', '', {
    'shape': playerStandShapeJson['player-00-00'],
})

Where playerStandShapeJson is the exported shape from PhysicsEditor.

This works perfectly, however my problem is that when the player presses a key I want to change the size of the collision area to be a bit smaller. I’ve already exported the smaller shape from PhysicsEditor but I’m not sure how to tell the sprite to change it’s shape to this new shape.

Summary: I’m creating a sprite with a certain shape and after creation I would like it to change it’s shape to a different shape.

Thanks in advance.

How can you solve it?
thanks

@Sanchez3 Unfortunately I haven’t been able to change the shape of a body after creation yet.

I do hope somebody would help us figure this out though.

I try this way:

var sx = sprite.x;
var sy = sprite.y;
var sav = sprite.body.angularVelocity;
var sv = sprite.body.velocity;
sprite.setBody(shape);
sprite.setPosition(sx, sy);
sprite.setVelocity(sv.x, sv.y);
sprite.setAngularVelocity(sav);

I use the Tile map editor to create my shapes, and when importing to phaser I use the shapes to create bodies using the matter.bodies.rectangle/circle/polygon methods.

Example for rectangle

let newBody =   matter.bodies.rectangle(
    x,
    y,
   width,
    height,
    {
        isStatic: false,
        isSensor: false
    }
);

matterGameObject.setExistingBody(newBody, true);

Do not know if there is a builtin way to use the json directly, but you can create a method that takes the json data you want to make the new body and assign it like shown in the example. Then a callback or other type of check to know when to use what body for what frame.

I’ve finally got this working by the way.

I create my sprite like:

var x           = 100;
var y           = 100;
var width       = 40;
var height      = 60;
var sprite      = game.matter.add.sprite(x, y, 'player');
var sprite_body = Bodies.rectangle(x, y, width, height);
sprite.setExistingBody(sprite_body);

Using sprite.setExistingBody.

Then I resize with:

Body.scale(sprite_body, 0.5, 0.5);