A capsule body rests on a convex rock; how do GJK distance with collision margins and EPA together give its penetration depth and normal?
answer
- GJK proves overlap, not depth
- core shape plus a margin
- shallow path vs deep path
- expand the polytope toward the boundary
basics
~20 sA capsule is a core segment plus a radius: GJK distance measures core to rock, depth is radius plus rock margin minus that distance, and the normal joins the closest points. Only overlapping cores need EPA, which expands GJK's simplex to the boundary.
solid answer
~50 sGJK proves overlap but gives no depth, so narrow phase splits shapes into a **core** and a **margin**. A capsule already is one, a segment plus its radius; a rock hull gets a small margin. While the cores are apart, which is the common resting case because the solver only lets shapes sink a little, GJK's distance mode returns closest points `pA`, `pB` and a distance `dist`. The normal is `(pB - pA) / dist`, from capsule toward rock, the depth is `rCapsule + mRock - dist`, and the contact points are the closest points pushed out by their margins. If the cores themselves overlap, after a teleport or a heavy landing, the distance is zero and has no direction, so EPA runs: starting from GJK's enclosing tetrahedron it repeatedly takes the polytope face closest to the origin, fetches the support point along that face's normal, and stops when that point barely moves past the face; the face's distance plus both margins is the depth.
go deeper
Recall that GJK only proves two convex shapes overlap, and that EPA is the separate step that measures penetration depth and the contact normal.
Explain how a capsule splits into a core segment and a radius, and how the depth is the margins minus the core distance on the shallow path.
Show why resting contacts should stay on GJK's distance path, how EPA expands from the enclosing tetrahedron, and how to diagnose normal flicker on deep contacts.
Set the margin policy across a game's shapes, trading visible rounding against how often the expensive and less stable deep-penetration path runs.
## Why GJK alone is not enough GJK tells you whether the origin lies inside the **Minkowski difference** `A - B` of two convex shapes, and in its distance form how far apart separated shapes are. Once the shapes overlap, the boolean answer "yes" carries no **penetration depth** and no **normal**, and the distance form simply reports zero. A solver needs both, so narrow phase adds one of two things: a **margin** scheme that keeps the common case in GJK's distance form, and the **expanding polytope algorithm (EPA)** for the rare deep case. ## Core shapes and margins - A **core shape** is the shape you hand to GJK; the **margin** is a radius wrapped around it, so the collision shape is the core grown by the margin in every direction. - A **capsule** is naturally a core segment with its radius as margin; a sphere is a core point with its radius as margin. - A box or hull can be given a small margin too, either by shrinking the core so the rounded result matches the original size, or by inflating the original and accepting a slightly rounded, slightly larger shape. - Contacts are computed between cores, and the margins are added back to the depth and to the contact points. ## The shallow path: GJK distance Take the scenario of a capsule body lying on a convex rock. Call the capsule A, with core segment and radius `rA`, and the rock B, with core hull and margin `mB`. 1. Run GJK in distance mode on the core segment and the core hull, seeded with last frame's result. 2. If the core distance `dist` is greater than `rA + mB`, there is no contact. 3. Otherwise the **normal** is `(pB - pA) / dist`, pointing from the capsule toward the rock, from the closest points `pA` on the segment and `pB` on the hull. 4. The **depth** is `rA + mB - dist`. 5. The **contact points** are `pA + normal * rA` on the capsule's surface and `pB - normal * mB` on the rock's surface, or their midpoint. With figures chosen for this scenario: `rA = 0.4 m`, `mB = 0.02 m` and a core distance of `0.41 m` give a depth of `0.4 + 0.02 - 0.41 = 0.01 m`. Because the solver only lets resting shapes sink a small amount, the cores stay apart frame after frame and every frame takes this cheap, smooth and well-defined path. ## The deep path: EPA When the cores themselves overlap, GJK's distance is zero and there are no closest points to define a normal. **EPA** finds the point of the Minkowski difference's boundary closest to the origin: 1. Start from GJK's final simplex. It must be a tetrahedron that encloses the origin; if GJK stopped at a triangle or segment, add support points in directions that grow it to one. 2. For each triangular face, store its outward unit normal `n` and its distance from the origin, `dot(n, v)` for any of its vertices `v`. 3. Pick the face with the smallest distance. 4. Fetch `w = support(n)` for that face's normal. 5. If `dot(w, n)` exceeds the face's distance by less than a tolerance, that face lies on the boundary: its distance is the **core penetration depth** and `n` is the **normal**. Stop. 6. Otherwise remove every face that `w` can see, collect the **horizon** (edges belonging to exactly one removed face), connect each horizon edge to `w` with a new face, and return to step 3. Because the polytope only grows, the smallest face distance does not shrink in exact arithmetic; it climbs toward the true depth. The full depth is the EPA depth plus both margins, and the contact points come from the closest face's barycentric weights applied to the stored support points of each shape. ## Choosing margins | Margin | Effect | |---|---| | Zero | Resting contacts overlap the cores, so EPA runs constantly and normals can flicker | | Small, a fraction of the smallest feature | Resting contacts stay on the distance path; corners round barely visibly | | Large | Corners round visibly, thin shapes lose their core, objects can appear to float or roll | ## Failure modes - **Normal flicker under EPA**: two faces of the polytope at nearly the same distance can swap from one frame to the next, so the normal jumps; keeping shallow contacts off EPA hides this. - **Thin shapes**: when margins are carved out of the shape, a margin larger than half its thickness leaves no core; scale margins to each shape. - **Iteration and face limits**: EPA's polytope grows with each step; cap it, and accept the best face found when the cap is reached. - **Mixed conventions**: if the distance path reports the normal from A to B and the EPA path from B to A, deep contacts push bodies together. Both paths must share the convention.
- Why does EPA need GJK's final simplex to be a full tetrahedron?EPA expands a polytope that encloses the origin with non-zero volume, so every face has a well-defined outward normal and a positive distance. If GJK stopped with the origin on a triangle, edge or vertex, the shapes are only just touching; you add support points along the missing directions, such as both sides of the triangle's normal, until a tetrahedron encloses the origin.
- What goes wrong when collision margins are too large or too small?Too small, and almost every resting contact overlaps the cores, so EPA runs constantly and its normals flicker. Too large, and box corners round visibly, objects sit on a cushion above surfaces, and thin shapes lose their core altogether. Size margins to a fraction of each shape's smallest dimension.
- Why is EPA's normal often less stable from frame to frame than the margin path's?EPA stops at a tolerance, and when two polytope faces sit at almost the same distance from the origin, a tiny change in position can make the other face win, so the normal jumps. The distance path's normal comes from closest points that move smoothly with the bodies, which is why margins keep resting contacts there.
- GJK returns the penetration depth once the simplex encloses the origin.
- EPA should run for every touching pair, including shallow resting contacts.
- A collision margin is visual padding with no effect on contact depth.
- A capsule's radius must be baked into a hull before GJK can handle it.
- Larger margins are always more stable, so make them as big as possible.