Created
February 24, 2015 02:48
-
-
Save bpander/97bdb1efc038cc6f550d to your computer and use it in GitHub Desktop.
Parse an svg file to create a Box2D hit box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function b2SVGParser () { | |
} | |
b2SVGParser.prototype.parse = function (svg, world, bodyDef, fixtureDef) { | |
fixtureDef.shape = new b2PolygonShape(); | |
var body = world.CreateBody(bodyDef); | |
var traverse = function (nodes) { | |
var i = -1; | |
var node; | |
var fixture; | |
while ((node = nodes[++i]) !== undefined) { | |
switch (node.tagName) { | |
case 'line': | |
fixtureDef.shape.SetAsEdge( | |
new b2Vec2(node.getAttribute('x1'), node.getAttribute('y1')), | |
new b2Vec2(node.getAttribute('x2'), node.getAttribute('y2')) | |
); | |
body.CreateFixture(fixtureDef); | |
break; | |
case 'polyline': | |
// Basically just iterate over the node's [points] attribute to make a bunch of 'line's | |
break; | |
} | |
traverse(node.children); | |
} | |
}; | |
traverse(svg.children); | |
return body; | |
}; | |
var svgParser = new b2SVGParser(); | |
svgParser.parse(svg, world, bodyDef, fixtureDef); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment