> ## Content Index
> Fetch the complete content index at: https://evil-penguin.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Raycasting
- URL: https://evil-penguin.com/raycasting/
- Published: 2022-05-08T10:23:06.000Z
- Updated: 2026-11-16T23:12:21.000Z
- Description: Raycasting is a basic rendering algorithm
- Author: evilpenguin
- Tags: C, MLX

## Intro

Raycasting was first introduced in the 90's.  
The Raycasting rechnique can render games very fast, or collition detecion. Wolfenstein 3D was a fomous game which used Raycasting.

![](https://evil-penguin.com/content/images/2026/11/Wolfenstein.gif)

The rays rendered are exactly the amount columns of the window displayed (pixels).  
For example, for a window 100 pixels wide and 200 pixels tall, the algorithm just needs to calculate the nearest coalission for every column, (i.e) calculate 100 coalissions...  
Every coalission will be draw a column on the screen (200 pixels tall taking the example as ref.), as the distance to the coalission grows the column drawn is smaller, if the coalission is smaller, the column rendered will be taller.Raycasting allows you to render a 3D game wich only handles 2D calculations.  
You can calculate all the trigonometric values before starging the game which will save a lot of game processing time.  
  
![](https://evil-penguin.com/content/images/2026/11/Screenshot_2022-12-28_at_23.44.03.png)

> **C**

#define PI 3.14159265  
#define WINDOW\_WIDTH 320  
  
int r\_angle(int angle)  
{  
 return ((angle \* WINDOW\_WIDTH) / 60);  
}  
  
double fcos(int angle)  
{  
 static double fcos\[360 \* WIN\_W / 60\];  
 int i;  
  
 if (fcos\[r\_angle(0)\] > 1.0)  
 return (fcos\[angle\]);  
 i = 0;  
 while (i < r\_angle(360))  
 {  
 fcos\[i\] = cos(i \* PI / r\_angle(180)) + 0.00001;  
 i++;  
 }  
 return (fcos\[angle\]);  
}

> **JS**

const WINDOW\_WIDTH = 320  
  
function r\_angle(angle) {  
 return ((angle \* WINDOW\_WIDTH) / 60);  
}  
  
function arcToRad(angle)  
{  
 return ((angle\*Math.PI)/r\_angle(180));   
}  
  
Raycasting will set the color below the column drawn and the color above.

![](https://evil-penguin.com/content/images/2026/11/raycasting_rendering.png)