From Test-Scratch-Wiki
(Redirected from Eng:Sprite-Based Raycaster Tutorial)
A raycaster is a project that renders a 3D world based on a 2D map. This is a working example. A raycaster in Scratch is usually single frame and low resolution, to prevent lag.
Raycasting should not be mistaken with raytracing, which renders rays with more physical accuracy, catering to reflection and refraction of light rays, and traces rays in two dimensions rather than one dimension like in a raycaster.
Concept
Raycasting works by casting "rays" to measure the distance to the nearest wall, hence the term "raycaster". The program send out rays starting from the player, moving forward until it hits a wall, at which point it takes the distance it has traveled and draws a column based on the distance. The closer the wall, the larger the column. The rays are sent in different directions, with the angle sent determining where the column will be drawn. A ray sent out to the right side of the player's viewpoint will draw a column at the right side of the screen and a ray sent to the left will draw a column at the left. After all rays have been sent, the complete picture will be seen.
Sprite Based vs List Based
There are two types of raycaster that can be programmed: the sprite-based method, and the list-based method. Each has their own advantages and disadvantages:
Sprite Based | List Based | |
---|---|---|
Degree of Difficulty | Easy | Hard |
Framerate | Low | High |
Knowledge Required | Basic Scratch Programming | Scratch Programming; Trigonometry; Array |
How Map is Stored | As a Sprite; each map/world is a Costume | As an Array (grid of numbers); each map is a separate array |
Sprites Needed | 1, but 3 is usually used (map, renderer, and ray) | At least 1 (renderer) |
Other Advantages | Can be used for making worlds with curved walls | Can easily generate random worlds |
For beginners, it is recommended to start by making the sprite-based method, as it is easy and uncomplicated, but slow. More advanced programmers and those who have finished making a sprite-based raycaster, can make an array based raycaster, which is faster but more complicated.
Sprite-Based Raycaster
In this raycaster, you will need three sprites:
- A pixel, the "sensor"
- This moves to the player's position, points in the direction the player is facing, then moves forwards until it touches the map
- It then records the distance moved in a list.
- It repeats this for many angles to the left and right of the direction.
- A 480x360 rectangle with certain inner paths cleared out, the "map"
- The rectangle left after clearing some paths is used to obstruct the sensor
- Another pixel, the "renderer"
- The renderer draws each wall based on the distance given by the ray
The tutorial below will teach you how to make a working sprite-based Raycaster. Here is a finished example
Creating the map
Make a map sprite with walls, make sure it has a closed outside area. Then, add the following script to it:
when gf clicked go to x: (0) y: (0) set [ghost v] effect to (100)
Movement
Make a movement sprite that has a one pixel by one pixel costume. Then put it inside the walls of your map sprite. Then add this script to it:
when gf clicked go to x: (-20) y: (0) //assuming that that is where you put your sprite set [drawing v] to [0] set [touching wall v] to [0] set [ghost v] effect to (100) forever if <not <(drawing) = [1]>> then if <key [right arrow v] pressed?> then turn cw (2) degrees //this can be adjusted to rotate faster, good for fighting lag broadcast [sense v] end if <key [left arrow v] pressed?> then turn cw (-2) degrees //this can be adjusted to rotate faster, good for fighting lag broadcast [sense v] end if <not <(touching wall) = [1]>> then if <key [up arrow v] pressed?> then move (speed) steps //where speed is the desired amount of steps you want it to move broadcast [sense v] end end if <not <(touching wall) = [-1]>> then if <key [down arrow v] pressed?> then move <(speed) * (-1)> steps //where speed is the desired amount of steps you want it to move broadcast [sense v] end end end
Also, clicking the information button on this sprite and clicking "don't rotate" speeds it up a little and reduces lag.
Wall sensing
You now have your map and something that moves around in that map, but the only thing that will stop it moving is variable values. Add this wall sensing script to the wall sensing sprite:
when gf clicked set [ghost v] effect to (100) forever if <not <(drawing) = [1]>> then go to [movement v] //where movement is the movement sprite point in direction <[direction v] of [movement v]> move (speed) steps if <touching [walls v]?> then//the map sprite set [touching wall v] to [1] else set [touching wall v] to [0] move <(speed) * (-2)> steps if <touching [walls v]?> then set [touching walls v] to [-1] else set [touching walls v] to [0] end end end
Now you can move around your map without going through walls.
Distance sensing
This is the sensing script if you only have one sensor. If you have multiple sprites for sensing, you should divide up the numbers and for each sprite make a new list.
when gf clicked set [ghost v] effect to (0) delete (all v) of [distances v] go to [movement v]
when I receive [sense v] //the broadcast from before set [drawing v] to [1] delete (all v) of [distances v] set [distance v] to [0] set [angle offset v] to [48] repeat (96) set [distance v] to [60] go to [movement v] point in direction <<[direction v] of [movement v]> + (angle offset)> repeat until <<touching [walls v]?> or <(distance) = [0]>> move (1) steps change [distance v] by (-1) end add (distance) to [distances v] change [angle offset v] by (-1) end wait until <(length of all lists) = [96]> //if you have other sensors broadcast [draw v]
Drawing
Divide up all the lists between all of the drawing sprites you have. Here is a script if you have only one drawer and one distance sensor
when I receive [draw v] go to x: (-237.5) y: (180) set pen size to (5) //because 96*5=480 which is the width of the screen set pen color to (Color you want) //Pick the color you want clear pen up set [help counting v] to (length of [distances v]) repeat (length of [distances v]) set pen shade to ((item (help counting) of [distances v]) * ((1) + ((2) / (3)))) set y to ((item (help counting) of [distances v]) * (4)) pen down set y to (((item (help counting) of [distances v]) * (4)) * (-1)) pen up change [help counting v] by (-1) set pen shade to (0) change x by (5) end set [drawing v] to [0]
This project will be slow when you run it in Scratch, even in turbo speed. Upload and run in the flash player in turbo mode, to run in at a decent speed. However, you can run this project at a decent speed without turbo mode by running this project without screen refresh by using the more blocks. Here is an example.
List-Based Raycaster
A list-based raycaster relies on a map stored as a list and coordinates, such as that of the player and the ray, stored as variables. This method of raycasting is very virtual, all data is stored as numbers and there are no actual sprite costumes used. The only sprite required is a moving pen to draw the walls. Here is an example. If you want an example map, one is downloadable here.
This tutorial will teach you how to make a simple list-based raycaster. It uses custom blocks to make editing easier. Make sure to check off "run without screen refresh" box.
Note: | Only try this method after you have completely understood the Sprite-Based Raycaster or are already familiar with arrays and raycasting. |
Setting the Variables
Here is the code used to setup the variables:
define Set up Variables set [X Position v] to [11] set [Y Position v] to [7] set [Direction X v] to [-1] set [Direction Y v] to [0] set [Plane X v] to [0] set [Plane Y v] to [0.66] set [Actual Resolution v] to [1] // If you aren't using resolution, you don't need this variable. set [Height v] to [300] set [Resolution v] to [12] // Resolution is not needed, but it is recommended.
The Main Loop
This is the green flag script that starts all the other scripts.
when green flag clicked Set Up Variables // You have already created this block! forever pen up // DadOfMrLog did some tests and found that setting pen size to 1 and using the pen up block reduces lag! set pen size to (1) hide set [Actual Resolution v] to (((Resolution) - (16)) * (-1)) //If you want to have resolution, you need this script. Raycast // This is the next script in the tutorial. end
The Raycasting Script
Next is the main raycasting script. This block controls most of the custom blocks. Be sure to make it a run without screen refresh block.
define Raycast clear set [x v] to [-240] // "x" is the increment variable here. Read Keys // This is the next script in the tutorial. repeat until <(x) > [280]> pen up set pen size to (1) set pen shade to (0) set [Camera X v] to ((2) * ((x) / ((Actual Resolution) - (1))) set [Ray X Position v] to (X Position) set [Ray Y Position v] to (Y Position) set [Ray X Direction v] to ((X Direction) + ((Plane X) * (Camera X))) set [Ray Y Direction v] to ((Direction Y) + ((Plane Y) * (Camera X))) set [Map X v] to ([floor v] of (Ray X Position)) set [Map Y v] to ([floor v] of (Ray Y Position)) Calculate Walls :: custom // Explained later on. Draw Walls :: custom // Explained later on. change [x v] by (Actual Resolution) end
Controls
One of the custom blocks in the "raycast" script was the control block, "Read Keys." Here, we'll focus on that script.
define Read Keys if <not <(Touching Wall) = [1]>> then if <key [up arrow v] pressed?> then change [X Position v] by ((X Direction) * (Move Speed)) change [Y Position v] by ((Y Direction) * (Move Speed)) end end if <not <(Touching Wall) = [-1]>> then if <key [down arrow v] pressed?> then change [X Position v] by ((X Direction) * (Move Speed)) change [Y Position v] by ((Y Direction) * (Move Speed)) end end if <key [right arrow v] pressed?> then set [Direction X Old v] to (Direction X) set [Direction X v] to (((Direction X) * ([cos v] of ((Rotation Speed) * (-1)))) - ((Direction Y) * ([sin v] of ((Rotation Speed) * (-1))))) set [Direction Y v] to (((Direction X Old) * ([sin v] of ((Rotation Speed) * (-1)))) + ((Direction Y) * ([cos v] of ((Rotation Speed) * (-1))))) set [Plane X Old v] to (Plane X) set [Plane X v] to (((Plane X) * ([cos v] of ((Rotation Speed) * (-1)))) - ((Plane Y) * ([sin v] of ((Rotation Speed) * (-1))))) set [Plane Y v] to (((Plane X Old) * ([sin v] of ((Rotation Speed) * (-1)))) + ((Plane Y) * ([cos v] of ((Rotation Speed) * (-1))))) end if <key [left arrow v] pressed?> then set [Direction X Old v] to (Direction X) set [Direction X v] to (((Direction X Old) * ([cos v] of ((Rotation Speed) * (1)))) - ((Direction Y) * ([sin v] of ((Rotation Speed) * (1))))) set [Direction Y v] to (((Direction X Old) * ([sin v] of ((Rotation Speed) * (1)))) + ((Direction Y) * ([cos v] of ((Rotation Speed) * (1))))) set [Plane X Old v] to (Plane X) set [Plane X v] to (((Plane X) * ([cos v] of ((Rotation Speed) * (1)))) - ((Plane Y) * ([sin v] of ((Rotation Speed) * (1))))) set [Plane Y v] to (((Plane X Old) * ([sin v] of ((Rotation Speed) * (1)))) + ((Plane Y) * ([cos v] of ((Rotation Speed) * (1))))) end
Calculating Walls
Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. |
This section explains the block that calculates the walls. Unfortunately, wall touch detection is not included. If you know how to do this, please contact Furious-.
define Calculate Walls set [Touching Wall v] to [0] set [Distance X Delta v] to ([sqrt v] of ((1) + (((Ray Y Direction) * (Ray Y Direction)) / ((Ray X Direction) * (Ray X Direction))))) set [Distance Y Delta v] to ([sqrt v] of ((1) + (((Ray X Direction) * (Ray X Direction)) / ((Ray Y Direction) * (Ray Y Direction))))) set [Wall Found v] to [0] // Once the ray hits a wall, this variable is set to one, which is the same as the boolean value "true" in this case. if <(Ray X Direction) < [0]> then set [Step X v] to [-1] set [Side X Distance v] to (((Ray X Position) - (Map X)) * (Distance X Delta)) else set [Step X v] to [1] set [Side X Distance v] to ((((Map X) + (1)) - (Ray X Position)) * (Distance X Delta)) end if <(Ray X Direction) < [0]> then set [Step Y v] to [-1] set [Side Y Distance v] to (((Ray Y Position) - (Map Y)) * (Distance Y Delta)) else set [Step Y v] to [1] set [Side Y Distance v] to ((((Map Y) + (1)) - (Ray Y Position)) * (Distance Y Delta)) end repeat until <(Wall Found) = [1]> if <(Side X Distance) < (Side Y Distance)> then change [Side X Distance v] by (Distance X Delta) change [Map X v] by (Step X) set [side v] to [0] // The variable "side" is referring to which wall is being faced, a Y wall or an X wall. In this case, it is an X wall. else change [Side Y Distance v] by (Distance Y Delta) change [Map Y v] by (Step Y) set [side v] to [1] // In this case, the ray has hit a Y wall. end set [Map XY v] to (letter (Map Y) of (item (Map X) of [World Map v])) // Map XY is the item of the grid that the ray is in. Example: if X is eleven and Y is seven, then Map XY would be (letter 7 of (item 11 of World Map). if <(Map XY) > [0]> then // If Map XY is more than zero, it has hit a wall. set [Wall Found v] to [1] end end if <(side) = [0]> then set [Perpendicular Wall Distance v] to ([abs v] of ((((Map X) - (Ray X Position)) + (((1) - (Step X)) / (2))) / (Ray X Direction))) else set [Perpendicular Wall Distance v] to ([abs v] of ((((Map Y) - (Ray Y Position)) + (((1) - (Step Y)) / (2))) / (Ray Y Direction))) end set [Line Height v] to ([abs v] of ((h) / (Perpendicular Wall Distance))) set [Draw Start v] to (((0) - (Line Height)) / (2)) set [Draw End v] to ((Line Height) / (2))
Drawing the Walls
The last part in this tutorial is the pen script that draws the walls. It is a very simple script.
define Draw Walls set pen color to [#179fd7] if <(side) = [1]> then set [Brightness v] to (115) // This script makes the Y walls darker than the X walls for a nice effect. else set [Brightness v] to (150) end go to x: (x) y: (Draw Start) set pen shade to ((Brightness) * (-0.2)) set pen size to (Actual Resolution) pen down go to x: (x) y: ((2) * (Draw End)) pen up set pen size to (1) set pen shade to (0)
And that wraps up the list-based raycasting tutorial! Enjoy!
External Links
- Ray casting on Wikipedia