Ray Casting in FPS Games (e.g., Wolfenstein 3D)
In early first-person shooter games like Wolfenstein 3D, ray casting is a technique used to render a pseudo-3D world from a 2D map. It works by projecting rays from the player’s viewpoint into the 2D map to detect walls, then drawing vertical slices on the screen based on how far each ray travels before hitting a wall. The closer the wall, the taller the line drawn — this simulates depth.
It's a computationally efficient technique that allows for a fast rendering of a 3D-looking space using 2D math, which is why it was ideal in an era of more limited hardware.
✅ Your Ray Casting Sketch in Processing 4
Below is a fully working, simplified ray casting program in Processing 4, showcasing a static square room and a player who can move using WASD and rotate using the arrow keys.
Copy and Paste Everything Below into a New Sketch
final int screenWidth = 800;
final int screenHeight = 400;
final int mapWidth = 8;
final int mapHeight = 8;
final int blockSize = 64;
int[][] map = {
{1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1},
};
float posX = 100;
float posY = 100;
float dir = 0; // Camera angle in radians
float fov = PI / 3.0; // 60 degrees field of view
float moveSpeed = 2.0;
float rotSpeed = 0.05;
void setup() {
size(800, 400);
noCursor();
}
void draw() {
background(70);
castRays();
movement();
}
void castRays() {
int numRays = screenWidth;
float rayAngle = dir - fov / 2;
for (int ray = 0; ray < numRays; ray++) {
float angle = rayAngle + (float)ray / numRays * fov;
float distanceToWall = 0;
boolean hitWall = false;
float eyeX = cos(angle);
float eyeY = sin(angle);
while (!hitWall && distanceToWall < 1000) {
distanceToWall += 0.1;
int testX = int((posX + eyeX * distanceToWall) / blockSize);
int testY = int((posY + eyeY * distanceToWall) / blockSize);
if (testX < 0 || testX >= mapWidth || testY < 0 || testY >= mapHeight) {
hitWall = true;
distanceToWall = 1000;
} else if (map[testY][testX] == 1) {
hitWall = true;
}
}
// Simple fix to remove fish-eye distortion
distanceToWall *= cos(angle - dir);
float wallHeight = (blockSize * screenHeight) / max(distanceToWall, 0.0001);
float startY = screenHeight / 2 - wallHeight / 2;
float endY = screenHeight / 2 + wallHeight / 2;
stroke(255 - distanceToWall * 0.4);
line(ray, startY, ray, endY);
}
}
void movement() {
// Movement with WASD keys
if (keyPressed) {
float dx = cos(dir);
float dy = sin(dir);
float strafeDX = cos(dir + HALF_PI);
float strafeDY = sin(dir + HALF_PI);
if (key == 'w') {
if (noCollision(posX + dx * moveSpeed, posY + dy * moveSpeed)) {
posX += dx * moveSpeed;
posY += dy * moveSpeed;
}
}
if (key == 's') {
if (noCollision(posX - dx * moveSpeed, posY - dy * moveSpeed)) {
posX -= dx * moveSpeed;
posY -= dy * moveSpeed;
}
}
if (key == 'a') {
if (noCollision(posX - strafeDX * moveSpeed, posY - strafeDY * moveSpeed)) {
posX -= strafeDX * moveSpeed;
posY -= strafeDY * moveSpeed;
}
}
if (key == 'd') {
if (noCollision(posX + strafeDX * moveSpeed, posY + strafeDY * moveSpeed)) {
posX += strafeDX * moveSpeed;
posY += strafeDY * moveSpeed;
}
}
// Rotation with arrow keys
if (keyCode == LEFT) {
dir -= rotSpeed;
} else if (keyCode == RIGHT) {
dir += rotSpeed;
}
}
}
boolean noCollision(float x, float y) {
int gridX = int(x / blockSize);
int gridY = int(y / blockSize);
if (gridX < 0 || gridX >= mapWidth || gridY < 0 || gridY >= mapHeight) return false;
return map[gridY][gridX] == 0;
}
▶️ Controls
- W: Move forward
- S: Move backward
- A: Strafe left
- D: Strafe right
- Left Arrow: Turn left
- Right Arrow: Turn right
🧠 TL;DR: What You're Seeing
Each column of pixels on the screen is a "ray" cast into the map. Based on where the ray hits a wall, the code draws a vertical line representing that wall with proper height and shading (to simulate depth and distance).
This is a core concept that powered the first-generation 3D shooters like Wolfenstein 3D and still serves as a foundation for understanding game graphics.
Let me know if you want to add textures, map editor, or minimap overlay!