Class SvgPath2D
Reads the straight-line commands of an SVG path into a list of points, so a level outline drawn
in Inkscape can become a chain shape. Ported from the Box2D.NET samples' SvgParser
(MIT, (c) 2025 Erin Catto, (c) 2025 Choi Ikpil).
public static class SvgPath2D
- Inheritance
-
SvgPath2D
Remarks
Only M, L, H, V and their relative forms m, l,
h, v are read, with the SVG rule that coordinates after a move-to continue as
line-tos; z or Z ends the path. Curves are refused with a
FormatException: flatten them in the editor first.
SVG's y axis points down, so every point's y is negated after the offset is added, which puts the drawing the right way up in a world where y points up.
Methods
Parse(string, Vector2, float, bool)
Parses path into points: scale * (point + offset) with y flipped.
public static Vector2[] Parse(string path, Vector2 offset = default, float scale = 1, bool reverse = false)
Parameters
pathstringThe
dattribute of an SVG path made of straight lines.offsetVector2Added to every point in SVG units before scaling, to move the origin.
scalefloatMultiplies every point after the offset.
reverseboolReverses the point order, which flips the side a chain collides on.
Returns
- Vector2[]
The points in path order, or reversed when asked.
Exceptions
- FormatException
The path uses a command other than the straight-line ones, or a number does not parse.