Post by James Cook [home]
January 17, 2022
In which I show you a demo I had a lot of fun making.
If you follow this link, you'll be taken to an unusual house.
You start on an enclosed lawn, surrounded by four walls with a doorway in one of them. If you go in and explore, you might begin to feel trapped. You'll find you can visit five different rooms in the house, or go back outside to the lawn, but there's no way to go anywhere else. It's not because there are walls stopping you from leaving. The house, and the little yard, are the whole universe. There's nothing else. If you keep going forward long enough, you end up back where you started. (Try to find a clear path in the house where you can do this without running into a wall.)
Some other things to notice:
This house exists in a geometric world called a 3-sphere, which is higher-dimensional version of a sphere (aka 2-sphere).
It's easiest to explain with an analogy.
Suppose you took a two-dimensional person from a two-dimensional universe and put them on the surface of a sphere.
Everything in this new universe is confined to the sphere, including light.
This flatlander would experience many of the same things you did in the 3-sphere house:
If you're familiar with the definition of a 3-sphere as 4-D points (x,y,z,w) with magnitude 1, the six rooms of the house are each centred on one of the axes: the yard is at (0,0,-1,0), the next room is at (0,0,0,1), and the next room forward is centred at (0,0,1,0). The floor is the plane y=0, the point (0,1,0,0) sits directly above all of the rooms, and if you check "hide floor" and look straight down, the red box you see is at (0,-1,0,0).
It may seem like you're just walking on the inside of a big balloon with stripes drawn on it. That's close to the truth, but there are a couple of differences:
A torus is another shape which wraps around: if you keep going far enough, you'll get back where you started.
The 3-sphere is different from a torus, though. Here's one way to see. Go and look for a place in the house where you can walk forward forever without running into a wall. If you were to draw a line on the floor while you did that (there's an idea for a feature...), that line would split the house into two parts. Each part would have one room (or the yard) at its centre, and there would be no way to walk from one part to the other without crossing the line you drew. If you tried the same thing on the torus, the floor would still be in one piece.
I've been interested in non-Euclidean geometry for a long time. I wanted to get some intuition for it, and I think the best way is to put myself inside and explore.
This is done using WebGL, an API for drawing 3D graphics in a browser. You might think it's only designed to draw ordinary, Euclidean things, but it turns out it's perfectly suited to rendering this world as well. WebGL (and OpenGL) works with four-dimensional coordinates (x,y,z,w). Normally, you'll be advised to just set that w coordinate to 1 all the time. If you do that, the x, y and z coordinates will behave like ordinary 3-D geometry. I just ignored that rule and used the coordinates to represent points on the 3-sphere instead. It all works out. (You have to be a bit careful if you want to put textures on things without seeing seams between the triangles, but it can be done. The key is to make sure every flat surface is still a flat surface in 4-D.)
Here's a link the the source code. It's written in Idris2, an experimental language with a powerful type system, and uses the idris2-dom library to interact with your browser. I had a lot of fun with it, but spent a lot of time learning to appease the typechecker, and also had to submit some patches in order to get my demo to run without overflowing the stack.
The code actually includes a constructive solid geometry library, in the src/SolidGeometry
directory.
I used it to build the walls (a big block minus smaller blocks for the doors and windows) and some of the furniture around the house.
It doesn't always work, and it's quite slow, and if you read the code while knowing anything about computational geometry, I can only assume you'll quickly realize I made it up as I went along.
But I had fun making it!
The input is a bunch of planes which split the world into cells, and a rule saying which cells are "inside" or "outside"; and the output is a bunch of triangles separating inside from outside.