Starfield Warp, Explained
After reading this you will understand the single perspective divide that turns a cloud of random 3D points into a convincing flight through space, and you will be able to reproduce every number the simulation draws by hand.
What it is and one worked frame
Starfield Warp scatters points in the space ahead of a camera, moves them toward you frame by frame, and projects each one onto a flat screen. The screen is 2D, but because near stars sweep outward faster than far stars, your eye reads the motion as depth. Push the throttle and the projected points stretch into radial streaks, the classic jump to light speed.
Here is the hook. Put one star at 3D position (x, y, z) = (0.4, 0.2, 5) in camera units, with a screen scale of 256 pixels. Its screen position is (0.4/5, 0.2/5) \times 256 = (20.5, 10.2) pixels from centre. Move it one step closer to z = 4 and it jumps to (0.4/4, 0.2/4) \times 256 = (25.6, 12.8). Same star, one step, and it moved about 5.5 pixels. Keep halving z and the jumps explode. That runaway is the whole illusion.
When this model helps and when it does not
Use the star tunnel when you want the smallest possible demonstration of 3D perspective. Every 3D engine, from a phone game to a flight simulator, rests on the same divide by depth. Here that divide is the entire program, with nothing else in the way, so you can watch exactly what perspective does.
Do not read it as astronomy. Real stars do not stream radially past a ship, and nothing in this toy respects the speed of light, relativistic aberration, or Doppler shift. The "warp" look is motion blur drawn on purpose, not physics. If you want a system where the numbers model something real, the Lorenz Attractor and Three-Body Choreographies integrate genuine differential equations. The starfield is a projection demo wearing a science-fiction costume.
The perspective divide is one of the few places in graphics where a nonlinear operation (dividing by z) is unavoidable. Rotations and translations are linear, but turning depth into apparent size is not, which is why z = 0 is a singular point you must always guard against.
The formula and why it works
A star lives at 3D coordinates (x, y, z) where z is distance straight ahead. The screen sits one focal length in front of the camera. Projecting the star onto that screen gives pixel coordinates measured from the centre:
Here u and v are horizontal and vertical screen offsets in pixels, f is the focal length in pixels (the screen scale), and x, y, z are the star's position in world units. The division by z is the perspective divide. A star twice as far away appears half as far from centre, which is exactly how your eye judges distance.
Flying forward means decreasing z every frame by a speed s:
When z drops below a small threshold (the star has flown past you), recycle it to the far distance and give it fresh random x and y. That recycling is why the flight never ends: the same few hundred stars loop forever.
The apparent outward speed of a star follows from differentiating the projection. Along the horizontal axis, holding x fixed while z shrinks at rate s:
Read the 1/z^2 factor carefully. Screen speed grows with the square of nearness. A star at z = 1 moves 25 times faster across the screen than the identical star at z = 5. That single fact is why the edges of the tunnel blur while the centre stays calm.
Steering, streaks, and the vanishing point
Everything above assumes you fly dead ahead, so the vanishing point sits at screen centre. To turn, shift the point that stars radiate from. If you offset every projected position by (c_x, c_y), the origin of the outward flow moves, and your brain reads that as banking or turning while still moving forward.
Warp streaks are motion blur made explicit. Instead of drawing a star as a dot at its current projection, draw a line segment from its previous projected position to its new one. The length of that segment is the per-frame screen displacement, which the derivative above already gave you. Faster flight (larger s) or nearer stars (smaller z) means a longer line. The streak is literally the path the star swept in one frame.
Reproducing the default flight
Take the demo defaults: focal length f = 256 pixels, speed s = 0.05 world units per frame, and a star that starts at (x, y, z) = (0.4, 0.2, 5). Track its horizontal projection u for four frames.
- Frame 0: z = 5.00, so u = 256 \cdot 0.4 / 5.00 = 20.48 pixels.
- Frame 1: z = 4.95, so u = 256 \cdot 0.4 / 4.95 = 20.69 pixels. Streak length
0.21px. - Frame 2: z = 4.90, so u = 256 \cdot 0.4 / 4.90 = 20.90 pixels. Streak length
0.21px. - Frame 3: z = 4.85, so u = 256 \cdot 0.4 / 4.85 = 21.11 pixels. Streak length
0.21px.
At z near 5 the streak is tiny, about 0.21 pixels per frame. Now jump the same star to z = 0.5: the derivative predicts a per-frame move of 256 \cdot 0.4 \cdot 0.05 / 0.5^2 = 20.48 pixels. That is a streak nearly a hundred times longer, which is why stars appear to accelerate outward as they approach.
Reading the motion
The chart below plots one star's horizontal screen offset u as it flies from z = 5 down to z = 0.3 at the default settings. Nothing changes about the star except its depth. Watch how flat the curve is far away and how sharply it bends near the end.
Two readings matter. First, the vanishing point is where all stars converge as z \to \infty: their offset goes to zero regardless of x and y. Second, the density of visible motion is uneven. Most of a star's screen travel happens in its final few frames, so your attention is pulled to the edges even though the stars are evenly spaced in depth.
Common mistakes
The first mistake is letting z reach or pass zero. The projection divides by z, so z = 0 is division by zero and any negative z puts the star behind the camera, where the formula produces a mirrored ghost. Always recycle a star while z is still a small positive number, such as 0.1, never at exactly zero.
The second mistake is respawning recycled stars back at a fixed distance with no random spread. If every recycled star reappears at exactly z = 5, you see pulsing rings instead of a steady stream. Scatter the new depth across a range so arrivals are staggered.
Watch the frame rate. Speed s is measured per frame, not per second. If the browser runs at 30 frames per second on one device and 120 on another, the same s gives a flight four times faster on the fast machine. A robust version scales the step by elapsed time, moving z by s \cdot \Delta t instead of s alone.
The third mistake is drawing streaks from the wrong endpoint. The streak must run from the previous projected position to the current one. If you instead draw from the current position outward by a guessed length, the streaks point the wrong way when you steer and the illusion breaks.
Related tools
If the perspective divide interests you, other geometric toys here build depth and pattern from equally small rules. The Phyllotaxis model places points by the golden angle and grows a sunflower head. The Spirograph traces curves from rolling gears. For motion driven by random rules rather than fixed geometry, see the Random Walk Explorer and the drifting streams of Flow-Field Particles. If you would rather watch simple update rules build complexity, Conway's Game of Life and the Elementary Cellular Automata are the canonical examples.
Frequently asked questions
Why do the stars seem to speed up as they get close?
Screen speed grows as 1/z^2. A star at z = 1 crosses the screen 25 times faster than the same star at z = 5, so almost all of its visible motion happens in the last few frames before it passes you.
How does moving the pointer steer the ship?
Moving the pointer shifts the vanishing point, the screen location that all stars radiate from. Since that point marks the direction of travel, offsetting it is mathematically the same as pivoting your view while still flying forward.
Is this how real hyperspace or warp drive would look?
No. The streaks are motion blur drawn on purpose, and the model ignores the speed of light, Doppler shift, and aberration entirely. It reproduces the look from science fiction films, not any physical prediction.
Why does the flight never end?
There are only a few hundred stars. When one reaches the camera at small z, it is recycled to a far random depth with new x and y. The same points loop forever, so the tunnel appears endless.
What controls how long the streaks are?
Streak length equals the star's per-frame screen displacement, f \cdot x s / z^2 horizontally. Larger speed s and smaller depth z both lengthen it, which is why pushing the throttle stretches every star toward the edges.