Skip to content

Commit 45a41af

Browse files
committed
exporter update
1 parent 66fa003 commit 45a41af

4 files changed

Lines changed: 93 additions & 147 deletions

File tree

exporter/PhysicsEditor/exporter.xml

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,14 @@
1111
<enabled>no</enabled>
1212
</anchorPoint>
1313
<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>
14+
<type>fixed</type>
15+
<relX>0.0</relX>
16+
<relY>1.0</relY>
17+
</origin>
18+
<supportsCircles>yes</supportsCircles>
2519
<body>
2620
</body>
2721
<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>
5222
<parameter>
5323
<name>isSensor</name>
5424
<displayName>Is Sensor</displayName>

exporter/PhysicsEditor/phaser.json

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33
"{{body.name}}": [
44
{% for fixture in body.fixtures %}{% if not forloop.first %} ,{% endif %}
55
{
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-
]
6+
"isSensor": {{fixture.isSensor}},
7+
"filter": {
8+
"categoryBits": {{fixture.filter_categoryBits}},
9+
"maskBits": {{fixture.filter_maskBits}}
10+
},
11+
12+
{% if fixture.isCircle %}
13+
"circle": {
14+
"radius": {{fixture.radius|floatformat:3}},
15+
"position": [
16+
{{fixture.center.x|floatformat:3}},
17+
{{fixture.center.y|floatformat:3}}
18+
]
19+
}
20+
{% else %}
21+
"polygons":[
22+
{% for polygon in fixture.polygons %}{% if not forloop.first %} ,{% endif %}
23+
[ {% for point in polygon %} {% if not forloop.first %}, {% endif %} {{point.x}}, {{point.y}} {% endfor %} ]
24+
{% endfor %}
25+
]
26+
{% endif %}
1327
}
1428
{% endfor %}
1529
]

src/physics/p2/Body.js

Lines changed: 66 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,41 +1097,79 @@ Phaser.Physics.P2.Body.prototype = {
10971097

10981098
loadPhaserPolygon: function (key, object, options) {
10991099
var data = this.game.cache.getPhysicsData(key, object);
1100-
1101-
if (data.length === 1)
1100+
//cycle through the fixtures
1101+
for (var i = 0; i < data.length; i++)
11021102
{
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);
1103+
var fixtureData = data[i]
1104+
this.addFixture(fixtureData)
11131105
}
1114-
else
1115-
{
11161106

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();
1107+
this.data.aabbNeedsUpdate = true;
1108+
this.shapeChanged();
11291109

1130-
return true;
1110+
return false;
11311111

1112+
},
1113+
1114+
/**
1115+
* Add a polygon fixture. This is used during #loadPhaserPolygon.
1116+
*
1117+
* @method Phaser.Physics.P2.Body#addPolygonFixture
1118+
* @param {string} fixtureData - The data for the fixture.
1119+
* It contains: isSensor, filter (collision) and the actual polygon shapes
1120+
*/
1121+
1122+
addFixture: function(fixtureData){
1123+
console.log('addPolygonFixture', fixtureData)
1124+
1125+
if (fixtureData.circle){
1126+
//a circle has unfortunately no position in p2
1127+
var shape = new p2.Circle(this.world.pxm(fixtureData.circle.radius))
1128+
shape.collisionGroup = fixtureData.filter.categoryBits
1129+
shape.collisionMask = fixtureData.filter.maskBits
1130+
shape.sensor = fixtureData.isSensor
1131+
1132+
this.data.addShape(shape);
1133+
}else{
1134+
polygons = fixtureData.polygons
1135+
var cm = p2.vec2.create();
1136+
1137+
for (var i = 0; i < polygons.length; i++){
1138+
shapes = polygons[i]
1139+
1140+
var vertices = [];
1141+
for (var s = 0; s < shapes.length; s += 2)
1142+
{
1143+
vertices.push([ this.world.pxmi(shapes[s]), this.world.pxmi(shapes[s + 1]) ]);
1144+
}
1145+
1146+
var shape = new p2.Convex(vertices);
1147+
1148+
// Move all vertices so its center of mass is in the local center of the convex
1149+
for (var j = 0; j !== shape.vertices.length; j++)
1150+
{
1151+
var v = shape.vertices[j];
1152+
p2.vec2.sub(v, v, shape.centerOfMass);
1153+
}
1154+
1155+
p2.vec2.scale(cm, shape.centerOfMass, 1);
1156+
1157+
cm[0] -= this.world.pxmi(this.sprite.width / 2);
1158+
cm[1] -= this.world.pxmi(this.sprite.height / 2);
1159+
1160+
shape.updateTriangles();
1161+
shape.updateCenterOfMass();
1162+
shape.updateBoundingRadius();
1163+
1164+
1165+
shape.collisionGroup = fixtureData.filter.categoryBits
1166+
shape.collisionMask = fixtureData.filter.maskBits
1167+
shape.sensor = fixtureData.isSensor
1168+
1169+
this.data.addShape(shape, cm);
11321170
}
1171+
}
11331172

1134-
return false;
11351173

11361174
},
11371175

@@ -1148,82 +1186,6 @@ Phaser.Physics.P2.Body.prototype = {
11481186
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
11491187
* @return {boolean} True on success, else false.
11501188
*/
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);
1182-
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-
},
12271189

12281190
loadPolygon: function (key, object, options) {
12291191
var data = this.game.cache.getPhysicsData(key, object);

src/physics/p2/BodyDebug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
109109
var l = obj.shapes.length
110110

111111
i = 0;
112-
112+
console.log('shapes',l);
113113
while (i !== l)
114114
{
115115
child = obj.shapes[i];

0 commit comments

Comments
 (0)