Discuss Scratch

Dogg_Thing
Scratcher
37 posts

RayCasting

I know this is going to be hard to answer, but can someone help me with RayCasting? I don't really get how to do it with all the custom blocks and pen scripts
awesome-llama
Scratcher
1000+ posts

RayCasting

First, you need to understand how ray casting works. I will leave these two links to Wikipedia and Scratch's wiki about it. I really recommend you read them first so you have an idea on how it works.

https://en.wikipedia.org/wiki/Ray_casting
https://en.scratch-wiki.info/wiki/Raycaster

You want to be casting rays out from a camera (the player), and finding the distance they travel to reach a wall.
See this image below from the Scratch Wiki:


————

To implement ray casting in scratch, you have to set up a map to cast onto. The easiest way is to make sprites to do all the work (as opposed to doing it all with lists). You need a sprite of the map viewed from the top, as well as a sprite which acts as the camera.
Every frame, you need the camera to test the distance to the walls at many places. Once you do that, you can draw the scene in with pen, making vertical lines for each scanned disance. The pen line length and colour is set according to the distance.

Powered by DjangoBB