Lobster Quest, Part 2: Stitching a Cube
This is the second of three parts from my Patreon devlog of the first
Emoji Quest, in Lobster.
It joins two posts of the same day, November 23, 2019: how the ents learned
to wander the six faces of a cube, and how a camera learned to follow them.
The tables here are the ones the game walks still: the world of
Emoji Quest is a cube whose faces are stitched the same
way, and its
topology.js
still names its seams by the letters of the stitch diagram below.
In the last part, I showed Langton’s Ant as a cellular automaton with the potential for an “entity” in every cell. I also rendered a 3D cube. I’ve since combined these two, so Langton’s Ents now wander the faces of a cube. This was a challenge for me, and I discovered a few wrinkles while dragging it to the desired æsthetic.
In the video, you will notice that entities transition over the edges of the cube. Although each face has its own intrinsic orientation, the headings of the entities are preserved when they cross over edges.
Neighborhoods #
The original Langton’s Ents ran around on a square that wrapped around the edges. The simulation doesn’t actually care about the “topology”. It stores all of its data in flat arrays. The topology is defined by “neighborhoods”, an array of arrays. The outer array has as many cells as the world, and the inner array has an entry for each of the cardinal directions from the corresponding cell.
This is the layout of a 3×3 grid of neighborhoods.
N E S W
0: [6, 1, 3, 2] at (0, 0)
1: [7, 2, 4, 0] at (0, 1)
2: [8, 0, 5, 1] at (0, 2)
3: [0, 4, 6, 5] at (1, 0)
4: [1, 5, 7, 3] at (1, 1)
5: [2, 3, 8, 4] at (1, 2)
6: [3, 7, 0, 8] at (2, 0)
7: [4, 8, 1, 6] at (2, 1)
8: [5, 6, 2, 7] at (2, 2)
So, if I have an array of entity numbers, or an array of headings, or an array of rule numbers (cell colors), whatever value is at index 4 corresponds to the center of the world, at coordinate (1, 1).
So, making this work on a cube is a matter of laying out six squares’ worth of cells, knitting the neighborhoods inside each square, and stitching the edges together.
Faces #
For the cube rendering demo, you may recall that each face has an arbitrary orientation. For the purposes of orienting textures, there are four possible ways to rotate the texture (and four more with the opposite normal vector, facing the other direction), and we can fiddle with those by changing the order we trace the corners.
Wouter van Oortmerssen’s voxel rendering demo highlights a trick for
addressing the vertices of a cube using the three bits of an integer as a
coordinate in three-space.
The characters '0' through '7' carry those three bits at the bottom
of their codes.
-----ZYX CHR corner
00110000 '0' origin
00110001 '1' x
00110010 '2' y
00110011 '3' xy
00110100 '4' z
00110101 '5' xz
00110110 '6' yz
00110111 '7' xyz
The last row read “zyz” in the post and in the code; it is xyz.
From this, I chose an arbitrary (not random!) set of face orientations, based on the æsthetics I’ve planned for the game I was calling Tale ten years ago, Die as in Dice more recently, and now Emoji Quest. The faces of the world are numbered according to the pips of a right-handed die. With faces 0, 1 and 2 sharing the origin vertex, and 3, 4 and 5 sharing the antipodal corner of the world, this has a pleasant geometric sensibility. With face 0 being between vertices 1 and 2, then face 1 between 1 and 4, then face 2 between 2 and 4, you can see that the vertex numbers generally ascend with the face value.
inverted clockwise
Z 4---5 .---. .---.
\ |\ |\ 1540->1 |\ 1 \ | |\ 5<-5464
0--X | 0---1 | .---. | 5 | .
| | | | | 0462->2 |2| | | |3| 3<-5137
Y 6-|-7 | ' | 0 | '---' |
\| \| 3102->0 \| | \ 4 \| 4<-7326
2---3 '---' '---'
counter-clockwise
The game uses opposition and triads thematically. The faces could be arranged randomly, but as Ender Wiggin would no doubt point out, anchoring the player’s reference to minimize disorientation is important. The middle faces, 2 and 3 (ordinally), serve as the south and north poles and are oriented so their tops and bottoms connect with their adjacent numbered faces. The remaining faces are equatorial and oriented with their north and south edges stitched to the same pole.

This means that when it’s possible to walk over the edge of a face to the face with an adjacent number, the player’s orientation doesn’t have to change. Only when traveling between the equator and the poles between non-adjacent numbers do we have to roll the camera.
Stitches #
Which brings us back to stitching together squares for our cellular automaton. In the next diagram, you’ll see that there are six faces, and they’re all oriented so their (0, 0) origin is on the top left. The arrows are the directions the needle has to iterate to stitch neighboring cells across each edge. If two edges have the same letter, they are the same edge, after the cube has been folded.
+ ←L +
I 3 ↑
↓ K
+ I→ + J→ + K→ + L→ +
B 5 C 4 D 0 A 1 B
↓ ↓ ↓ ↓ ↓
+ E→ + F→ + G→ + H→ +
↑ 2 E
G ↓
+ ←F +
You’ll note in particular that edges F and L run in opposite directions. That means that the camera will have to roll 180° when the player crosses that edge.
So, it’s not sufficient to just stitch the neighborhoods. It’s also necessary to track the implied turn that the entity will have to make when it crosses the edge. This chunk of opaque code shows each of the stitches, parameterized by the edge length and face area of the desired cube. This drives the creation of two data sets: neighborhoods and reorientation turns. It works by iterating two edges at once, writing both of the neighbor indices and symmetric rotations.
The first and second arguments are the indices of the respective starting cells. The cells of each face are laid out flat, in row-major order within the face, and in order of the face number. That means that the top-left cell of face 0 is at index 0, and the top-left corner of each subsequent face advances the index by the area of the square. We compute the position of every corner of every face by adding or subtracting one, the length of an edge, or the whole area of a face.
The third and fourth arguments are the strides to take to reach the next
respective cells along the pair of edges.
A stride of 1 or −1 moves left or right.
Strides of size or -size move down or up.
The fifth and sixth arguments are the direction toward the adjacent face, relative to the orientation of the respective face. These are not always symmetric! The last argument is whether the orientation change requires the entity to turn clockwise, counter-clockwise, round about, or to continue straight ahead.
stitch(area*6-size, area*2+size-1, 1, size, southward, eastward, cw) // E 5-2
stitch(area*5-size, area*3-1, 1, -1, southward, southward, about) // F 4-2
stitch(area*1-size, area*3-size, 1, -size, southward, westward, ccw) // G 0-2
stitch(area*2-size, area*2, 1, 1, southward, northward, steady) // H 1-2
stitch(area*0+size-1, area*1, size, size, eastward, westward, steady) // A 0-1
stitch(area*1+size-1, area*5, size, size, eastward, westward, steady) // B 1-5
stitch(area*5+size-1, area*4, size, size, eastward, westward, steady) // C 5-4
stitch(area*4+size-1, area*0, size, size, eastward, westward, steady) // D 4-0
stitch(area*5, area*3, 1, size, northward, westward, cw) // I 5-3
stitch(area*4, area*4-size, 1, 1, northward, southward, steady) // J 4-3
stitch(area*0, area*4-1, 1, -size, northward, eastward, ccw) // K 0-3
stitch(area*1, area*3+size-1, 1, -1, northward, northward, about) // L 1-3
The stitch function itself is short.
def stitch(a, b, as, bs, dab, dba, turn):
for(size) i:
let ai = a + as * i
let bi = b + bs * i
neighborhoods[ai][dab] = bi
neighborhoods[bi][dba] = ai
turns[ai][dab] = turn
turns[bi][dba] = -turn
Both are from
topology.lobster,
as it stood when the ents first crossed the edges.
Over the edge #
The final wrinkle was animating the transitions over the edges. There was no problem rendering the entity as it slid off the edge of one face, but the drawing code needed more nuance to handle the corresponding animation onto the destination face.
The original algorithm for the round square was pretty simple. If the entity left the square, it would be rendered again on the other side of the square, using some modulo math on its coordinate.
The more general algorithm has to walk all the edges and look at the neighboring index over the edge. If the entity on the other side is transitioning onto this face (accounting for the heading change over the edge!), we draw it as it transitions (again, accounting for the orientation change!).
Josh Corbin and I are now both building a branch for Emoji Quest. You can find the working Langton’s Ents cube on GitHub in our “moji” branch.
Browsing a cube #
Langton’s Ents wandered over the surface of a cube. The next challenge is to get you to wander the surface of a cube.
I’ve made a demo with a camera that hovers over a position on the cube. The camera follows the orientation of the face of the cube it’s looking at, as indicated by the orientation of the face number. All the transitions from face to face are animated. For the sake of the demo, each face only has one cell, but the camera will follow any of the cell topologies we generated for the cellular automaton.
The cube browser uses the topology and face-to-face transition tables for the camera pitch and yaw. I think, ideally, I’d be able to roll the camera as well, for the 180° orientation changes, but that’s an exercise for future me.
All of the camera position, yaw and pitch transitions are driven by a few tables. The “corner vertex” array is the same as in the Langton cube demo, using 0 to 7 as bit fields of ZYX coordinates. We have a table for the initial camera pitch and yaw on faces 0 to 5 of the cube. The origins are the coordinates of the top-left corner vertex of each face. We also track the major and minor axis vector of each face, so we can move the camera over every cell by its two-dimensional (x, y) coordinate on that face. The x is the major axis component and y is the minor axis component.
// cube vertices
let cv = map(8) c: xyz_v(map(3) i: float(c & (1 << (2 - i)) != 0))
let camera_pitches = [0, 0, 90, -90, 0, 0]
let camera_yaws = [-90, 0, 0, 180, 180, 90]
let camera_origins = [cv[3], cv[1], cv[0], cv[5], cv[7], cv[5]]
let camera_majors = [-cv[2], cv[4], cv[4], -cv[4], -cv[4], cv[2]]
let camera_minors = [-cv[1], -cv[1], cv[2], cv[2], -cv[1], -cv[1]]
let pitch_transitions = [
[-90, 0, 90, 0],
[-90, 0, 90, 0],
[-90, -90, -90, -90],
[90, 90, 90, 90],
[-90, 0, 90, 0],
[-90, 0, 90, 0],
]
let yaw_transitions = [
[-90, 90, 90, -90],
[180, 90, 0, -90],
[0, 90, 180, -90],
[180, 90, 0, -90],
[0, 90, 180, -90],
[90, 90, -90, -90],
]
Of particular interest, it’s necessary to track the pitch and yaw transition tables because it’s reasonable for yaw to wrap around the cube. For purposes of animation, +90° is very different than −270°!
This demo is checked in if you would like to give it a spin.
Next, the devlog, from the roadmap to the sky box.
Lobster Quest:
- Langton’s Ents
- Stitching a Cube
- The Devlog, from Roadmap to Sky Box
First published on Patreon in two posts, “Langton’s Ents Wander a Cube” and “Browsing a Cube”, November 23, 2019.