A fascinating technical breakdown shared on Hacker News explains the complex mathematics behind a famous Super Mario 64 glitch known as “Parallel Universes.” While often viewed as a speedrunning trick, this concept provides a perfect case study for understanding topology and state spaces: critical knowledge for anyone developing AI agents or reinforcement learning models for 3D environments.
Here is a guide to the topological concepts that allow Mario to travel through invisible worlds.
Quick Start
- What you will learn: The definitions of covering spaces, homeomorphisms, and universal covers, and how they physically manifest in game engine collision logic.
- Why it matters: AI agents, particularly in Tool-Assisted Speedruns (TAS), exploit these mathematical structures to navigate environments in ways humans cannot visually perceive.
Step 1: Define the Covering Space
First, we establish the mathematical foundation. A covering space is essentially a larger space that maps onto a smaller one in a repetitive way.
- Definition: A covering space of a topological space X is a topological space C together with a continuous surjective map p: C → X.
- The Condition: For every point x ∈ X, there exists an open neighborhood U of x such that the inverse image p⁻¹(U) looks like a stack of copies of U (called the fiber).
In game terms, this explains how a single level map can mathematically exist as multiple “stacked” layers, even if the game only renders one.
Step 2: Understand Homeomorphisms
Next, we define how we determine if two spaces are topologically equivalent.
- Definition: A homeomorphism is a bijective map f: X → Y where both f and its inverse f⁻¹ are continuous.
- Implication: If such a map exists, X and Y are “homeomorphic” (X ≅ Y).
This concept allows us to simplify complex 3D game maps into standard shapes (like tori) to analyze their properties. It is the reason a doughnut is topologically the same as a coffee cup.
Step 3: Define the Universal Cover
This step identifies the “ultimate” version of the covering space.
- Definition: A universal cover X̃ is a covering space of X that is “simply connected,” meaning it has no nontrivial loops.
- Uniqueness: The universal cover is unique up to homeomorphism.
For an AI agent, the universal cover represents the “unrolled” version of a looping world, allowing for pathfinding algorithms that don’t get stuck in loops.
Step 4: Visualize with “Bob’s World”
To make this concrete, the guide uses an analogy of “Bob,” who lives on a doughnut-shaped world (a torus).
- The Setup: Bob drives on a road that loops back to the start. Topologically, his world is S¹ × D² (a circle times a disk).
- The Stretch: If you “unroll” the doughnut, you get a long, infinite road. This is the covering space.
- The Stack: If you stack copies of Bob’s world on top of each other, Bob wouldn’t notice as long as movements in the base world are mirrored in the copies.
This visualization helps developers understand how an agent might perceive a seamless world even if the underlying data structure is a closed loop.
Step 5: Apply to Super Mario 64
Now we apply these concepts to the actual game engine.
- The Parallel: In the Super Mario 64 community, covering spaces are referred to as “Parallel Universes” (PUs).
- The Reality: Walking in a straight line long enough leads you back to the same coordinate point, similar to Bob’s doughnut world.
- The Stack: We can imagine stacking copies of the game map infinitely. The universal cover of Mario’s world is effectively D² × ℝ².
Speedrunners use this to move Mario to a different “copy” of the map. The game logic treats him as being in a valid location, even if the graphics engine renders him in empty space.
Step 6: Analyze the Collision Logic
Finally, we look at the specific code implementation that creates this topology.
- The Cause: The game uses float (floating point) numbers for positions but casts them to short int (short integers) for collision detection.
- The Result: Only values between -32,768 and 32,767 are detected as unique positions. Any value outside this range “wraps around” due to integer overflow logic.
- The Math: Mario’s position detection occurs in B:= (ℝ/65536ℤ)³, creating a 3-dimensional torus structure.
This reveals that the “Parallel Universe” isn’t a glitch in the rendering, but a fundamental property of the collision detection’s topology. An AI agent reading memory values can exploit this discrepancy to teleport or bypass obstacles.
Next Steps
Understanding the topology of a simulation allows for more robust AI testing. When building environments for reinforcement learning:
- Check how your coordinate systems handle overflow.
- Determine if your state space is simply connected or if it contains loops (tori) that might confuse a pathfinding agent.
- Watch the Super Mario 64 Tool-assisted speedrun world record explained video by Bismuth for a visual demonstration of these mechanics in action.