Discuss Scratch

sandals79775
Scratcher
9 posts

How to make a 3d raycasted project

when green flag clicked 
say [hello it’s me sandals79775 today we’re trying to make 3d raycasted maze ,but I need help]

Last edited by sandals79775 (Yesterday 14:27:35)

-steamboat-
Scratcher
4 posts

How to make a 3d raycasted project

when green flag clicked
think [Hm….]
wait (0.6) secs
say [Sorry, I don’t know trig or I would help you. Sorry.]
ClarkeGhost
Scratcher
100+ posts

How to make a 3d raycasted project

I'm not an expert on that topic, but I do know griffpatch has a tutorial on YouTube on how to make exactly what you want (a 3D raycasted maze).
happywaffle0
Scratcher
100+ posts

How to make a 3d raycasted project

sandals79775 wrote:

when green flag clicked 
say [hello it’s me sandals79775 today we’re trying to make 3d raycasted maze ,but I need help]
end your scratchblock tag bruh

no worries i gotcha

this is just how to make a simple raycaster, see griffpatch's tutorial for a more advanced one
its insanely easier and very simple if you know how it works.
Basically a raycaster is just a 2d project, but simulated in “3d”

First of all: you need a level to begin with. For that, just make a sprite, call it “level” and make its costume a maze, or something in a top-down layout (as if you're looking at it from above)
You also need another sprite, which'll help render the raycaster. Call it the “render” make its 2x2 pixel at the center of the screen.

As for variables, these are the ones you really need:
(player x)
(player y) // keep track of player pos
(player dir) // important!
(trace x)
(trace y) // for raycaster portion.
(distances :: list) // a list; for keeping track of how far something is
// and more, above are the most important ones

Now that you have the essentials: we can start.

note that we will not at all edit the “level” sprite, it just exists. btw make sure it's position is at (0,0), also make its ghost effect 100 as well.
Start with a simple movement script (in the render sprite).
when green flag clicked
set rotation style [don't rotate v] // optional, but recommended
set [ghost v] effect to (100) // needed so you don't see the pixel
set [player x v] to (0)
set [player y v] to (0)
forever
raycaster :: custom // renders the 3d stuff
if <key (up arrow v) pressed?> then
change [velocity v] by (1)
end
if <key (down arrow v) pressed?> then
change [velocity v] by (-1)
end
if <key (left arrow v) pressed?> then
change [player dir v] by (-3)
end
if <key (right arrow v) pressed?> then
change [player dir v] by (3)
end
set [velocity v] to ((velocity) * (0.6)) // friction
point in direction (player dir)
move (velocity) steps
if <touching (level v) ?> then // for collisions
move ((0)-(velocity)) steps
end
set [player x v] to (x position)
set [player y v] to (y position)
end

now that we have the movement script, we can make the engine that make the level actually 3d

this is part optional, but i recommend this, so we'll start with some constants
set [fov v] to (75) // self-explanatory
set [step v] to (2) // for raycaster; lower value == more precision in points
set [detail v] to (75) // how much rays we'll cast (higher == more detail)
set [max dist v] to (200) // recommended to reduce lag; acts as a bound to how far ray can go
now for the actual script that is the raycaster.
define raycaster // run without screen refresh on!
point in direction ((player dir) - ((fov) / (2))) // starting direction
delete all of [distances v]
repeat (detail)
set [dist v] to (0) // keep track of current distance
go to x: (player x) y: (player y)
repeat until <<(dist) > (max dist)> or <touching (level v) ?>>
move (step) steps
change [dist v] by (step)
end
add (dist) to [distances v] // record distance
turn cw ((fov) / (detail)) degrees
end
render :: custom // for later
go to x: (player x) y: (player y)

Now that we have that, we can use the data we recorded (distances list) and actually use it.
We use the pen extension to do this.
define render // run without screen refresh on!
erase all
pen up
set x to (-240)
set pen size to ((480) / (detail)) // tweak this how you want to.
set pen color to [#0000ff] // this also.
set [i v] to (0)
repeat (detail)
change [i v] by (1)
set y to (-40) // tweak this how you want to
set pen (brightness v) to ((100) - ((100) * ((item (i) of [distances v]) / (max dist)))) // tweak this how you want to
pen down
change y by ((((max dist) * (20)) / (item (i) of [distances v])) - (1)) // simple projection formula, change this if you dont like it
pen up
change x by ((480) / (detail))
end
pen up // really don't need this, but i'll include this anyways.

and voila you just made a really basic raycaster

Last edited by happywaffle0 (Yesterday 22:44:46)

Powered by DjangoBB