2. 3D Projection
How to flatten a 3D world onto a 2D screen using nothing but division
You're staring at a flat screen, but the game world looks three-dimensional. Distant mountains appear smaller than nearby trees. Parallel railroad tracks seem to converge. All of this happens because of one operation: dividing by Z.
This article builds 3D projection from scratch — no libraries, no GPU, just arithmetic you already know.
The Core Idea: Perspective Division
Imagine you're standing behind a window, looking at a scene. Objects farther away appear smaller. If you traced everything you see onto the glass, a 10-meter-tall building 100 meters away would be drawn the same height as a 1-meter-tall post 10 meters away.
The rule is simple: apparent size is inversely proportional to distance.
The Perspective Divide
To project a 3D point (x, y, z) onto a 2D screen, divide the x and y
coordinates by z:
screenX = x / z
screenY = y / z
That's it. This single operation is the heart of every 3D renderer ever written. Everything else — field of view, aspect ratio, near/far planes — is refinement.
Why Division by Z Works
Think of it geometrically. You have your eye at the origin and a projection plane at some distance d in front of you. A point at (x, y, z) projects onto the plane by similar triangles:
Similar triangles
Eye --------+----------- Point (x, y, z)
|
Projection plane
at distance d
screenX = d * x / z
screenY = d * y / z
The factor d just controls zoom. When d = 1, you get
the simplest form: screenX = x / z.
Field of View
The distance d in the formula above controls how much of the world you can see — your field of view (FOV). A small d means a wide angle (you see more but with more distortion). A large d means a narrow, telephoto view.
The relationship is:
d = 1 / tan(fov / 2)
A 90-degree FOV gives d = 1. A 60-degree FOV gives d ≈ 1.73, which is a common default for games.
Centering on Screen
Real screens have pixel coordinates starting at (0, 0) in the top-left corner, not the center. After the perspective divide, we need to shift and scale to screen coordinates:
screenX = (d * x / z) * (width / 2) + (width / 2)
screenY = -(d * y / z) * (height / 2) + (height / 2)
The negative sign on Y flips the axis — in math, Y points up; on screen, Y points down.
Rotation with Sine and Cosine
So far we can project a static scene. But we want to look around — rotate the world (or the camera). The simplest tool for this is trigonometry.
Rotation in 2D
To rotate a point (x, y) by angle θ around the origin:
2D rotation formula
x' = x * cos(θ) - y * sin(θ)
y' = x * sin(θ) + y * cos(θ)
This comes directly from the angle-addition formulas. The point traces a circle around the origin at its original distance.
Extending to 3D
In 3D, a rotation always happens around an axis. We rotate the two coordinates perpendicular to that axis and leave the third alone:
Rotation around the Y axis (yaw — looking left/right):
x' = x * cos(θ) - z * sin(θ)
z' = x * sin(θ) + z * cos(θ)
y' = y (unchanged)
Rotation around the X axis (pitch — looking up/down):
y' = y * cos(θ) - z * sin(θ)
z' = y * sin(θ) + z * cos(θ)
x' = x (unchanged)
Rotation around the Z axis (roll — tilting your head):
x' = x * cos(θ) - y * sin(θ)
y' = x * sin(θ) + y * cos(θ)
z' = z (unchanged)
Composing Rotations
To apply multiple rotations, just apply them one after another. Rotate around Y first, then X, then project. The order matters — rotating pitch-then-yaw gives a different result than yaw-then-pitch. This "order dependence" is a fundamental property of 3D rotations that will become clearer when we move to matrices.
The Projection Pipeline
Putting it all together, here's the full pipeline for rendering a 3D point to the screen:
-
Start with world coordinates
Each vertex in your scene has a position
(x, y, z)in world space. -
Translate relative to the camera
Subtract the camera's position:
x -= camX,y -= camY,z -= camZ. -
Rotate to camera orientation
Apply sin/cos rotations for yaw and pitch to align the scene with the camera's view direction.
-
Perspective divide
Compute
screenX = d * x / zandscreenY = d * y / z. Skip points wherez ≤ 0(behind the camera). -
Map to screen pixels
Scale and offset to place the center of projection in the center of the canvas.
The Horizon and Vanishing Points
A consequence of perspective division: all parallel lines that recede into the distance converge at a single vanishing point. Horizontal lines converge at the horizon — which sits at the camera's eye level.
This is why:
- Railroad tracks appear to meet in the distance
- The tops and bottoms of buildings converge upward
- A grid floor has lines that fan out from a central point
The horizon line is at screenY = height/2 when the camera has zero pitch. Tilting the camera up moves the horizon down; tilting down moves it up.
Depth Sorting: The Painter's Algorithm
When you project 3D onto 2D, some objects are in front of others. The simplest solution is the painter's algorithm: sort all faces by their distance from the camera, then draw from farthest to nearest. Later draws paint over earlier ones, naturally hiding what's behind.
It's not perfect — it fails for intersecting or cyclically overlapping polygons — but it works for simple scenes and requires zero extra data structures.
What's Missing?
This sin/cos approach works, and many early 3D engines (including the original Doom engine's horizontal rotation) used exactly this. But it has limitations:
- Verbose: Three separate rotation functions, each with its own copy-paste formula
- Hard to combine: Composing many rotations means nesting function calls in the right order
- No easy inverse: Undoing a rotation means rotating by
-θ, but tracking accumulated angles gets messy
The next article introduces rotation matrices, which solve all of these problems by packaging rotation into a single, composable data structure.
Key Takeaways
What to remember
- Perspective divide:
screenX = x/z,screenY = y/z. That's the core of all 3D projection. - Field of view controls the zoom factor
d = 1/tan(fov/2). - Sin/cos rotation rotates two coordinates while leaving the third axis unchanged.
- Order matters: yaw-then-pitch ≠ pitch-then-yaw.
- The horizon is where all horizontal parallel lines converge — it's at eye level.