Phaser API Documentation

alignBody(body, x, y, align)


Aligns a Body, or Matter Game Object, against the given coordinates.

The alignment takes place using the body bounds, which take into consideration things like body scale and rotation.

Although a Body has a position property, it is based on the center of mass for the body, not a dimension based center. This makes aligning bodies difficult, especially if they have rotated or scaled. This method will derive the correct position based on the body bounds and its center of mass offset, in order to align the body with the given coordinate.

For example, if you wanted to align a body so it sat in the bottom-center of the Scene, and the world was 800 x 600 in size:

this.matter.alignBody(body, 400, 600, Phaser.Display.Align.BOTTOM_CENTER);

You pass in 400 for the x coordinate, because that is the center of the world, and 600 for the y coordinate, as that is the base of the world.


name type description
body Phaser.Types.Physics.Matter.MatterBody

The Body to align.

x number

The horizontal position to align the body to.

y number

The vertical position to align the body to.

align number

One of the Phaser.Display.Align constants, such as Phaser.Display.Align.TOP_LEFT.


This Matter Physics instance.

Since: 3.22.0