DEV Community

Cover image for 2D Raytracing
No_Arms_studio
No_Arms_studio

Posted on

2D Raytracing

< First thing I would like to say is that thank you all, I have hade a massive influx of followers and I thank you all for it. But anyway time to get to my approach of raytracing in scratch. First I would like to talk about what is a raytracer. A raytracer is a computer code that send out rays into an environment and let them bounce to see how light would act in that environment. This fundamental code makes all sorts of things such as cg in big budget Hollywood movies to just little side projects for indie devs.
The way that I made my version of a raytracing engine is first to find the math for the rays. There are 2 inputs, the amount of rays and the FOV. The raycaster sprite turns half of the FOV and repeats the amount of rays turning the opposite direction the FOV divided by amount of rays. each of those iterations make a new ray.
My engine is a 2d raytracer meaning that it has a 2d texture that it bounces it's light in. The way that each ray works is that when it is initialized it moves forward 1 pixel, if it is touching a wall then it will turn around and point in a random 180 degree radius and it gets darker. If it touches the light then it will end it's loop and draw the walls height depending on the distance to the camera, if it fails to touch the light within a certain length then it turns up completely dark.
This system in the end makes a pretty realistic looking simulation of light for scratches standards, in no means is it as good as Hollywood or even a normal indie side project but it works. Thank you for reading this far and have a great day :)

Top comments (1)

Collapse
 
no_arms_studio profile image
No_Arms_studio

Hello! :)