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.
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.
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);