Interactive Studio · Pure 2D Canvas Math · Zero 3D Libraries

The 3D Engine Explorer

Interact with the core algorithms that trick human eyes into seeing 3D space on a flat glass screen. Switch between perspective wireframes, Wolfenstein 2D raycasting, SNES Mode 7 ground planes, and mathematical raytracing.

Drag canvas to orbit camera (Yaw & Pitch) Vertex V0 highlighted in red

Camera & Math Controls

260
4.5
35°
20°

Live Vertex V0 Math

World Point (X,Y,Z): (-1, -1, -1)
Camera Depth (ΔZ): ΔZ = 4.50
Rotated Vector: (0.00, 0.00, 0.00)
Screen Coordinate: (0, 0)

Computer Science · Graphics & Game Development · Math Algorithms

3D from Scratch: Projection, Raycasting & Render Engines

Perspective Division Rotation Matrices DDA Raycasting Mode 7 Scanlines Z-Buffer Rasterization Software Raytracing

Every computer monitor, phone screen, and VR display in the world is completely flat: a two-dimensional grid of glowing pixels. There is no physical depth behind the glass. In this lesson, we break down the mathematical formulas and graphics algorithms that trick our brains into experiencing three-dimensional space—from retro wireframes and Wolfenstein-style raycasters to SNES Mode 7 and modern raytracers.

1 · The Grand Illusion: How 2D Screens Fake Depth

When you sit in front of a monitor playing a 3D game, your eyes are not taking in light from a three-dimensional world. Your eyes are focusing on a flat sheet of glass less than two feet from your face. The 3D world you experience is entirely an optical illusion constructed by your brain from visual depth cues:

  • Linear Perspective: Parallel lines converge toward a single vanishing point as they recede into the distance.
  • Relative Size: Identical objects cover fewer pixels on screen the farther away they are.
  • Occlusion: Opaque objects in front hide objects behind them.
  • Motion Parallax: Objects closer to you move across your field of view faster than distant mountains or clouds.

The Spectrum of 3D in Game History

Over the last 40 years, game developers and graphics programmers invented several clever ways to produce these depth cues. We can categorize them into three major families:

Technique Family How It Works Underlying Math Classic Examples
Static Illusions Pre-rendered 2D art arranged with fixed perspective angles or depth scaling. None (drawn by artists). Sprites are stacked like Lego blocks or scaled over a painted room backdrop. Diablo (isometric tiles), Five Nights at Freddy's (1-point room images with interactive sprites).
Pseudo-3D (2D Geometry) The game world is strictly 2D in memory, but rendered using perspective projection tricks on screen slices. Grid DDA raycasting (vertical wall stripes) or affine horizon scanlines (horizontal ground strips). Wolfenstein 3D, Doom, F-Zero (Mode 7), OutRun (scaling road sprites).
True 3D Pipelines Objects exist as full 3D coordinates $(X, Y, Z)$ and are mathematically transformed onto the 2D display. Matrix transformations, perspective division, polygon scanline rasterization, or ray-surface optical simulation. Battlezone (wireframe), Star Fox, Quake (software rasterizer), modern Unreal/Godot engines.

2 · First Principles: The Mathematics of Perspective Projection

Suppose you have a point in 3D space: $(X, Y, Z)$. The monitor only has two axes: horizontal pixels $(x')$ and vertical pixels $(y')$. How do you turn three numbers into two?

The answer is Thales' Intercept Theorem (Similar Triangles). Imagine an eye or camera sitting at the origin $(0, 0, 0)$ looking straight down the $+Z$ axis. A transparent projection screen sits in front of the camera at a distance $d$ (which represents your Field of View or focal length).

Diagram of 3D perspective projection using similar triangles: a camera at the origin looks toward a 3D point P(X,Y,Z). The light ray pierces the 2D projection screen at distance d (FOV), yielding screen coordinate x' = X * (d / Z).
Figure 1 · The geometry of perspective division. The right triangle formed by the eye, the screen, and the screen point $(x', y')$ is similar to the larger triangle formed by the eye, the world depth $Z$, and the world point $(X, Y)$. Dividing by $Z$ is the universal rule of 3D graphics.
The Core Perspective Projection Formulas:
DrawX = (PointX - CameraX) × (FOV / (PointZ - CameraZ)) + CenterX
DrawY = (PointY - CameraY) × (FOV / (PointZ - CameraZ)) + CenterY

Notice what happens as depth $Z$ increases:

  • When $Z$ is small (close to the camera), the denominator $(PointZ - CameraZ)$ is small, so the scaling factor is large—the point is pushed far outward toward the screen edge.
  • When $Z$ is large (far away), the denominator is huge, so the scaling factor approaches zero—the point is pulled tight toward the screen's vanishing point $(CenterX, CenterY)$.

Camera Rotation: Rotating the World Around You

When you look around in a 3D game, your camera does not physically rotate your computer screen. Instead, the game rotates every object in the world around the camera in the opposite direction! To rotate a 3D point by angle $\theta$ around the vertical axis (Yaw):

X_rotated = X × cos(θ) - Z × sin(θ)
Z_rotated = X × sin(θ) + Z × cos(θ)

Once you transform the $(X, Y, Z)$ vertices of a 3D shape, you can connect them with 2D lines to create a wireframe (the technique used by early arcade machines like Battlezone and Asteroids), or draw 2D sprites at the projected coordinates scaled inversely with depth ($1/Z$).

3 · Parallel, Axonometric & 2.5D Projections

What if you don't divide by depth $Z$? That is parallel projection (also called orthographic or axonometric projection). In a parallel projection, rays of sight do not converge on a single camera point—they travel perfectly parallel to each other.

Parallel / Axonometric Projection Formula:
DrawX = (PointX - CameraX) + CenterX
DrawY = (PointY - CameraY) + ((PointZ - CameraZ) / DepthFactor) + CenterY

Because there is no division by $Z$:

  • Parallel lines never meet: Edges that are parallel in 3D space remain strictly parallel on your 2D screen.
  • Zero perspective foreshortening: A cube that is 100 meters away looks the exact same size as a cube 1 meter away.
  • The Depth Factor: Controls how much the $Z$ axis tilts the screen vertically. A factor of 2 creates a standard isometric or dimetric slant.
What does "2.5D" really mean?

The term "2.5D" is used in two different ways across gaming:

  1. 3D Math without Full Camera Freedom: A game whose engine calculates full 3D coordinates $(X, Y, Z)$, but locks the camera to a fixed parallel or side-scrolling perspective (e.g. Super Mario 3D Land or Diablo).
  2. 2D Graphics Faking 3D: A 2D sprite engine that uses a "height" variable (like $Z$) or skews the screen by 10–15 degrees to fake turning without true rotational matrices.

4 · Software Rasterization: Drawing Solid Triangles Without a GPU

Wireframes show the edges of an object, but what if you want solid, opaque surfaces? That requires rasterization—converting continuous geometric shapes into discrete screen pixels.

Diagram of software polygon rasterization and Z-buffering: 3D vertices are projected to 2D triangles, scanlines interpolate pixels horizontally between edges, and a depth buffer resolves occlusion.
Figure 2 · The software rasterization pipeline. Every solid 3D face is split into triangles. The software interpolates horizontal spans line by line (scanlines) and tests each pixel against the depth Z-buffer.

The Rasterization Pipeline in Four Steps:

  1. Triangulation: Every complex polygon (quads, pentagons, n-gons) is divided into triangles $(V_0, V_1, V_2)$. Triangles are always flat (planar) and mathematically easy to fill.
  2. Backface Culling: Calculate the triangle's normal vector. If it faces away from the camera, skip it immediately—there is no need to draw faces on the back of an opaque solid!
  3. Scanline Interpolation: Sort the triangle's three vertices from top to bottom ($y_0 \le y_1 \le y_2$). For each horizontal row $y$, find the left edge $x_{left}$ and right edge $x_{right}$, then draw a horizontal line of pixels between them.
  4. The Z-Buffer (Depth Buffer): What happens when two triangles overlap? Early games used the Painter's Algorithm (sorting whole polygons back-to-front), but it failed when triangles intersected or wrapped around each other. Modern software uses a Z-Buffer: a 2D memory array that remembers the closest depth $Z$ drawn at every single pixel $(x, y)$. If the new pixel is closer ($Z_{new} < Z_{buffer}[x, y]$), it writes the color and updates the depth; if it is farther behind, it is discarded.

5 · Pseudo-3D & 2D Raycasting: The Wolfenstein 3D Revolution

In 1992, John Carmack realized that running a full 3D polygon rasterizer on a 1990s 386 PC was too slow for fast action. He pioneered 2D Raycasting in Wolfenstein 3D. The brilliant insight was: the entire world is just a flat 2D overhead tile map, but the player sees a convincing first-person 3D dungeon!

Diagram comparing top-down 2D grid raycasting with first-person 3D column rendering, illustrating ray casting, grid DDA stepping, and the cosine fisheye correction.
Figure 3 · How 2D raycasting builds 3D vistas. For each column on the screen, a ray is cast across the 2D grid. When it strikes a wall, its distance determines the height of a single vertical wall slice on screen.

The Raycasting Loop:

If your screen is 320 pixels wide, you cast 320 rays across the player's field of view:

  1. Cast a ray from player $(px, py)$ at angle $\theta$.
  2. DDA Algorithm (Digital Differential Analyzer): Instead of marching forward in tiny steps (which is slow and can slip through wall corners), DDA steps from grid boundary to grid boundary along the $X$ and $Y$ tile lines. It reaches the wall in just a few mathematical steps.
  3. Measure distance $d$ to the solid wall tile.
  4. Fisheye Correction: Raw Euclidean distance creates curved, fishbowl distortion at the screen edges because rays at the edge travel farther than rays in the center. Multiply distance by $\cos(\text{rayAngle} - \text{playerAngle})$ to find the true perpendicular distance to the projection plane.
  5. Draw a vertical wall column: $\text{ColumnHeight} = \frac{\text{TileSize} \times \text{DistanceToPlane}}{d_{\perp}}$.

The 5 Flavors of Raycasting:

1 · Grid DDA

Classic 90-degree tile grid walls. Fastest possible calculation; used in Wolfenstein 3D.

2 · Raylisting

Stores multiple wall intersections in lists. Allows stepped heights, windows, and transparent fences (popularized in Scratch by Griffpatch's 3D maze).

3 · Iterative Vector Stepping

Advances a point forward by a small step vector. Easy to write in Scratch motion blocks, but slow and prone to corner clipping.

4 · Signed Distance Fields (SDF)

Uses mathematical distance equations to take giant safe steps toward surfaces without checking every tile (sphere tracing).

5 · Line Segment Intersection

Walls are arbitrary 2D line segments rather than grid squares. Enables non-grid layouts, angled corridors, diagonal slopes, and dynamic moving doors.

6 · Mode 7: Affine Horizon & Ground Plane Mapping

In 1990, Nintendo launched the Super Nintendo Entertainment System (SNES) featuring Mode 7. While standard raycasters draw vertical wall slices from left to right, Mode 7 draws horizontal ground scanlines from the horizon downward!

Diagram showing Mode 7 affine floor rendering: horizontal scanlines drawn from the horizon to the bottom of the screen calculate ground distance Z and sample (U,V) texture coordinates.
Figure 4 · Mode 7 scanline perspective. Each horizontal line on the screen represents a constant physical distance $Z$ in front of the camera. The hardware scales and rotates a single 2D background tilemap to draw the track.

The Math of Mode 7 Scanlines:

For every horizontal row $y$ beneath the horizon line:

GroundDistance Z = (CameraHeight × ProjectionDistance) / (ScanlineY - HorizonY)

As you approach the horizon ($ScanlineY \to HorizonY$), the denominator approaches zero, so $Z$ approaches infinity. At the very bottom of the screen, $Z$ is tiny, so ground features look huge. By sweeping across each scanline and rotating by the player's steering angle, games like Super Mario Kart, F-Zero, and Pilotwings achieved rotating 3D race tracks on 16-bit hardware.

7 · Software Raytracing: Simulating the Physical Behavior of Light

Rasterization and Raycasting are shortcuts: they transform geometry forward onto screen pixels. Raytracing does the exact opposite: it simulates how light physically travels through the universe, but in reverse!

Diagram of software raytracing optics: camera rays pass through screen pixels to hit 3D spheres, casting shadow rays toward lights and specular reflection rays into the scene.
Figure 5 · Reverse optical ray paths. The eye shoots a primary ray through each pixel. If it hits an object, it casts a shadow ray toward the light source and a reflection ray into the scene.

Why We Trace Light in Reverse:

In the real world, the sun emits trillions of photons every second. Less than 0.0001% of those photons ever strike your eye pupil. Simulating real light forward from the source would waste 99.999% of computer time calculating light that nobody ever sees. Raytracing shoots rays backward from the eye through each screen pixel—guaranteeing that every ray computed directly contributes to the picture.

Optical Effects That Come "For Free" in Raytracing:

  • Shadow Rays: From the point of intersection, shoot a secondary ray directly toward each light source. If any object blocks the path, the pixel is in shadow. No shadow maps or faked shaders required!
  • Specular Reflections: Using the surface normal vector $\vec{N}$, calculate the reflection bounce vector: $\vec{R} = \vec{D} - 2(\vec{D} \cdot \vec{N})\vec{N}$. Cast a bounce ray to see what the mirror reflects, and blend that color into the pixel.
  • Ambient Occlusion: Points tucked inside corners or crevices receive fewer ambient light bounces, naturally creating smooth, realistic contact shadows (like Minecraft smooth lighting).

8 · Building 3D Engines in Block Environments (Scratch & TurboWarp)

Can you build these engines in block-coding environments like Scratch? Absolutely. Thousands of students and passionate creators have built wireframe viewers, 3D mazes, and software rasterizers directly in Scratch using its Pen extension.

Scratch Wireframe Loop

erase all repeat (length of [edges v]) set [x1 v] to ((x * FOV) / z) pen up go to x: (x1) y: (y1) pen down go to x: (x2) y: (y2)

Why TurboWarp is Essential

Scratch runs as an interpreted loop, capped at 30 frames per second. A raycaster casting 80 rays per frame performs thousands of trigonometric calculations. TurboWarp compiles Scratch blocks directly into optimized JavaScript machine code, unlocking 60+ FPS and custom screen resolutions without changing the underlying block logic.

The Secret of 90s Hybrid Engines:

Early 3D masterpieces (like Doom, Duke Nukem 3D, and original PlayStation titles) rarely relied on just one technique. They combined them:

  • Line-segment raycasting for dynamic rooms and angled walls.
  • Mode 7 style scanlines for textured floors and ceilings.
  • Scaled billboard 2D sprites or low-poly software rasterized models for enemies and items.

By blending these approaches, developers squeezed rich, fluid pseudo-3D worlds out of hardware with less computing power than a modern digital watch.

9 · Check Your Understanding

Test your grasp of perspective division, raycasting geometry, and rendering pipelines.

Loading question...