[Phaser 3] Modifying collision box

Happy Friday everyone. So I’m a bit foggy on modifying the collision box for my ground platform. I found the documentation on the site for adjusting the collision box but I would like to understand it more so I don’t keep putting random numbers in hoping it works. With the code below, I know that the setSize method adjusts the body but I don’t think I fully understand each value. Also setting body.velocity.x and body.immovable, is that part of the modified collision box?

//  This adjusts the collision body size to be a 100x50 box.
//  50, 25 is the X and Y offset of the newly sized box.

sprite1.body.setSize(100, 50, 50, 25);
sprite1.body.immovable = true;

sprite2.body.velocity.x = -100;

Is this a static body?

It is.

this.platforms = this.physics.add.staticGroup();

this.ground = this.platforms.create(400, 590, "grassGround").setScale(1).refreshBody();

Offsetting a static body after it’s created is peculiar (I think there’s another thread on this).

But if you want your platform to move, don’t use a static body/group. Just use something like

this.ground = this.physics.add.image(400, 590, 'grassGround');

Set { arcade: { debug: true } } to see all the boxes.

I don’t want it to move, this particular platform is static (my ground platform). The problem is that my art work for the platform (because of how I have my grass implemented) makes the objects hover slightly above the ground platform by about 5 pixels. So I want to reduce the collision box so that my objects will appear to be actually on the platform.

Avoid the offset arguments, use just setPosition() and setSize().

2 Likes

@samme thank you for the help. I used this and it worked great. Thanks for the documentation resources for this. This still feels like a somewhat clunky way to adjust collision boxes just because I have to change the collision box on each object and it doesn’t let me target an entire group which would be ideal. Especially when my physics group objects are all the same size. but it works!

Thanks again.