How do you build a stable box-box contact manifold when two tumbling boxes settle face to face but rock and jitter on one deepest point per frame?
answer
- one point is a pivot
- reference face and incident face
- clip against side planes
- keep four, remember them
basics
~20 sBuild several points at once: clip the other box's most anti-parallel face against the side planes of the reference face that won the separating-axis test, keep points below it, reduce to four, and match them across frames by feature ids.
solid answer
~50 sA single deepest point gives the box one pivot, so it tips until another corner becomes deepest, and the point jumps back and forth. Build a **manifold** instead. The face whose normal won the separating-axis test, with a bias toward face axes and last frame's choice, is the **reference face**; on the other box, the face whose normal is most anti-parallel to it is the **incident face**. Clip the incident quad against the four side planes of the reference face (Sutherland-Hodgman), which leaves up to eight points; keep those whose signed distance `s` above the reference plane is at or below a small margin, with depth `-s`. Reduce to four that span the widest area, deepest first. If an edge axis won, emit one point from the closest points of the two edges. Finally tag each point with a **feature id** naming the faces and edges that produced it, so next frame's matching point is recognised and its accumulated impulse carries over.
code
pseudocode · 25 linesfunction clipPolygon(points, n, offset): // keep the side where dot(n, p) <= offset
out = []
count = length(points)
for i in 0 .. count - 1:
p = points[i]
q = points[(i + 1) % count]
dp = dot(n, p) - offset
dq = dot(n, q) - offset
if dp <= 0:
out.push(p)
if (dp < 0 and dq > 0) or (dp > 0 and dq < 0):
t = dp / (dp - dq)
out.push(p + t * (q - p)) // crossing point on edge p-q
return out
function faceManifold(ref, incidentVertices, margin):
poly = incidentVertices // 4 world-space points
for side in ref.sidePlanes: // 4 planes, normals pointing out of the face
poly = clipPolygon(poly, side.normal, side.offset)
contacts = []
for p in poly:
s = dot(ref.normal, p) - ref.offset // signed distance above the reference face
if s <= margin:
contacts.push(Contact(point = p - ref.normal * s, depth = -s))
return reduceToFour(contacts)go deeper
Recall that a box resting on a face needs several contact points, and that one point lets it pivot and rock.
Explain reference and incident faces, why the incident face is the most anti-parallel one, and what clipping against side planes produces.
Diagnose rocking from its symptom, and show the full pipeline: hysteresis on the axis choice, clipping, reduction to four points and feature ids that persist across frames.
Decide where clipping-based manifolds pay off against incremental caches for general convex shapes, given the mix of boxes, hulls and stacks your game must keep stable.
## Why one point per frame rocks Picture two boxes that have tumbled and now rest face to face. If narrow phase reports only the single deepest point each frame, the solver pushes at that one corner. A push at one point can stop the box sinking there, but it also gives the box a pivot to rotate around, so the opposite corner dips. Next frame that corner is deepest, the point jumps there, and the box tips back. The result is the classic **rocking and jitter** of a resting box. A face resting on a face needs several contact points spread across the touching area, a **contact manifold**, so the pushes balance each other in one solve. ## Reference and incident faces The separating-axis test that found the overlap already says how the boxes touch: - If the minimum-overlap axis is a **face normal**, that face is the **reference face**, and its box is the reference box. If the face belongs to the second box, the reference normal is the flipped contact normal. - On the other box, the **incident face** is the face whose outward normal is **most anti-parallel** to the reference normal, the face that looks back at the reference face. - If an **edge-edge** axis won, there is no face to clip: the manifold is one point, from the closest points between the two edges. - Add **hysteresis**: choose a face axis over an edge axis, and the previous frame's reference face over a new one, unless the alternative is better by a small tolerance. Otherwise near-ties flip the reference face and the points jump. ## Clipping, step by step 1. Take the incident face's four vertices in world space. 2. Build the four **side planes** of the reference face: planes through its edges, perpendicular to the face, with normals pointing out of the face. 3. Clip the incident polygon against each side plane in turn with **Sutherland-Hodgman clipping**: keep vertices inside the plane and add the crossing point wherever an edge crosses it. 4. Each clip can add at most one vertex, so the four-sided incident face ends with at most **eight** points. 5. For each remaining point `p`, compute its signed distance above the reference plane, `s = dot(n, p) - offset`. 6. Keep points with `s` at or below a small margin; each kept point has depth `-s` and can be projected onto the reference face with `p - n * s`. Vertices alone are not enough: when one box is rotated on the other, the touching region's corners are often where an incident edge crosses a reference edge, which is exactly what clipping produces. ## Reducing to four points A solver gains little from more than four points on one face and pays for each one, so keep four that span the contact: 1. the **deepest** point, 2. the point **farthest** from it, 3. the point that makes the **largest triangle** with those two, 4. the point that adds the **most area** on the far side of that triangle. Four well-spread points give pushes at the edges of the touching region, which is what stops rotation about any axis lying in the contact plane. ## Persistent contacts and feature ids A stable stack needs contacts that **persist** from frame to frame, not a fresh set each time: - Give each point a **feature id**: the reference face, and for a clipped point the incident edge and the reference side plane that produced it, or the incident vertex if it survived unclipped. - Next frame, a point with the same id is the **same contact**, so its stored state, such as the impulse the solver accumulated, carries over and lets the solver start from last frame's answer. - Points whose ids disappear are dropped; new ids start fresh. ## Incremental manifolds for GJK and EPA shapes GJK and EPA return one point per call, so general convex shapes build a manifold over several frames: | Approach | How points arise | Strength | Weakness | |---|---|---|---| | Clipping | All points in one frame | Full manifold at once, ids from features | Needs faces: boxes and polyhedra | | Incremental cache | One new point per frame, up to four kept | Works for any convex shape | Takes frames to fill; points must be pruned | An incremental cache stores points in each body's local space, adds the new point, replaces the one whose loss shrinks the area least when full, and drops points that have separated or drifted sideways past a threshold, measured by re-transforming the stored points with the bodies' current poses.
- How do you stop the reference face flipping between the two boxes on a near-tie?Add hysteresis to the axis choice: prefer a face axis over an edge axis, the first box's face over the second's, and last frame's axis over a new one, unless the alternative's overlap is smaller by a relative tolerance plus a small absolute one. A near-tie then keeps the previous choice, so the points and their ids stay put.
- What goes into a feature id for a clipped box contact?Enough to name the geometry that produced the point: the reference face index, and for a clipped point the incident edge and the reference side plane it was clipped against, or the incident vertex if it survived unclipped. While the boxes keep the same configuration the ids repeat each frame, so each point is matched with its predecessor.
- How do you pick four points from the eight clipping can produce?Keep the deepest point, then the point farthest from it, then the one forming the largest triangle with those two, then the one adding the most area on the opposite side. That keeps the spread of the contact region, which is what lets the solver resist rotation, while capping solver cost.
A table balanced on a single leg tips toward whichever side is heavier, and lands on another corner; a table with four legs spread to its corners stands still. A one-point contact is the single leg; a four-point manifold is the four.
- One deepest contact point is enough if the solver runs more iterations.
- Every incident box vertex inside the other box is a complete set of contacts.
- The reference face can be chosen fresh each frame without hysteresis.
- Clipping a box face against another can produce at most four points.
- Persistent contacts only save time and do nothing for stacking stability.