Hey guys.
I have spent the last few days finding a way to do this in 2.1a, and finally I came up with a solution. Though I think many of you already did this (or something like this), I couldn't find on this forum or anywhere in internet a working function that allowed me take a level made in InkScape and export it to Box2DAS3.
Finally, i ended up with my own version and I wanted to share it. Maybe you can improve and add it to the svn. don't know. Well, here it is:
A few things you need to know first:
ok, now the code. The only thing you need is to copy paste the function in to your code and call it with the right info:
ok. That's about it.
Enjoy, be ungry, cry or whatever this code does for you. 🙂
Rod
I have spent the last few days finding a way to do this in 2.1a, and finally I came up with a solution. Though I think many of you already did this (or something like this), I couldn't find on this forum or anywhere in internet a working function that allowed me take a level made in InkScape and export it to Box2DAS3.
Finally, i ended up with my own version and I wanted to share it. Maybe you can improve and add it to the svn. don't know. Well, here it is:
A few things you need to know first:
Quote:
1) What this functions does is create a static body in the world based in a ARRAY.
2) After creating your level, you'll have to save it as a OPTIMIZED .SVG.
3) Open the .svg file and clear everything BUT the coordinates (example: "-314.05,25.5-23.02 .....").
4) Replace every "-" with ",-".
5) Make that coordinates an array like this: var newLevel:Array = [-314.05 , 25.5, -23.02 , .....];
6) Copy that array to your code. This is your level.
7) This function ONLY supports one single chain.
2) After creating your level, you'll have to save it as a OPTIMIZED .SVG.
3) Open the .svg file and clear everything BUT the coordinates (example: "-314.05,25.5-23.02 .....").
4) Replace every "-" with ",-".
5) Make that coordinates an array like this: var newLevel:Array = [-314.05 , 25.5, -23.02 , .....];
6) Copy that array to your code. This is your level.
7) This function ONLY supports one single chain.
ok, now the code. The only thing you need is to copy paste the function in to your code and call it with the right info:
Code:
public static function parseSVG(svg: Array, world: b2World, position:b2Vec2): void {
// 1. Var's needed to build the level.
var chainDef:b2PolygonShape = new b2PolygonShape();
var chainBodyDef:b2BodyDef = new b2BodyDef();
var chainFixture:b2FixtureDef = new b2FixtureDef();
var chain:b2Body;
chainFixture.friction = 1.0;
chainFixture.restitution = 0.1;
chainFixture.density = 0.0;
var lastPosition:b2Vec2 = new b2Vec2(0,0/Constantes.RATIO);
var lastLength:Number = 0;
var lastAngle:Number = 0;
for (var i:int = 2; i < svg.length; i+=2)//
{
// 1. The new position.
var newPosition:b2Vec2 = new b2Vec2(lastPosition.x + lastLength * Math.cos(lastAngle)*2, lastPosition.y + lastLength * Math.sin(lastAngle)*2);
// 2. New chain.
var largo:Number = Math.sqrt((svg[i]) * (svg[i]) + (svg[i + 1] ) * (svg[i + 1] )) / Constantes.RATIO/2;
var angulo:Number = Math.atan((svg[i + 1] ) / (svg[i] ) );
chainBodyDef = new b2BodyDef();
chainDef.SetAsOrientedBox(largo, 0.01, new b2Vec2(newPosition.x+largo*Math.cos(angulo),newPosition.y+largo * Math.sin(angulo)),angulo);
chainBodyDef.type = b2Body.b2_staticBody;
chainFixture.shape = chainDef;
chain = Constantes._world.CreateBody(chainBodyDef);
chain.CreateFixture(chainFixture);
// 3. After creating the last chain, we save it's position, length and angle.
lastPosition.Set(newPosition.x,newPosition.y);
lastLength = largo;
lastAngle = angulo;
}
chain.SetPosition(position);
}
// 1. Var's needed to build the level.
var chainDef:b2PolygonShape = new b2PolygonShape();
var chainBodyDef:b2BodyDef = new b2BodyDef();
var chainFixture:b2FixtureDef = new b2FixtureDef();
var chain:b2Body;
chainFixture.friction = 1.0;
chainFixture.restitution = 0.1;
chainFixture.density = 0.0;
var lastPosition:b2Vec2 = new b2Vec2(0,0/Constantes.RATIO);
var lastLength:Number = 0;
var lastAngle:Number = 0;
for (var i:int = 2; i < svg.length; i+=2)//
{
// 1. The new position.
var newPosition:b2Vec2 = new b2Vec2(lastPosition.x + lastLength * Math.cos(lastAngle)*2, lastPosition.y + lastLength * Math.sin(lastAngle)*2);
// 2. New chain.
var largo:Number = Math.sqrt((svg[i]) * (svg[i]) + (svg[i + 1] ) * (svg[i + 1] )) / Constantes.RATIO/2;
var angulo:Number = Math.atan((svg[i + 1] ) / (svg[i] ) );
chainBodyDef = new b2BodyDef();
chainDef.SetAsOrientedBox(largo, 0.01, new b2Vec2(newPosition.x+largo*Math.cos(angulo),newPosition.y+largo * Math.sin(angulo)),angulo);
chainBodyDef.type = b2Body.b2_staticBody;
chainFixture.shape = chainDef;
chain = Constantes._world.CreateBody(chainBodyDef);
chain.CreateFixture(chainFixture);
// 3. After creating the last chain, we save it's position, length and angle.
lastPosition.Set(newPosition.x,newPosition.y);
lastLength = largo;
lastAngle = angulo;
}
chain.SetPosition(position);
}
ok. That's about it.
Enjoy, be ungry, cry or whatever this code does for you. 🙂
Rod