Skip to content

Commit 7730af6

Browse files
committed
Add better support for concave polygons to MatterTileBody
1 parent 578fe59 commit 7730af6

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

src/physics/matter-js/MatterTileBody.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ var MatterTileBody = new Class({
134134
* Note: Matter doesn't support all shapes from Tiled. Rectangles and polygons are directly
135135
* supported. Ellipses are converted into circle bodies. Polylines are treated as if they are
136136
* closed polygons. If a tile has multiple shapes, a multi-part body will be created. Concave
137-
* shapes are supported if poly-decomp library is included, but it's usually best to manually
138-
* decompose a concave polygon into multiple convex polygons.
137+
* shapes are supported if poly-decomp library is included. Decomposition is not guaranteed to
138+
* work for complex shapes (e.g. holes), so it's often best to manually decompose a concave
139+
* polygon into multiple convex polygons yourself.
139140
*
140141
* @method Phaser.Physics.Matter.TileBody#setFromTileCollision
141142
* @since 3.0.0
@@ -188,9 +189,17 @@ var MatterTileBody = new Class({
188189
});
189190
var vertices = Vertices.create(points);
190191

191-
// Translate from object position to center of mass
192-
var center = Vertices.centre(vertices);
193-
body = Bodies.fromVertices(ox + center.x, oy + center.y, vertices, options);
192+
// Points are relative to the object's origin (first point placed in Tiled), but
193+
// matter expects points to be relative to the center of mass. This only applies to
194+
// convex shapes. When a concave shape is decomposed, multiple parts are created and
195+
// the individual parts are positioned relative to (ox, oy).
196+
if (Vertices.isConvex(points)) {
197+
var center = Vertices.centre(vertices);
198+
ox += center.x;
199+
oy += center.y
200+
}
201+
202+
body = Bodies.fromVertices(ox, oy, vertices, options);
194203
}
195204

196205
if (body)

0 commit comments

Comments
 (0)