Body.loadPolygon now parses all shapes that may exist in a JSON data file, and re-creates them all on the Body. Assumes shapes are CCW + Convex (tested working with Lime Exportor). Almost fixes #449 but Body rotation seems to screw orientation of shapes.

This commit is contained in:
photonstorm
2014-02-28 05:46:02 +00:00
parent 8dc28f4800
commit e45a929b00
5 changed files with 244 additions and 3 deletions
+1 -1
View File
@@ -607,7 +607,7 @@ Phaser.Cache.prototype = {
{
if (this._physics[key] && this._physics[key].data[object])
{
return this._physics[key].data[object][0];
return this._physics[key].data[object];
}
else
{
+42 -2
View File
@@ -986,7 +986,7 @@ Phaser.Physics.Body.prototype = {
* @method Phaser.Physics.Body#loadPolygon
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
* @param {object} options - An object containing the build options:
* @param {object} options - An object containing the build options. Note that this isn't used if the data file contains multiple shapes.
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
@@ -996,7 +996,7 @@ Phaser.Physics.Body.prototype = {
var data = this.game.cache.getPhysicsData(key, object);
if (data && data.shape)
if (data.length === 1)
{
var temp = [];
@@ -1008,6 +1008,46 @@ Phaser.Physics.Body.prototype = {
return this.addPolygon(options, temp);
}
else
{
// 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)
{
vertices.push([ this.px2pi(data[i].shape[s]), this.px2pi(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.px2pi(this.sprite.width / 2);
cm[1] -= this.px2pi(this.sprite.height / 2);
c.updateTriangles();
c.updateCenterOfMass();
c.updateBoundingRadius();
this.data.addShape(c, cm);
}
// this.data.adjustCenterOfMass();
this.data.aabbNeedsUpdate = true;
return true;
}
return false;