Skip to content
A 3D Effect via 2D Raycasting
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Transforming 2D Maps into 3D Environments with Ray Casting

In the early days of computer hardware, game developers had to use clever techniques of creating perspective without much memory or computing power. Inspired by the MS-DOS game Wolfenstein 3D, this single Java file uses one such technique: ray casting. Ray casting was first presented for computer graphics in 1982 by Scott Roth.

My implementation uses three properties to simulate depth: the relative height of the walls, the darkness of the color of the walls, and the color gradients on the floor and ceiling.

The algorithm begins with a 2D map of the area. This is as simple as a matrix of integers denoting empty space with 0 and different colored walls with 1, 2, and 3.

We then split the field of view into columns of equal width. Here we use a field of view of 90 degrees, and 200 columns of width 5 pixels. Then, at each frame, for each of these columns, we calculate the angle between looking straight ahead and looking directly at that column. This is done on the 2D map of the environment. We can then follow that direction until the first non-zero cell is hit, and record that distance. With the distance for each column recorded, we can set the vertical height of the wall in that column to be larger if the distance was small, and smaller if the distance was large.

alt text

In this example, we can see the blue vertical bars representing 2 of the 200 columns that make up the image. The corner of the green wall is pretty close in the 2D map, so it gets a tall green box. The purple wall in the distance is quite far in the 2D map, so its cyan box is much shorter.

Alt Text


Download and double click on the jar file, or run from a command line with:

> java -jar .\Raycast3D.jar
You can’t perform that action at this time.