Function contourToTriangles

  • Attaches a triangulated polygon collider to bodyId. Each triangle becomes its own b2PolygonShape (3 verts, two-sided collision).

    This is the preferred path for both terrain and debris bodies because polygon shapes resolve penetration on either side, unlike one-sided b2ChainShape. Non-convex contours (e.g. an L-shaped piece left after a carve) are handled directly by the triangulator — no convex check or fallback is needed.

    Returns the number of triangle shapes successfully attached. 0 means the contour was too short or earcut produced no triangles (e.g. all vertices collinear). Callers that need a body-creation gate should check for > 0.

    Vertex direction does not affect correctness — earcut handles either winding — but we still flip y at the same place chain conversion does so that pixel-space (y-down) contours land in Box2D meter-space (y-up).

    Parameters

    Returns number