mirror of
https://github.com/photonstorm/phaser
synced 2025-02-17 14:38:30 +00:00
custom export from physics editor
This commit is contained in:
parent
634b1d1093
commit
66fa003901
3 changed files with 231 additions and 2 deletions
95
exporter/PhysicsEditor/exporter.xml
Normal file
95
exporter/PhysicsEditor/exporter.xml
Normal file
|
@ -0,0 +1,95 @@
|
|||
<exporter>
|
||||
<name>lime-json</name>
|
||||
<displayName>Phaser (JSON)</displayName>
|
||||
<description>Exporter for Phaser, JSON</description>
|
||||
<version>1.0</version>
|
||||
<yAxisDirection>down</yAxisDirection>
|
||||
<physicsEngine>box2d</physicsEngine>
|
||||
<template>phaser.json</template>
|
||||
<fileExtension>json</fileExtension>
|
||||
<anchorPoint>
|
||||
<enabled>no</enabled>
|
||||
</anchorPoint>
|
||||
<origin>
|
||||
<type>fixed</type>
|
||||
<relX>0.0</relX>
|
||||
<relY>1.0</relY>
|
||||
</origin>
|
||||
<!-- Circle support does not work as with standard box2d - you can't change the center
|
||||
or add more than one circle to a shape. This is why it is disabled for now -->
|
||||
<supportsCircles>no</supportsCircles>
|
||||
<body>
|
||||
</body>
|
||||
<global>
|
||||
</global>
|
||||
<body>
|
||||
</body>
|
||||
<fixture>
|
||||
<parameter>
|
||||
<name>density</name>
|
||||
<displayName>Density</displayName>
|
||||
<type>float</type>
|
||||
<min>-1000</min>
|
||||
<max>1000</max>
|
||||
<default>2.0</default>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>bounce</name>
|
||||
<displayName>Bounce</displayName>
|
||||
<type>float</type>
|
||||
<min>0</min>
|
||||
<max>1000</max>
|
||||
<default>0.0</default>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>friction</name>
|
||||
<displayName>Friction</displayName>
|
||||
<type>float</type>
|
||||
<min>0</min>
|
||||
<max>1000</max>
|
||||
<default>0.0</default>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>isSensor</name>
|
||||
<displayName>Is Sensor</displayName>
|
||||
<description>If set the physial </description>
|
||||
<type>bool</type>
|
||||
<default>false</default>
|
||||
</parameter>
|
||||
|
||||
<parameter>
|
||||
<name>filter_groupIndex</name>
|
||||
<displayName>Group</displayName>
|
||||
<description>Collision group.</description>
|
||||
<shortDescription></shortDescription>
|
||||
<type>int</type>
|
||||
<default>0</default>
|
||||
</parameter>
|
||||
|
||||
<parameter>
|
||||
<name>filter_bitfield</name>
|
||||
<type>bitfield</type>
|
||||
<size>16</size>
|
||||
</parameter>
|
||||
|
||||
<parameter>
|
||||
<name>filter_categoryBits</name>
|
||||
<displayName>Cat.</displayName>
|
||||
<description>Collision category</description>
|
||||
<shortDescription>Collision category</shortDescription>
|
||||
<type>uint16</type>
|
||||
<default>1</default>
|
||||
<bitfield>yes</bitfield>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>filter_maskBits</name>
|
||||
<displayName>Mask</displayName>
|
||||
<description>Collision mask</description>
|
||||
<shortDescription>Collision mask</shortDescription>
|
||||
<type>uint16</type>
|
||||
<default>65535</default>
|
||||
<bitfield>yes</bitfield>
|
||||
</parameter>
|
||||
</fixture>
|
||||
</exporter>
|
||||
|
17
exporter/PhysicsEditor/phaser.json
Normal file
17
exporter/PhysicsEditor/phaser.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{ {% for body in bodies %}
|
||||
{% if not forloop.first %}, {% endif %}
|
||||
"{{body.name}}": [
|
||||
{% for fixture in body.fixtures %}{% if not forloop.first %} ,{% endif %}
|
||||
{
|
||||
"density": {{fixture.density}}, "friction": {{fixture.friction}}, "bounce": {{fixture.bounce}}, {% if fixture.isSensor %}"isSensor"=true, {% endif %}
|
||||
"filter": { "categoryBits": {{fixture.filter_categoryBits}}, "maskBits": {{fixture.filter_maskBits}} },
|
||||
"polygons":[
|
||||
{% for polygon in fixture.polygons %}{% if not forloop.first %} ,{% endif %}
|
||||
[ {% for point in polygon %} {% if not forloop.first %}, {% endif %} {{point.x}}, {{point.y}} {% endfor %} ]
|
||||
{% endfor %}
|
||||
]
|
||||
}
|
||||
{% endfor %}
|
||||
]
|
||||
{% endfor %}
|
||||
}
|
|
@ -1095,6 +1095,47 @@ Phaser.Physics.P2.Body.prototype = {
|
|||
|
||||
},
|
||||
|
||||
loadPhaserPolygon: function (key, object, options) {
|
||||
var data = this.game.cache.getPhysicsData(key, object);
|
||||
|
||||
if (data.length === 1)
|
||||
{
|
||||
var temp = [];
|
||||
|
||||
data = data.pop()
|
||||
// We've a list of numbers
|
||||
for (var i = 0, len = data.shape.length; i < len; i += 2)
|
||||
{
|
||||
temp.push([data.shape[i], data.shape[i + 1]]);
|
||||
}
|
||||
|
||||
return this.addPolygon(options, temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// We've multiple Convex shapes, they should be CCW automatically
|
||||
var cm = p2.vec2.create();
|
||||
|
||||
//cycle through the fixtures
|
||||
for (var i = 0; i < data.length; i++)
|
||||
{
|
||||
var fixtureData = data[i]
|
||||
this.addPolygonShape(fixtureData)
|
||||
}
|
||||
|
||||
this.data.aabbNeedsUpdate = true;
|
||||
this.shapeChanged();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body.
|
||||
*
|
||||
|
@ -1107,8 +1148,84 @@ Phaser.Physics.P2.Body.prototype = {
|
|||
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
|
||||
* @return {boolean} True on success, else false.
|
||||
*/
|
||||
loadPolygon: function (key, object, options) {
|
||||
|
||||
addPolygonShape: function(fixtureData){
|
||||
fixtureData.density
|
||||
fixtureData.filter
|
||||
fixtureData.friction
|
||||
polygons = fixtureData.polygons
|
||||
|
||||
var cm = p2.vec2.create();
|
||||
|
||||
for (var i = 0; i < polygons.length; i++){
|
||||
shapes = polygons[i]
|
||||
|
||||
var vertices = [];
|
||||
for (var s = 0; s < shapes.length; s += 2)
|
||||
{
|
||||
vertices.push([ this.world.pxmi(shapes[s]), this.world.pxmi(shapes[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);
|
||||
}
|
||||
|
||||
/*for (var i = 0; i < data.length; i++)
|
||||
{
|
||||
|
||||
polygons = fixtureData.polygons
|
||||
|
||||
for (var k = 0; k < polygons.length; k++)
|
||||
{
|
||||
polygon = polygons[k] //array of numbers
|
||||
|
||||
var vertices = [];
|
||||
for (var s = 0; s < polygon.length; s += 2)
|
||||
{
|
||||
vertices.push([ this.world.pxmi(polygon[s]), this.world.pxmi(polygon[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);
|
||||
}
|
||||
}*/
|
||||
},
|
||||
|
||||
loadPolygon: function (key, object, options) {
|
||||
var data = this.game.cache.getPhysicsData(key, object);
|
||||
|
||||
if (data.length === 1)
|
||||
|
@ -1132,7 +1249,7 @@ Phaser.Physics.P2.Body.prototype = {
|
|||
for (var i = 0; i < data.length; i++)
|
||||
{
|
||||
var vertices = [];
|
||||
|
||||
console.log(data[i].filter.categoryBits)
|
||||
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]) ]);
|
||||
|
|
Loading…
Add table
Reference in a new issue