Created
January 4, 2017 07:38
-
-
Save Penagwin/708dc34ab7043edf5f2c860c425fc3aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace the default
loadPolygon
in Phaser with this class.