2018-05-07 13:22:16 +00:00
|
|
|
/**
|
|
|
|
* @author Joachim Grill <joachim@codeandweb.com>
|
|
|
|
* @copyright 2018 CodeAndWeb GmbH
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Bodies = require('./lib/factory/Bodies');
|
|
|
|
var Body = require('./lib/body/Body');
|
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
|
|
|
var Common = require('./lib/core/Common');
|
|
|
|
var Vertices = require('./lib/geometry/Vertices');
|
|
|
|
var Bounds = require('./lib/geometry/Bounds');
|
|
|
|
var Vector = require('./lib/geometry/Vector');
|
|
|
|
|
|
|
|
|
2018-06-11 14:37:32 +00:00
|
|
|
/**
|
|
|
|
* Use PhysicsEditorParser.parseBody() to build a Matter body object, based on a physics data file
|
|
|
|
* created and exported with PhysicsEditor (https://www.codeandweb.com/physicseditor).
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PhysicsEditorParser
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
var PhysicsEditorParser = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a body element exported by PhysicsEditor.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PhysicsEditorParser#parseBody
|
|
|
|
*
|
|
|
|
* @param {number} x - x position
|
|
|
|
* @param {number} y - y position
|
|
|
|
* @param {number} w - width
|
|
|
|
* @param {number} h - height
|
|
|
|
* @param {object} config - body configuration and fixture (child body) definitions
|
|
|
|
* @return {object} a matter body, consisting of several parts (child bodies)
|
|
|
|
*/
|
|
|
|
parseBody: function (x, y, w, h, config)
|
2018-05-07 13:22:16 +00:00
|
|
|
{
|
2018-06-04 20:44:36 +00:00
|
|
|
var fixtureConfigs = GetFastValue(config, 'fixtures', []);
|
|
|
|
var fixtures = [];
|
|
|
|
for (var fc = 0; fc < fixtureConfigs.length; fc++)
|
2018-05-07 13:22:16 +00:00
|
|
|
{
|
2018-06-11 14:37:32 +00:00
|
|
|
var fixtureParts = this.parseFixture(fixtureConfigs[fc]);
|
2018-06-04 20:44:36 +00:00
|
|
|
for(var i = 0; i < fixtureParts.length; i++)
|
|
|
|
{
|
|
|
|
fixtures.push(fixtureParts[i]);
|
|
|
|
}
|
2018-05-07 13:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var matterConfig = Common.extend({}, false, config);
|
|
|
|
delete matterConfig.fixtures;
|
|
|
|
delete matterConfig.type;
|
|
|
|
|
2018-06-04 20:44:36 +00:00
|
|
|
var body = Body.create(matterConfig);
|
2018-05-07 13:22:16 +00:00
|
|
|
Body.setParts(body, fixtures);
|
|
|
|
body.render.sprite.xOffset = body.position.x / w;
|
|
|
|
body.render.sprite.yOffset = body.position.y / h;
|
|
|
|
Body.setPosition(body, { x: x, y: y });
|
|
|
|
|
|
|
|
return body;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2018-06-11 14:37:32 +00:00
|
|
|
/**
|
|
|
|
* Parses an element of the "fixtures" list exported by PhysicsEditor
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PhysicsEditorParser#parseFixture
|
|
|
|
*
|
|
|
|
* @param {object} fixtureConfig - the fixture object to parse
|
|
|
|
* @return {object[]} - a list of matter bodies
|
|
|
|
*/
|
|
|
|
parseFixture: function (fixtureConfig)
|
2018-05-07 13:22:16 +00:00
|
|
|
{
|
|
|
|
var matterConfig = Common.extend({}, false, fixtureConfig);
|
|
|
|
delete matterConfig.circle;
|
|
|
|
delete matterConfig.vertices;
|
|
|
|
|
2018-06-04 20:44:36 +00:00
|
|
|
var fixtures;
|
2018-05-07 13:22:16 +00:00
|
|
|
if (fixtureConfig.circle)
|
|
|
|
{
|
2018-06-04 20:44:36 +00:00
|
|
|
var x = GetFastValue(fixtureConfig.circle, 'x');
|
|
|
|
var y = GetFastValue(fixtureConfig.circle, 'y');
|
|
|
|
var r = GetFastValue(fixtureConfig.circle, 'radius');
|
2018-05-07 13:22:16 +00:00
|
|
|
fixtures = [ Bodies.circle(x, y, r, matterConfig) ];
|
|
|
|
}
|
|
|
|
else if (fixtureConfig.vertices)
|
|
|
|
{
|
2018-06-11 14:37:32 +00:00
|
|
|
fixtures = this.parseVertices(fixtureConfig.vertices, matterConfig);
|
2018-05-07 13:22:16 +00:00
|
|
|
}
|
|
|
|
return fixtures;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2018-06-11 14:37:32 +00:00
|
|
|
/**
|
|
|
|
* Parses the "vertices" lists exported by PhysicsEditor
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PhysicsEditorParser#parseVertices
|
|
|
|
*
|
|
|
|
* @param {object} vertexSets - the vertex lists to parse
|
|
|
|
* @param {object} options - matter body options
|
|
|
|
* @return {object[]} - a list of matter bodies
|
|
|
|
*/
|
|
|
|
parseVertices: function (vertexSets, options)
|
2018-05-07 13:22:16 +00:00
|
|
|
{
|
|
|
|
var i, j, k, v, z;
|
|
|
|
var parts = [];
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
2018-06-04 09:12:02 +00:00
|
|
|
for (v = 0; v < vertexSets.length; v += 1)
|
|
|
|
{
|
2018-05-07 13:22:16 +00:00
|
|
|
parts.push(Body.create(Common.extend({
|
|
|
|
position: Vertices.centre(vertexSets[v]),
|
|
|
|
vertices: vertexSets[v]
|
|
|
|
}, options)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// flag coincident part edges
|
2018-06-04 09:12:02 +00:00
|
|
|
var coincidentMaxDist = 5;
|
2018-05-07 13:22:16 +00:00
|
|
|
|
2018-06-04 09:12:02 +00:00
|
|
|
for (i = 0; i < parts.length; i++)
|
|
|
|
{
|
2018-05-07 13:22:16 +00:00
|
|
|
var partA = parts[i];
|
|
|
|
|
2018-06-04 09:12:02 +00:00
|
|
|
for (j = i + 1; j < parts.length; j++)
|
|
|
|
{
|
2018-05-07 13:22:16 +00:00
|
|
|
var partB = parts[j];
|
|
|
|
|
2018-06-04 09:12:02 +00:00
|
|
|
if (Bounds.overlaps(partA.bounds, partB.bounds))
|
|
|
|
{
|
2018-05-07 13:22:16 +00:00
|
|
|
var pav = partA.vertices,
|
|
|
|
pbv = partB.vertices;
|
|
|
|
|
|
|
|
// iterate vertices of both parts
|
2018-06-04 09:12:02 +00:00
|
|
|
for (k = 0; k < partA.vertices.length; k++)
|
|
|
|
{
|
|
|
|
for (z = 0; z < partB.vertices.length; z++)
|
|
|
|
{
|
2018-05-07 13:22:16 +00:00
|
|
|
// find distances between the vertices
|
|
|
|
var da = Vector.magnitudeSquared(Vector.sub(pav[(k + 1) % pav.length], pbv[z])),
|
|
|
|
db = Vector.magnitudeSquared(Vector.sub(pav[k], pbv[(z + 1) % pbv.length]));
|
|
|
|
|
|
|
|
// if both vertices are very close, consider the edge concident (internal)
|
2018-06-04 09:12:02 +00:00
|
|
|
if (da < coincidentMaxDist && db < coincidentMaxDist)
|
|
|
|
{
|
2018-05-07 13:22:16 +00:00
|
|
|
pav[k].isInternal = true;
|
|
|
|
pbv[z].isInternal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-11 14:37:32 +00:00
|
|
|
module.exports = PhysicsEditorParser;
|