← Back to the game

Games in 2-Manifolds

The math behind the Non-Euclidean Spaceship: one formula for the three geometries, with the curvature \(K\in\{0,1,-1\}\) as a parameter, and a closed surface for each, so nothing ever flies off to infinity.

The three surfaces

Each surface is drawn as a fundamental domain: a polygon whose edges are glued in pairs. Leaving through an edge means coming back through the edge it is glued to. In the game, glued edges are drawn in the same color.

Euclidean (\(K=0\)): flat torus

The square \([-1,1]^2\) with opposite sides glued by the translations \(z\mapsto z\pm 2\) and \(z\mapsto z\pm 2i\).

Elliptic (\(K=1\)): projective plane

The unit disk is the upper hemisphere of the sphere, under stereographic projection. Its boundary circle is the equator, and each point is glued to its antipode $$z\mapsto -\frac{1}{\overline z}=-\frac{z}{|z|^2}.$$ This map reverses orientation, so the surface is non-orientable: an object that crosses the circle comes back mirrored.

Hyperbolic (\(K=-1\)): genus-2 surface

A regular octagon in the Poincaré disk with interior angles \(\pi/4\), so that eight copies meet at each vertex. Its sides are arcs of circles orthogonal to the unit circle, centered at \(C e^{ik\pi/4}\) with radius \(R\): $$\ell=\tfrac12\left(2^{1/4}+2^{-1/4}\right),\qquad C=\frac{\ell}{\cos(\pi/8)},\qquad R=C\tan(\pi/8),\qquad C^2=1+R^2.$$ Side \(k\) is glued to side \(k+4\) by a reflection across a line through the origin, followed by the inversion in the circle of side \(k+4\).

Metric

$$ds^2=\frac{4(dx^2+dy^2)}{(1+K(x^2+y^2))^2}=\frac{4\,dz\,\overline{dz}}{(1+K|z|^2)^2}$$ $$\arctan_K(z)=\sum_{n=0}^\infty \frac{K^n z^{2n+1}}{2n+1}= \begin{cases} \operatorname{arctanh}(z), & K=-1\\ z, & K=0\\ \arctan(z), & K=1 \end{cases}$$ $$d(z_1,z_2)=2\arctan_K\left|\frac{z_1-z_2}{1+Kz_1\overline{z_2}}\right|$$

Collisions use this distance, minimized over the glued copies of the second point. On the projective plane, the antipode is at distance \(\pi\), so the distance there is \(\min(d,\pi-d)\).

Isometries

Translation (\(a\to 0\))

$$T_a(z)=\frac{z-a}{1+Kz\overline{a}}$$

Rotation around 0

$$R_\theta(z)=e^{i\theta}z$$

Reflection through 0

$$R(z)=-z$$

Mirror (\(a\leftrightarrow0\))

$$M(z)=\frac{a^2}{|a|^2}\frac{\overline{a-z}}{1+Ka\overline{z}}$$

Perpendicular bisector of \(\overline{0a}\)

$$K|a|^2(x^2+y^2)+2a_xx+2a_yy=|a|^2$$

If \(K\neq 0\),

$$\left(x+K\frac{a_x}{|a|^2}\right)^2+\left(y+K\frac{a_y}{|a|^2}\right)^2=\frac{1}{|a|^2}+K$$

Midpoint of \(\overline{0a}\)

$$m=\begin{cases} \frac{1}{2}a, & K=0\\ K\dfrac{\sqrt{1+K|a|^2}-1}{|a|^2}\,a, & K\neq0 \end{cases}$$

Each sprite is drawn by pulling every pixel \(z\) back with \(T_a\) (with \(a\) the object's position), then dividing by its direction \(e^{i\theta}\), and reading the texture there.

Geodesic circle \((O,R)\)

$$p=\tan_K\left(\frac{R}{2}\right)$$ $$\left(x-\frac{O_x(1+Kp^2)}{1-p^2K^2|O|^2}\right)^2+ \left(y-\frac{O_y(1+Kp^2)}{1-p^2K^2|O|^2}\right)^2= \left(\frac{p(1+K|O|^2)}{1-p^2K^2|O|^2}\right)^2$$

Geodesic update rule

Objects move along geodesics, integrated with a variational integrator: the Lagrangian is discretized, and the discrete Euler–Lagrange equations are solved for the next position.

$$L(q_x,q_y,p_x,p_y)=\frac{4(p_x^2+p_y^2)}{(1+K(q_x^2+q_y^2))^2}$$ $$L_d(q_x,q_y,q_x',q_y',h)=hL\left(q_x,q_y,\frac{q_x'-q_x}{h},\frac{q_y'-q_y}{h}\right)$$

Given \((q,p)\), the step \(\Delta q\) solves \(p=-\partial L_d/\partial q\) (a quadratic system, solved in closed form in solveSystem), and the new momentum is \(p'=\partial L_d/\partial q'\):

$$\begin{aligned} p_x &= \frac{8}{h(1+K|q|^2)^2}\left(\Delta q_x+\frac{2Kq_x\left((\Delta q_x)^2+(\Delta q_y)^2\right)}{1+K|q|^2}\right)\\ p_y &= \frac{8}{h(1+K|q|^2)^2}\left(\Delta q_y+\frac{2Kq_y\left((\Delta q_x)^2+(\Delta q_y)^2\right)}{1+K|q|^2}\right)\\ q_x' &= q_x+\Delta q_x, \qquad q_y' = q_y+\Delta q_y\\ p_x' &= \frac{8\Delta q_x}{h(1+K|q|^2)^2}, \qquad p_y' = \frac{8\Delta q_y}{h(1+K|q|^2)^2} \end{aligned}$$

The ship's heading is not tied to its velocity: it is parallel transported along the geodesic. The model is conformal, so this means keeping its angle with the velocity. After each step, the heading turns by the same angle as \(p\).

Aiming

The saucer shoots along the shortest geodesic to the ship. Over the glued copies \(b'\) of the ship's position, it takes the one nearest to its own position \(a\). The translation \(T_a\) takes \(a\) to 0, where geodesics are straight lines, and \(T_a'(a)=1/(1+K|a|^2)\) is a positive real, so the direction is $$\frac{T_a(b')}{|T_a(b')|}.$$