Skip to content

Instantly share code, notes, and snippets.

@Penagwin
Created January 4, 2017 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Penagwin/708dc34ab7043edf5f2c860c425fc3aa to your computer and use it in GitHub Desktop.
Save Penagwin/708dc34ab7043edf5f2c860c425fc3aa to your computer and use it in GitHub Desktop.
loadPolygon: function (key, object, scaleX, scaleY) {
if(scaleX === null)
scaleX = 1;
if(scaleY === null)
scaleY = 1;
if (key === null)
{
var data = object;
}
else
{
var data = this.game.cache.getPhysicsData(key, object);
}
// We've multiple Convex shapes, they should be CCW automatically
var cm = p2.vec2.create();
for (var i = 0; i < data.length; i++)
{
var vertices = [];
for (var s = 0; s < data[i].shape.length; s += 2)
{
if(scaleY < 0)
vertices.push([ this.world.pxmi((1 - data[i].shape[s] * scaleX)), this.world.pxmi(data[i].shape[s + 1] * scaleX) ]);
else
vertices.push([this.world.pxmi(data[i].shape[s] * scaleX ), this.world.pxmi(data[i].shape[s + 1] * scaleX) ]);
}
if(scaleY < 0)
vertices.reverse();
var c = new p2.Convex({ vertices: 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);
}
this.data.aabbNeedsUpdate = true;
this.shapeChanged();
return true;
}
};
@Penagwin
Copy link
Author

Penagwin commented Jan 4, 2017

Replace the default loadPolygon in Phaser with this class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment