Skip to content

Commit 66fa003

Browse files
committed
custom export from physics editor
1 parent 634b1d1 commit 66fa003

3 files changed

Lines changed: 231 additions & 2 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<exporter>
2+
<name>lime-json</name>
3+
<displayName>Phaser (JSON)</displayName>
4+
<description>Exporter for Phaser, JSON</description>
5+
<version>1.0</version>
6+
<yAxisDirection>down</yAxisDirection>
7+
<physicsEngine>box2d</physicsEngine>
8+
<template>phaser.json</template>
9+
<fileExtension>json</fileExtension>
10+
<anchorPoint>
11+
<enabled>no</enabled>
12+
</anchorPoint>
13+
<origin>
14+
<type>fixed</type>
15+
<relX>0.0</relX>
16+
<relY>1.0</relY>
17+
</origin>
18+
<!-- Circle support does not work as with standard box2d - you can't change the center
19+
or add more than one circle to a shape. This is why it is disabled for now -->
20+
<supportsCircles>no</supportsCircles>
21+
<body>
22+
</body>
23+
<global>
24+
</global>
25+
<body>
26+
</body>
27+
<fixture>
28+
<parameter>
29+
<name>density</name>
30+
<displayName>Density</displayName>
31+
<type>float</type>
32+
<min>-1000</min>
33+
<max>1000</max>
34+
<default>2.0</default>
35+
</parameter>
36+
<parameter>
37+
<name>bounce</name>
38+
<displayName>Bounce</displayName>
39+
<type>float</type>
40+
<min>0</min>
41+
<max>1000</max>
42+
<default>0.0</default>
43+
</parameter>
44+
<parameter>
45+
<name>friction</name>
46+
<displayName>Friction</displayName>
47+
<type>float</type>
48+
<min>0</min>
49+
<max>1000</max>
50+
<default>0.0</default>
51+
</parameter>
52+
<parameter>
53+
<name>isSensor</name>
54+
<displayName>Is Sensor</displayName>
55+
<description>If set the physial </description>
56+
<type>bool</type>
57+
<default>false</default>
58+
</parameter>
59+
60+
<parameter>
61+
<name>filter_groupIndex</name>
62+
<displayName>Group</displayName>
63+
<description>Collision group.</description>
64+
<shortDescription></shortDescription>
65+
<type>int</type>
66+
<default>0</default>
67+
</parameter>
68+
69+
<parameter>
70+
<name>filter_bitfield</name>
71+
<type>bitfield</type>
72+
<size>16</size>
73+
</parameter>
74+
75+
<parameter>
76+
<name>filter_categoryBits</name>
77+
<displayName>Cat.</displayName>
78+
<description>Collision category</description>
79+
<shortDescription>Collision category</shortDescription>
80+
<type>uint16</type>
81+
<default>1</default>
82+
<bitfield>yes</bitfield>
83+
</parameter>
84+
<parameter>
85+
<name>filter_maskBits</name>
86+
<displayName>Mask</displayName>
87+
<description>Collision mask</description>
88+
<shortDescription>Collision mask</shortDescription>
89+
<type>uint16</type>
90+
<default>65535</default>
91+
<bitfield>yes</bitfield>
92+
</parameter>
93+
</fixture>
94+
</exporter>
95+

exporter/PhysicsEditor/phaser.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{ {% for body in bodies %}
2+
{% if not forloop.first %}, {% endif %}
3+
"{{body.name}}": [
4+
{% for fixture in body.fixtures %}{% if not forloop.first %} ,{% endif %}
5+
{
6+
"density": {{fixture.density}}, "friction": {{fixture.friction}}, "bounce": {{fixture.bounce}}, {% if fixture.isSensor %}"isSensor"=true, {% endif %}
7+
"filter": { "categoryBits": {{fixture.filter_categoryBits}}, "maskBits": {{fixture.filter_maskBits}} },
8+
"polygons":[
9+
{% for polygon in fixture.polygons %}{% if not forloop.first %} ,{% endif %}
10+
[ {% for point in polygon %} {% if not forloop.first %}, {% endif %} {{point.x}}, {{point.y}} {% endfor %} ]
11+
{% endfor %}
12+
]
13+
}
14+
{% endfor %}
15+
]
16+
{% endfor %}
17+
}

src/physics/p2/Body.js

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,47 @@ Phaser.Physics.P2.Body.prototype = {
10951095

10961096
},
10971097

1098+
loadPhaserPolygon: function (key, object, options) {
1099+
var data = this.game.cache.getPhysicsData(key, object);
1100+
1101+
if (data.length === 1)
1102+
{
1103+
var temp = [];
1104+
1105+
data = data.pop()
1106+
// We've a list of numbers
1107+
for (var i = 0, len = data.shape.length; i < len; i += 2)
1108+
{
1109+
temp.push([data.shape[i], data.shape[i + 1]]);
1110+
}
1111+
1112+
return this.addPolygon(options, temp);
1113+
}
1114+
else
1115+
{
1116+
1117+
// We've multiple Convex shapes, they should be CCW automatically
1118+
var cm = p2.vec2.create();
1119+
1120+
//cycle through the fixtures
1121+
for (var i = 0; i < data.length; i++)
1122+
{
1123+
var fixtureData = data[i]
1124+
this.addPolygonShape(fixtureData)
1125+
}
1126+
1127+
this.data.aabbNeedsUpdate = true;
1128+
this.shapeChanged();
1129+
1130+
return true;
1131+
1132+
}
1133+
1134+
return false;
1135+
1136+
},
1137+
1138+
10981139
/**
10991140
* Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body.
11001141
*
@@ -1107,8 +1148,84 @@ Phaser.Physics.P2.Body.prototype = {
11071148
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
11081149
* @return {boolean} True on success, else false.
11091150
*/
1110-
loadPolygon: function (key, object, options) {
1151+
1152+
addPolygonShape: function(fixtureData){
1153+
fixtureData.density
1154+
fixtureData.filter
1155+
fixtureData.friction
1156+
polygons = fixtureData.polygons
1157+
1158+
var cm = p2.vec2.create();
1159+
1160+
for (var i = 0; i < polygons.length; i++){
1161+
shapes = polygons[i]
1162+
1163+
var vertices = [];
1164+
for (var s = 0; s < shapes.length; s += 2)
1165+
{
1166+
vertices.push([ this.world.pxmi(shapes[s]), this.world.pxmi(shapes[s + 1]) ]);
1167+
}
1168+
1169+
var c = new p2.Convex(vertices);
1170+
1171+
// Move all vertices so its center of mass is in the local center of the convex
1172+
for (var j = 0; j !== c.vertices.length; j++)
1173+
{
1174+
var v = c.vertices[j];
1175+
p2.vec2.sub(v, v, c.centerOfMass);
1176+
}
1177+
1178+
p2.vec2.scale(cm, c.centerOfMass, 1);
1179+
1180+
cm[0] -= this.world.pxmi(this.sprite.width / 2);
1181+
cm[1] -= this.world.pxmi(this.sprite.height / 2);
11111182

1183+
c.updateTriangles();
1184+
c.updateCenterOfMass();
1185+
c.updateBoundingRadius();
1186+
1187+
this.data.addShape(c, cm);
1188+
}
1189+
1190+
/*for (var i = 0; i < data.length; i++)
1191+
{
1192+
1193+
polygons = fixtureData.polygons
1194+
1195+
for (var k = 0; k < polygons.length; k++)
1196+
{
1197+
polygon = polygons[k] //array of numbers
1198+
1199+
var vertices = [];
1200+
for (var s = 0; s < polygon.length; s += 2)
1201+
{
1202+
vertices.push([ this.world.pxmi(polygon[s]), this.world.pxmi(polygon[s + 1]) ]);
1203+
}
1204+
1205+
var c = new p2.Convex(vertices);
1206+
1207+
// Move all vertices so its center of mass is in the local center of the convex
1208+
for (var j = 0; j !== c.vertices.length; j++)
1209+
{
1210+
var v = c.vertices[j];
1211+
p2.vec2.sub(v, v, c.centerOfMass);
1212+
}
1213+
1214+
p2.vec2.scale(cm, c.centerOfMass, 1);
1215+
1216+
cm[0] -= this.world.pxmi(this.sprite.width / 2);
1217+
cm[1] -= this.world.pxmi(this.sprite.height / 2);
1218+
1219+
c.updateTriangles();
1220+
c.updateCenterOfMass();
1221+
c.updateBoundingRadius();
1222+
1223+
this.data.addShape(c, cm);
1224+
}
1225+
}*/
1226+
},
1227+
1228+
loadPolygon: function (key, object, options) {
11121229
var data = this.game.cache.getPhysicsData(key, object);
11131230

11141231
if (data.length === 1)
@@ -1132,7 +1249,7 @@ Phaser.Physics.P2.Body.prototype = {
11321249
for (var i = 0; i < data.length; i++)
11331250
{
11341251
var vertices = [];
1135-
1252+
console.log(data[i].filter.categoryBits)
11361253
for (var s = 0; s < data[i].shape.length; s += 2)
11371254
{
11381255
vertices.push([ this.world.pxmi(data[i].shape[s]), this.world.pxmi(data[i].shape[s + 1]) ]);

0 commit comments

Comments
 (0)