Merge pull request #749 from georgiee/fix-loadPolygon

fixed loadPolygon center of mass
This commit is contained in:
Richard Davey 2014-04-22 22:08:53 +01:00
commit 7788365a74

View file

@ -1218,62 +1218,43 @@ Phaser.Physics.P2.Body.prototype = {
var data = this.game.cache.getPhysicsData(key, object);
if (data.length === 1)
{
var temp = [];
var localData = data[data.length - 1];
// We've multiple Convex shapes, they should be CCW automatically
var cm = p2.vec2.create();
// We've a list of numbers
for (var i = 0, len = localData.shape.length; i < len; i += 2)
for (var i = 0; i < data.length; i++)
{
var vertices = [];
for (var s = 0; s < data[i].shape.length; s += 2)
{
temp.push([localData.shape[i], localData.shape[i + 1]]);
vertices.push([ this.world.pxmi(data[i].shape[s]), this.world.pxmi(data[i].shape[s + 1]) ]);
}
return this.addPolygon(options, temp);
}
else
{
// We've multiple Convex shapes, they should be CCW automatically
var cm = p2.vec2.create();
var c = new p2.Convex(vertices);
for (var i = 0; i < data.length; i++)
// Move all vertices so its center of mass is in the local center of the convex
for (var j = 0; j !== c.vertices.length; j++)
{
var vertices = [];
for (var s = 0; s < data[i].shape.length; s += 2)
{
vertices.push([ this.world.pxmi(data[i].shape[s]), this.world.pxmi(data[i].shape[s + 1]) ]);
}
var c = new p2.Convex(vertices);
// Move all vertices so its center of mass is in the local center of the convex
for (var j = 0; j !== c.vertices.length; j++)
{
var v = c.vertices[j];
p2.vec2.sub(v, v, c.centerOfMass);
}
p2.vec2.scale(cm, c.centerOfMass, 1);
cm[0] -= this.world.pxmi(this.sprite.width / 2);
cm[1] -= this.world.pxmi(this.sprite.height / 2);
c.updateTriangles();
c.updateCenterOfMass();
c.updateBoundingRadius();
this.data.addShape(c, cm);
var v = c.vertices[j];
p2.vec2.sub(v, v, c.centerOfMass);
}
this.data.aabbNeedsUpdate = true;
this.shapeChanged();
p2.vec2.scale(cm, c.centerOfMass, 1);
return true;
cm[0] -= this.world.pxmi(this.sprite.width / 2);
cm[1] -= this.world.pxmi(this.sprite.height / 2);
c.updateTriangles();
c.updateCenterOfMass();
c.updateBoundingRadius();
this.data.addShape(c, cm);
}
return false;
this.data.aabbNeedsUpdate = true;
this.shapeChanged();
return true;
},