Slip & Diedevlog

Deep dive · Episode 6 ·

The stones were cheating

The ruined stair and the Maya temple are built from procedural stones, and for months those stones closed their joints by sitting inside each other. How we cut walls and arches into stones that tile exactly, the tests that keep them out of each other and out of the air, and the real cause of the temple floor that shimmered with the camera standing still.

A ruined stone arch lit by torches at the top of a broken stone stair on a grassy slope, beside a grey rock face

The walls of the ruined stair and of the Maya temple are generated stone by stone, and the stones used to close their joints by overlapping.
We made every stone tile the wall exactly, added tests for overlaps, holes and floating pieces, and found that the temple's shimmer came from two stones drawn on the same plane.

With the camera standing still, the floor of the temple would not settle: every few frames the cracks in the paving jumped and the stone went from grey to green and back. The obvious suspect was TAA: with FXAA the floor stayed still. It turned out to be the last of several problems with stones that were not where they should be: in the air, inside each other, on top of each other. The first ones were in the arches.

Between 13 October and 5 November we built a ruined stair on the slope next to the temple, with two arches across it, then the Maya temple that the big rolling boulder smashes, then dry-stone walls with breaches.

Courses counted from each stone’s feet

The first defect was in the arches: voussoirs (the wedge stones of an arch) and a jamb (the upright at its side) hanging in the air. The generator laid stones in columns, and every column started from the ground under its own centre and counted its courses from there. Beside the stair the ground climbs steeply, and in a running bond each stone straddles two below it, so neighbouring columns started two or three courses apart and stones hung off the slope.

The courses now belong to the world: one set of bed heights along the whole wall. The ground only decides where a column starts. A stone is laid when it reaches above the lowest ground under its footprint, minus one buried course, so every point of the wall has an unbroken stack from below the ground up.

Overlap was the mortar

Once the arches stood on their courses, I noticed stones going into each other. That was by design. Each stone was a box, widened by a joint_overlap parameter, lifted by up to a tenth of a course and turned on its own, and the neighbours closed the joints by interpenetrating.

First we measured it. For the test every stone is its hexahedron. Bevels and chips only ever remove material, so the hexahedron contains the real stone, and if two hexahedra do not overlap, the stones inside them cannot either. For two convex polyhedra, the penetration depth is the smallest overlap of their projections over the candidate separating axes: the face normals of both and the cross products of their edges (the separating axis theorem). The tolerance is 2.5 mm, float noise plus the width of a hairline joint, nothing a camera can see. The 14 walls of our test level had 6,109 interpenetrating pairs. The deepest went 120 mm in.

The fix lays the wall out the way a mason would on paper. Each course is a band of the wall’s elevation, cut into stones that tile it: a stone runs from one joint to the next and shares it exactly with its neighbour. Whatever makes a wall look laid by hand now changes a stone only along the wall’s normal, where it cannot reach a neighbour: depth, protrusion, and even the slight turn, which tilts the stone’s faces while its ends stay on the joints.

A box cannot close a joint that isn’t square, so each stone now carries its eight real corners. The four corners of a joint move together along one direction, so the joint is a plane for both stones. The bevelled box is mapped onto that hexahedron by trilinear interpolation of the corners. Bevels and chips follow the shape, and two stones sharing a joint share exactly the same face. The dry-stone wall gets its irregular look inside the same rule: some faces lose corners and read as five- or six-sided.

After the change the test counted zero pairs, on the level and on eight typical cases.

An arch ring planned with the courses

The voussoirs used to be boxes laid along the curve. On tight arches their joints opened into dark wedges, and stretching them to close the wedges made them overlap.

The ring is now planned together with the courses. Every joint of the ring is a straight line along the arch’s normal, from the intrados to the extrados (the inner and outer curves of the ring), ending exactly on a bed joint (the horizontal joint between two courses). Each voussoir is then a wedge with two corners on each curve, and the course stone beside it is cut along its outer face. The ring’s thickness, about one and a half courses, is chosen so that the crown lands on a bed joint too.

The arch is a half ellipse. Pushing a point of the ellipse out along its normal by the ring thickness gives the joint’s end on the extrados, so finding the joint that lands on a bed is a search on one angle:

// Half ellipse with half-width `a` and rise `b`; theta = PI/2 at the crown.
fn offset_point(a: f32, b: f32, theta: f32, thickness: f32) -> Vec2 {
    let (sin, cos) = theta.sin_cos();
    let normal = Vec2::new(cos / a, sin / b).normalize_or(Vec2::Y);
    Vec2::new(a * cos, b * sin) + normal * thickness
}
// The offset curve falls from the crown (the top) to the springing
// (where the arch leaves the wall): 40 bisection steps (more than f32
// needs) put the joint on the bed's height.
3 December. The banana climbs the ruined stair and passes under one of its torch-lit arches, built from wedge-shaped stones.

Taking the overlap away opened holes

Without the overlap, you could see sky through the dry-stone wall in a dozen small diamonds. Where the rounded corners of three or four stones meet there is a small tunnel, and the overlap used to fill it. The wall’s core, a box behind each stone, could not close it: it sits behind the bevels, and the tunnel runs exactly through the space the bevels cut away.

A real wall has rubble in there, so each stone now gets one more hexahedron, a joint fill. It sits behind the stone’s bevels and reaches the joint on every side where a neighbour really is. On a free side it stays behind the bevel, so it never shows, and it never leaves its own stone’s hexahedron, so the overlap test still passes.

This one is tested with rays. Around every stone that has neighbours on all sides, a grid of parallel rays 3 mm apart crosses the wall straight and at about 20 degrees in four directions. A ray that crosses a point where the wall should be solid without hitting any triangle is a hole. The eye had caught a dozen of them. The rays found the rest: without the fill, all 1,395 interior stones of the test level had holes, and 57,774 rays went through. With the fill, none did.

Nothing floats

A stone on a ruined crest is only laid if at least three of five probes under its bed find the ground or another stone, so it never hangs more than halfway over the void. A test reads the level file, without starting the engine. A rigid object must have terrain or pond water under its whole footprint, with no corner more than 15 cm above it. A 1-2 m block resting across a slope can have a corner 14 cm off the ground and still look at rest. Anything allowed to hang, like a torch on a wall, is listed with its reason.

The test found a felled column 75 cm in the air. Objects that drop to the ground are placed at the terrain height plus half the collider’s height, measured in the object’s own frame. A column lying on its side is a quarter turn, so its own height is its length, and it had been lifted by half of it. Objects that drop to the ground may now only turn around the vertical, because that height cannot be trusted after any other turn. The felled column is no longer dropped: it is placed lying, at a height we measured.

The temple floor that couldn’t pick a stone

Back to the floor, which was reported on 3 November and fixed the next day. Eight consecutive frames of the same patch, enlarged, showed the texture itself changing: two surfaces were fighting over the same pixels.

The temple’s platform is built from static boxes sunk into the ground; overlapping volumes there are harmless, overlapping faces are not. The band on top of each tier had its top face exactly level with the tier’s floor, the top step ran under the floor, and the stones of the stair’s side walls had their long faces on one plane. Two faces on one plane, facing the same way, are drawn at the same depth, and each pixel shows whichever the rounding favours. The stones had different tints and textures. TAA shifts the projection by a fraction of a pixel every frame, which changes the rounding, so the winner kept changing. FXAA has no jitter, so the fight stayed frozen in one pattern and looked fine.

Two rows of eight enlarged crops of the same patch of stone floor: in the top row the cracks and tint change from frame to frame, in the bottom row they stay the same
4 November. The same patch of the temple floor in eight consecutive frames with a still camera. Top: before, where the cracks and the tint change every frame. Bottom: after.

The measurement that confirmed it is cheap. With a still camera and a fixed time step, take the mean absolute difference of the grey values (0-255) between frames k apart, for k = 1, 2, 4, 8 and up. Bevy’s TAA cycles through eight offsets of a Halton (2, 3) sequence, so the jitter repeats every eight frames: a flicker it causes shows at lags that are not multiples of 8 and almost disappears at lag 8. On the floor it was 1.60 at lag 1, 3.44 at lag 4 and 0.35 at lag 8. What was left at lag 8 was SSAO noise, which repeats every 64 frames under TAA: in an earlier run, turning SSAO off took lag 8 from 0.30 to 0.016.

The fix touched only the shape of the platform: every visible face now belongs to one stone. The band sits 1.5 cm below the top of its tier, the top step stops against the floor instead of running under it, and each stone of the stair’s side walls is set in 6 mm towards the stair from the one below. Afterwards the floor measured 0.057 at lag 1 and 0.128 at lag 4, and the stair treads went from 2.30 to 0.101 at lag 1.

A test keeps it that way. For every pair of pieces it looks for faces on the same axis, facing the same way and lying on the same plane. It samples their shared rectangle on a 24 × 24 grid, and any sample above the ground and outside every other stone fails. Opposite faces are ignored, because back-face culling draws only one.

3 December, camera standing still. The temple stair in daylight: the steps and the bands along the tiers keep their pattern from frame to frame, and only the flames and the leaves move.

Still missing: the wall generator only knows half-ellipse arches. The corbelled arches (courses that step inwards until they meet) exist only in the temple, which builds them with its own code.

This is the technical companion to Episode 6: Shoot a torch and the jungle burns.

Spotted a mistake, or solved the same problem another way? Write to devlog@slipanddie.com.

← All episodes