HOME / BLOG / Raycasting
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.
The Raycasting rechnique can render games very fast, or collition detecion. Wolfenstein 3D was a fomous game which used Raycasting.
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.
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.
You can calculate all the trigonometric values before starging the game which will save a lot of game processing time.
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.