Magnetic Corridor: Timing Polarity Reversals in a One-Button MATLAB Game
Magnetic Corridor is a game in which you guide a magnet that moves forward automatically through a winding corridor by reversing its top and bottom poles. A maglev railway line being developed in Japan will pass near the area where I live, which inspired the electromagnet theme.
There is only one button, but pressing it does not instantly reverse the vertical velocity. It changes the magnetic force, which then changes velocity and position. This delayed response makes looking ahead part of the controls. The game does not reproduce real maglev levitation or propulsion, or an exact electromagnetic field.

How to Play
- Press Space or click to start. During a run, each press swaps N/S on the top and bottom of the player together.
- Red walls are N, cyan walls are S, and white walls have no magnetic pole. White walls exert no magnetic force, but touching them still causes a collision.
- A collision displays the distance traveled, Lv, and changes in speed and width. Press to return to the start screen with a new course, then press again to start.
At startup, an approximately 1.5-second title display leads to the start screen. It does not repeat on retries. Close the window with the × button to quit. Input is only whether a press occurred, not how long it was held or where the click occurred.
1. Magnetic Force, Inertia, and Timing
The pole on the top of the player is represented by p: +1 for N and -1 for S. The bottom always has the opposite pole. A press reverses only this sign.
if tap
s.p = -s.p;
end
Position y and vertical velocity vy are not overwritten, so motion remains continuous immediately after the reversal. You need to consider the current direction of motion when choosing when to press for the next bend.
The magnetic force is calculated from the interaction between each wall and the nearest face of the player. bands.Tp and bands.Bt represent the wall poles: +1 for N, -1 for S, and 0 for no pole.
weightTop = 0.25 + 1.5*exp(-max(distanceTop, 0)/95);
weightBottom = 0.25 + 1.5*exp(-max(distanceBottom, 0)/95);
Tp = -260*s.p*bands.Tp*weightTop;
Bt = -260*s.p*bands.Bt*weightBottom;
mag = Tp + Bt;
This game model gives a nearer wall a larger weight. The bottom face has polarity -p, and the lower wall's interaction direction is opposite to the upper wall's, allowing both terms to use the same -p factor. mag is used as the vertical acceleration corresponding to magnetic force.
The motion update adds velocity-proportional drag. Each update is divided into substeps no larger than 1/240 of a second. Velocity is updated first, then position is advanced using that velocity.
h = min(remaining, 1/240);
mag = computeForce(s);
s.vy = s.vy + h*(mag-2.2*s.vy);
s.y = s.y + h*s.vy;
s.x = s.x + h*s.vx;
This is a semi-implicit Euler-style calculation. Drag opposes the current vertical velocity; reversing polarity does not also reverse drag. When neither wall has a pole, mag becomes 0, but velocity does not suddenly become 0. It gradually decays through drag. Actions taken before entering a section with no magnetic poles therefore affect the path through it.
The yellow arrow shows the magnetic component, with a cap on its displayed length. Comparing the arrow with the player's motion lets you observe that the direction of force is not necessarily the direction of travel.
2. Smooth Width Transitions
The Lv increases every 10 seconds of running time. Forward speed starts at 120, increases by 18 per level, and is capped at 300. Each level reduces the corridor width by 2% of its initial width, down to a minimum of 80%. Speed changes at the level boundary, while width changes smoothly over the following 1 second.
s.lv = 1 + floor((s.t+1e-10)/10);
s.vx = min(300, 120+18*(s.lv-1));
u = max(0, min(1, s.t-10*(s.lv-1)));
s.c.w = 1-.02*min(10, max(0, s.lv-2+u*u*(3-2*u)));
u*u*(3-2*u) is smoothstep, with a rate of change of 0 at both endpoints. At Lv2, width changes from 100% at 10 seconds to 98% at 11 seconds. At Lv11, it changes from 82% at 100 seconds to 80% at 101 seconds. The reduction is relative to the initial width, not repeated multiplication by a fixed factor.
The width scale factor c.w multiplies both the corridor half-width and its spatial derivative. Rendering, magnetic force, and collision detection share the same corridor calculation, so the visible and collision walls do not need separate narrowing logic. Collisions remain active during the transition; there is no invincibility period.

Percentages in the HUD and results are relative to the initial settings. SPEED +150% means 2.5 times the initial speed, and WIDTH -20% means 80% of the initial width.
3. Reusing Graphics and Transforming Coordinates
Graphics objects are created during initialization. Rendering updates the data of existing lines, semicircles, text, and arrows. Wall points are supplied as arrays to XData and YData. Points that do not match a given magnetic polarity are set to NaN to split the lines. This groups the walls into a few lines instead of creating an object for every point.
Motion is calculated in world coordinates, with the camera position subtracted for rendering. The vertical expression playerY = s.y - cameraY; follows the same idea. This is a 2D view transformation: the camera follows the corridor without any force pulling the player back to its center. The dark shell, support ribs, and slowly scrolling distant segments are decorative, not magnetic-field visualizations or collision objects.
The corridor combines 3 sine waves, with a random seed chosen initially and on retries. The same seed generates the same course, which also supports comparison tests. Collision detection approximates distance to the wall's local tangent and runs before integration and after each substep. It is not an exact closest-point calculation on the curve or continuous collision detection.
4. Development Takeaways
The game is a single game.m file and contains 9,602 characters using the official mtree → tree2str method. Removing detailed diagnostics, trajectory history, and numerous labels freed up room for the background, displays, and width interpolation.
The 10-second version passed the official validator in MATLAB R2026b with the official Toolkit 1.0.1. Generative AI assisted with design exploration, implementation, validation, and writing. The images in this article were captured with the earlier 18-second version.
To experiment, change just one of the drag coefficient, magnetic force strength, or wave amplitudes or wavelengths, and compare runs with the same seed. This makes the connection to the controls easier to follow. The game offers a way to explore how reversing polarity connects force, velocity, and position through play.
The code consists of excerpts from game.m; the snippets are not intended to run independently.