exporter update

This commit is contained in:
Georgios Kaleadis 2014-03-20 15:27:50 +01:00
parent 66fa003901
commit 45a41af766
4 changed files with 95 additions and 149 deletions

View file

@ -15,40 +15,10 @@
<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>
<supportsCircles>yes</supportsCircles>
<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>

View file

@ -3,13 +3,27 @@
"{{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}} },
"isSensor": {{fixture.isSensor}},
"filter": {
"categoryBits": {{fixture.filter_categoryBits}},
"maskBits": {{fixture.filter_maskBits}}
},
{% if fixture.isCircle %}
"circle": {
"radius": {{fixture.radius|floatformat:3}},
"position": [
{{fixture.center.x|floatformat:3}},
{{fixture.center.y|floatformat:3}}
]
}
{% else %}
"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 %}
]
{% endif %}
}
{% endfor %}
]

View file

@ -1097,41 +1097,79 @@ 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.addFixture(fixtureData)
}
this.data.aabbNeedsUpdate = true;
this.shapeChanged();
return true;
return false;
},
/**
* Add a polygon fixture. This is used during #loadPhaserPolygon.
*
* @method Phaser.Physics.P2.Body#addPolygonFixture
* @param {string} fixtureData - The data for the fixture.
* It contains: isSensor, filter (collision) and the actual polygon shapes
*/
addFixture: function(fixtureData){
console.log('addPolygonFixture', fixtureData)
if (fixtureData.circle){
//a circle has unfortunately no position in p2
var shape = new p2.Circle(this.world.pxm(fixtureData.circle.radius))
shape.collisionGroup = fixtureData.filter.categoryBits
shape.collisionMask = fixtureData.filter.maskBits
shape.sensor = fixtureData.isSensor
this.data.addShape(shape);
}else{
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 shape = 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 !== shape.vertices.length; j++)
{
var v = shape.vertices[j];
p2.vec2.sub(v, v, shape.centerOfMass);
}
p2.vec2.scale(cm, shape.centerOfMass, 1);
cm[0] -= this.world.pxmi(this.sprite.width / 2);
cm[1] -= this.world.pxmi(this.sprite.height / 2);
shape.updateTriangles();
shape.updateCenterOfMass();
shape.updateBoundingRadius();
shape.collisionGroup = fixtureData.filter.categoryBits
shape.collisionMask = fixtureData.filter.maskBits
shape.sensor = fixtureData.isSensor
this.data.addShape(shape, cm);
}
}
return false;
},
@ -1149,82 +1187,6 @@ Phaser.Physics.P2.Body.prototype = {
* @return {boolean} True on success, else false.
*/
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);

View file

@ -109,7 +109,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
var l = obj.shapes.length
i = 0;
console.log('shapes',l);
while (i !== l)
{
child = obj.shapes[i];