Skip to content

Commit c0189c0

Browse files
committed
fixed loadPolygon center of mass
1 parent e0f850b commit c0189c0

1 file changed

Lines changed: 24 additions & 43 deletions

File tree

src/physics/p2/Body.js

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,62 +1218,43 @@ Phaser.Physics.P2.Body.prototype = {
12181218

12191219
var data = this.game.cache.getPhysicsData(key, object);
12201220

1221-
if (data.length === 1)
1221+
// We've multiple Convex shapes, they should be CCW automatically
1222+
var cm = p2.vec2.create();
1223+
1224+
for (var i = 0; i < data.length; i++)
12221225
{
1223-
var temp = [];
1224-
var localData = data[data.length - 1];
1226+
var vertices = [];
12251227

1226-
// We've a list of numbers
1227-
for (var i = 0, len = localData.shape.length; i < len; i += 2)
1228+
for (var s = 0; s < data[i].shape.length; s += 2)
12281229
{
1229-
temp.push([localData.shape[i], localData.shape[i + 1]]);
1230+
vertices.push([ this.world.pxmi(data[i].shape[s]), this.world.pxmi(data[i].shape[s + 1]) ]);
12301231
}
12311232

1232-
return this.addPolygon(options, temp);
1233-
}
1234-
else
1235-
{
1236-
// We've multiple Convex shapes, they should be CCW automatically
1237-
var cm = p2.vec2.create();
1233+
var c = new p2.Convex(vertices);
12381234

1239-
for (var i = 0; i < data.length; i++)
1235+
// Move all vertices so its center of mass is in the local center of the convex
1236+
for (var j = 0; j !== c.vertices.length; j++)
12401237
{
1241-
var vertices = [];
1242-
1243-
for (var s = 0; s < data[i].shape.length; s += 2)
1244-
{
1245-
vertices.push([ this.world.pxmi(data[i].shape[s]), this.world.pxmi(data[i].shape[s + 1]) ]);
1246-
}
1247-
1248-
var c = new p2.Convex(vertices);
1249-
1250-
// Move all vertices so its center of mass is in the local center of the convex
1251-
for (var j = 0; j !== c.vertices.length; j++)
1252-
{
1253-
var v = c.vertices[j];
1254-
p2.vec2.sub(v, v, c.centerOfMass);
1255-
}
1256-
1257-
p2.vec2.scale(cm, c.centerOfMass, 1);
1258-
1259-
cm[0] -= this.world.pxmi(this.sprite.width / 2);
1260-
cm[1] -= this.world.pxmi(this.sprite.height / 2);
1261-
1262-
c.updateTriangles();
1263-
c.updateCenterOfMass();
1264-
c.updateBoundingRadius();
1265-
1266-
this.data.addShape(c, cm);
1238+
var v = c.vertices[j];
1239+
p2.vec2.sub(v, v, c.centerOfMass);
12671240
}
12681241

1269-
this.data.aabbNeedsUpdate = true;
1270-
this.shapeChanged();
1242+
p2.vec2.scale(cm, c.centerOfMass, 1);
1243+
1244+
cm[0] -= this.world.pxmi(this.sprite.width / 2);
1245+
cm[1] -= this.world.pxmi(this.sprite.height / 2);
12711246

1272-
return true;
1247+
c.updateTriangles();
1248+
c.updateCenterOfMass();
1249+
c.updateBoundingRadius();
12731250

1251+
this.data.addShape(c, cm);
12741252
}
12751253

1276-
return false;
1254+
this.data.aabbNeedsUpdate = true;
1255+
this.shapeChanged();
1256+
1257+
return true;
12771258

12781259
},
12791260

0 commit comments

Comments
 (0)