What it does
A 2D physics solver wants convex polygons, and refuses or quietly truncates one with too many points. A lumpy rock, a ship with swept wings or a horseshoe is neither convex nor small. The pack gives four steps, and the one you want depends on how faithful the collider has to be.
convexHull wraps a point set in its convex hull: the fastest, and wrong for anything with a real concavity, since a
horseshoe becomes a disc. simplifyHull cuts a hull down to a vertex budget while keeping its extent. hullFor does
both, outline in and physics-ready convex polygon out, and is the call most games want. convexPieces decomposes a
concave outline into a set of convex polygons, each within the budget, so a body built from several fixtures keeps its
notch; it merges neighbouring pieces back together where the join stays convex, so a shape ends up as six polygons
rather than forty triangles.
Beside those, isConvex tests a polygon, and triangulate ear-clips an outline into triangles, returned as index
triples, for filling it.
Features
- Convex hulls —
convexHullof any point set. - A vertex budget —
simplifyHullandhullForcut a hull to a maximum, 8 by default (MAX_HULL_POINTS), and keep its extent. - Convex decomposition —
convexPiecessplits a concave outline into merged convex pieces within the budget. - Triangulation —
triangulateear-clips a simple polygon into index triples for a fill. - A convexity test —
isConvex. - Deterministic — the same outline always gives the same collider, so replays and tests agree.
Good for
- Physics games with line art — ships, rocks and props that must collide as real bodies.
- Generated shapes — an outline made at run time turned into a collider on the spot.
- Drawing games — a shape the player draws becoming a physics object, once it is checked for crossings.
- Filled vector shapes — triangles to give an outline a solid face.
Make it yours
Every hull call takes its own vertex budget, so a game can match its physics engine's limit or trade accuracy for
speed. A small asteroid can be one hullFor polygon, cheap and close enough; a space station with a docking bay can be
convexPieces, several fixtures on one body, so a ship can fly into the bay rather than bouncing off a lid that is not
drawn. Filling is separate: triangulate the same outline for its face, and keep the hull for the collider.
Works with
physics-box2d— hulls outlines withhullForfor a polygon body and keeps every concavity withconvexPiecesfor a decomposed one, within Box2D's eight-vertex limit.vector-hit— point tests and circle overlaps, when a full collider is more than the game needs.vector-art— the drawn outlines that most often need a collider.
Good to know
triangulatewants a simple polygon, one that does not cross itself. A self-intersecting outline gives nonsense rather than an error, so if players draw shapes, check them first.- Decomposition is a heuristic, not an optimal cover: it produces a good set quickly rather than the smallest possible one.
- A plain hull fills in every concavity; reach for
convexPieceswhenever the notch is the point.






