/box2d-xml-parser

Parse box2d XML generated by PhysicsEditor

Primary LanguageHaxeMIT LicenseMIT

box2d-xml-parser

Parse box2d XML generated by PhysicsEditor.

Usage

First version is generic to be used and converted for your own library by yourself. I personaly use this in conjonction with differ but it still needs to convert all float in Vector.

Because PhysicsEditor don't use trimmed data (transparency removal), you can use an optional method to trim all data while parsing once instead of doing it in an update loop.

//PhysicsEditor has a down-top y, so you need to set true in the 2nd parameter to invert it to use with most haxe libs and openfl.
var box2dData = Box2DGenericParser.parse(polygons_str, true, trimmer);

//if you use openfl-atlas, this is what the optional trimmer method should look like:
function trimmer(name:String, isX:Bool):Float
{
	var rect = _tilesetEx.getSize(_tilesetEx.getImageID(name));
	
	if (isX)
	{
		return rect.x;
	}else{
		return rect.y;
	}
}

Then you can get a specific polygon by looping through all objects:

for (body in box2dData)
{
	trace("body of", body.name)
	var vertices:Array<Array<Float>> = body.fixture[0].polygons;
	//do whatever you want with all these points (I personaly convert to vector or points depending of the engine used)
}

TODO

  • make a convenient generic method to give all points in the user format
  • make it directly compatible with box2d using box2d internal classes