Differences between setSize/setOffset methods in Arcade Static Body and Dynamic Body

I was wondering why there’s a difference between the setSize and setOffset methods of dynamic and static bodies.

I was using a dynamic body on a sprite and figure I could try a static body since my sprite will not be moving, just colliding/overlapping. An then I run into these differences, for example my sprite is a coin (sort of) my player would collect, Im reducing the size of the body because it feels better than leaving it at the default (taking the size of the gameobject). Everything works as expected if I use a dynamic body, the setSize() method will reduce the body size and set the body’s center to coincide with the gameobject center:

39

But when I make it a static body , and set the size of the body, it will not align with with the center of the gameobject even when leaving the offsetx and offsety parameter of the function to the default values. It will align to the top bottom of the gameobject’s frame.

30

I have to center the static body manually with something like:

// Within a Phaser.Physics.Arcade.Sprite    
this.body.setSize(15, 15);

const dw = this.displayWidth / 2;
const dh = this.displayHeight / 2;

this.body.setOffset(dw - this.body.halfWidth, dh - this.body.halfHeight);

Any thoughts on why is this? Is it by design for some reason?

EDIT: I should add the setSize method has different signature in dynamic body than the static body.

// In Phaser.Physics.Arcade.StaticBody
setSize: function (width, height, offsetX, offsetY)

// In Phaser.Physics.Arcade.Body
setSize: function (width, height, center)

I had a similar problem. If you look at StaticBody#setOffset it looks like offset is used only to adjust position, once. The only reliable method I found is to zero offset and then set position manually.

I should add the setSize method has different signature in dynamic body than the static body.

2 Likes

I should have taken a look to the issues before posting. Yes, it seems that the only reliable way is to position the body manually, which IMHO reduces the consistency of the API.
Thanks for your answer!

No worries, you’d have to read a bit farther into that issue to find it.