Skip to content

A minimalistic 3D environment builder using grid tiles, rendered using simple DDA raycasting.

Notifications You must be signed in to change notification settings

mattlep11/raycaster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mattlep11/raycaster

A customizable and playable 3D environment rendered using raycasting, implemented using C++ and the Raylib drawing library. Utilizes the same computer graphics technique used to ship retro classics like DOOM and Wolfenstein 3D.

Build your grid...

Use the mouse on the grid builder to place tiles and build an environment to interact with.

Gif of a mouse clicking onto a grid and painting the picture of a smiley face.

... watch the rays cast...

Use WASD to move around your new environment and watch as your view-cone adapts in real time.

Gif of a red-cone interacting with walls as a player character moves around a grid.

... and explore your grid in first person!

Swap to the first person perspective to move around what you've built.

Gif of a first person perspective navigating through player built walls.

What is raycasting?

Raycasting is an old method of rendering 3D environments by "casting" or projecting rays from the camera's position outwards until it collides with some object. As these rays are cast in 2D, the distance from the player's viewpoint to the collided object must be translated into a single pixel wide line with a height that accurately represent's how far it is. By casting one ray per pixel of screen width, we can effectively re-create 3D scenes without the help of the z-axis.

The algorithm used in this project is known as DDA - the digital differential analysis algorithm. The layman's approach to finding the collision point between a ray and an object would be to gradually increase the ray's length by a small number and increment it until it hits. As you would expect, however, this is incredibly inefficient and wouldn't to support a full screen's worth of rays every frame. Alternatively, DDA makes the assertion that within a square grid where all cells are the same width, we can instead increment by a calculated unit in either the x or y direction. In this case, the calculated unit is the deltaX or deltaY change needed to step one cell in either direction. This reduces the potentially hundreds of increments needed per ray into a reasonable amount such that at least 25,000 rays can be used without dipping below 75 FPS. Though of course, you only need as many as pixels in the screen size's width.

How to run the project

This project was developed using C++ 17 and depends on the Raylib drawing library to run.
!! Note for Windows 10+ users, a preprepared .exe can be downloaded directly from the releases tab.

To set-up Raylib, consult the official guide in their repo's README for your os of choice.

Afterwards, use the following to clone the repo to open in the IDE of your choice:

git clone https://github.com/mattlep11/raycaster.git
cd raycaster

Resources used:


Spinning green circle with a red view-cone.

About

A minimalistic 3D environment builder using grid tiles, rendered using simple DDA raycasting.

Topics

Resources

Stars

Watchers

Forks