Works on this device
Polygon Collision Generator
Draw the outline of a sprite corner by corner, with the mouse, the keyboard or typed numbers. The page simplifies the outline, finds its convex hull, cuts it into convex pieces for physics engines, and writes it all as a JSON file. Your picture is opened in this browser and is not uploaded.
Your drawing
PNG, JPEG, WebP, GIF or BMP, up to 50 MB and 8,192 pixels on a side. It is opened in this browser and is not uploaded.
In pixels. A picture sets its own size.
Click to add a corner. Or put the focus on the drawing, move the cross with the arrow keys (hold Shift for 10 pixels) and press Enter to add a corner at the cross. Backspace removes the last corner.
Corners
No corners yet.
| Corner | x (px) | y (px) | Remove |
|---|
For example 10, 20. This replaces the corners above when you choose Use these corners.
Settings
In pixels. 0 keeps every corner. Corners closer than this to the line through their neighbours' part of the outline are removed.
Leave it blank for no limit, or type 3 or more, such as the limit of your physics engine.
The JSON file
In pixels from the left edge, such as the middle of the sprite.
In pixels from the top edge.
The coordinates are divided by this, such as 100 or 16.
Ready. Add corners or choose Try an example.
Your collision shape
- Corners placed
- Corners after simplifying
- Simple outline
- Convex
- Area
- Perimeter
- Centroid
- Corners of the convex hull
- Convex pieces
How to use the generator
- Open a picture of your sprite from your device, or set the size of an empty drawing area. You can also choose Try an example.
- Add corners around the shape in order. Click on the drawing, or put the focus on the drawing and move the cross with the arrow keys (hold Shift for 10 pixels), then press Enter. You can also type the corners in the table or paste a list.
- Set a tolerance to remove corners that hardly change the shape, and the most corners in a piece if your physics engine has a limit.
- Check the outline, the convex hull and the convex pieces in the drawing and in the results, then choose Download JSON.
The methods
- Area by the shoelace formula: A = ½ |Σ (xᵢ yᵢ₊₁ − xᵢ₊₁ yᵢ)|. The centroid is the centre of that area.
- Convex hull by Andrew's monotone chain: the corners are sorted by x, then a lower and an upper chain are built. Points on a straight hull edge are left out.
- Simplification by the Ramer–Douglas–Peucker method: a corner is kept when it is farther than the tolerance from the straight line through the two ends of its part of the outline. The closed outline is cut at corner 1 and at the corner farthest from it.
- Convex pieces by the Hertel–Mehlhorn method: the outline is cut into triangles by ear clipping, then inner edges are removed while each joined piece stays convex and within the corner limit. This gives at most four times the fewest possible pieces, often far fewer.
- A simple outline has no edges that cross or touch, except neighbours at their shared corner. Only a simple outline can be cut into pieces.
Worked example
The example ship is 128 × 96 pixels with 9 corners. Its area is 4,412 square pixels. Its convex hull has 8 corners, because the notch at 52, 66 is left out. With no corner limit it splits into 2 convex pieces; with a limit of 4 corners and a tolerance of 8 pixels, the outline keeps 7 corners and splits into 3 pieces.
Limits you should know
- The page does not trace a picture by itself. You place the corners; the picture is a guide under them.
- Up to 1,000 corners; convex pieces for outlines of up to 400 corners; pictures up to 8,192 pixels on a side and 50 MB.
- An outline with holes needs more than one shape. Draw each part separately.
- Check what your physics engine accepts, such as the order of corners and the most corners in one shape, in its own documentation.