Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » 3D on scratch, And how it's professionally accomplished
- GL11B
-
Scratcher
2 posts
3D on scratch, And how it's professionally accomplished
Hi, this is really interesting. A few years ago I attempted to make a 3D project using Griffpatch’s ray casting tutorial, although at the time I didn’t really understand how it worked, after a while I figured out that the ray casting laser was like a camera which projected the area into a 3D space, however I still don’t understand how the laser manages to generate the surroundings onto the screen, can you help me understand?Well, the idea of “line intersection raycasting” is to eliminate the slowest bottleneck of a raycaster: Stepping to detect collision. Instead of having a starting point and a direction vector (like the sprite direction in griffpatch's raycaster), you have 1 line segment with a *known* start and end point. The start point is just the x position, and the end point is the direction vector multiplied by a huge value like 10000.
The idea is, line segment intersection is incredibly fast to calculate in programming. Using a simple formula and 2 values (A and B), and 2 lines (one is the Ray and one is just a wall) , If the algorithm returned A and B as values between 0 and 1 (So, (A > 0) and (A < 1) and (B > 0) and (B < 1)) that means an intersection happened! You might ask on how this can render entire scenes if it can only see one wall at once, and the answer is by comparing one wall at a time from a list. you'd have a list containing all your walls (which remember, are just 2D line segments), and a loop. each iteration of the loop, you'd compare the line in the list with the ray (the long line from before).
And if you're curious, the ray vector is super simple. instead of being a direction (like in griffpatch's raycaster), it's 2 variables, vectorX and vectorY. vectorY is just your FOV (usually I set it to 240), and vectorX is just the DrawX (the x position of the vertical line.)
Now, there are some technical details, like using A for the distance and B for the texture U, or that the walls need to be sorted now (preferably using a Z buffer), or that you'll need to rotate the vectors with a rotation matrix, so it's not exactly “easy” to make to be entirely honest. you'll need to do alot of research before actually making such a render.
Finally, if you're confused whether you should use this method in future projects or not, It has it's pros and cons. It's really easy to texture, it has capability for both 2D and 3D slopes, and it avoids 2D and Z clipping entirely. However, if you want the ability to look up and down, have completely free geometry in a 3D space, and be able to write code to Parse OBJ and import custom models, Learn *rasterization* instead
- gamemaker3121212
-
Scratcher
3 posts
3D on scratch, And how it's professionally accomplished
how to make 3d first fov*x/z and fov*y/z
Last edited by gamemaker3121212 (Aug. 11, 2026 00:26:45)
- gamemaker3121212
-
Scratcher
3 posts
3D on scratch, And how it's professionally accomplished
define go_to_x(X)y(Y)z(Z)
go to x: ((fov) * ((X) / (Z))) y: ((fov)*((X) / (Y)))
define set_p1(x)(y)(z)
set [x1 v] to [x]
set [y1 v] to [y]
set [z1 v] to [z]
define set_p2(x)(y)(z)
set [x2 v] to [x]
set [y2 v] to [y]
set [z2 v] to [z]
define draw_line_from(X1)(Y1)(Z1)to(X2)(Y2)(Z2)
set_p1((X1) - (cam x))(((Y1)) - ((cam y))((Z1) - (cam z))
go_to_(x1)y(y1)z(z1)
pen down
go_to_x(x2)y(y2)z(z2)
pen down
Last edited by gamemaker3121212 (Aug. 11, 2026 00:55:16)
- Raman15
-
Scratcher
100+ posts
3D on scratch, And how it's professionally accomplished
I know and I'm proud of itIts over 1000 now.Good! bro!This topic is so great that it nearly has 1K posts!It is actually 996 posts (my post made it 997) but yeah we are just 3 posts away from this topic having 1000 posts
- CrazyClown30
-
Scratcher
6 posts
3D on scratch, And how it's professionally accomplished
How do we know where the wall was that the line hit? Couldn't it have been anywhere along the line?Well, here's the neat part. the algorithm used to determine if the ray intersects returns 2 values: A, and B. Depending on your configuration, A for instance could be the distance. This is actually all you'd need to make a simple raycaster. if you want more, then B actually returns where the ray hit on the wall itself. this means you could use it for texturing. If you want to know the exact X and Y coordinate, then it's equal to Wall(Starting point) + (Wall(End point) - Wall(Starting point)) * B. Or, you can also do Ray(Starting point) + (Ray(End point) - Ray(Starting point)) * A . both return the X and Y where the intersection happens. ( Note that A and B are floating point values between 0 and 1. )
And what is this algorithm doing?
- Raman15
-
Scratcher
100+ posts
3D on scratch, And how it's professionally accomplished
how to make 3d first fov*x/z and fov*y/zLook at my new signature:
- abraham93420
-
Scratcher
81 posts
3D on scratch, And how it's professionally accomplished
Even though scratch isn't made for 3d if you make a classroom with a picture of whats in the doors and zoom in you can move forward
- acoolkidwashere
-
Scratcher
3 posts
3D on scratch, And how it's professionally accomplished
change [ v] by (0)
change [ v] by (0)
change { v] by (0)
- lisainma
-
New Scratcher
1 post
3D on scratch, And how it's professionally accomplished
move (23) steps
- finnyfun2
-
Scratcher
4 posts
3D on scratch, And how it's professionally accomplished
never mind i cant do this
Last edited by finnyfun2 (Aug. 20, 2026 23:41:01)
- mikhail_a_m
-
Scratcher
34 posts
3D on scratch, And how it's professionally accomplished
hey guys i thought this was an advanced forum so if you are here to say hello or say useless things, please don't. Also, what do you guys think is better, linear or barycentric interpolation for a z-buffer? personally, i use barycentric, mostly because the triangle renderer i use has them, but im pretty sure linear interpolation is more efficient. another question i have is do you guys have trig tables to speed up your engine? just curious
- AMW25
-
Scratcher
52 posts
3D on scratch, And how it's professionally accomplished
hey guys i thought this was an advanced forum so if you are here to say hello or say useless things, please don't. Also, what do you guys think is better, linear or barycentric interpolation for a z-buffer? personally, i use barycentric, mostly because the triangle renderer i use has them, but im pretty sure linear interpolation is more efficient. another question i have is do you guys have trig tables to speed up your engine? just curiousI don’t know but I use raycasting
- tictacsahur123
-
Scratcher
8 posts
3D on scratch, And how it's professionally accomplished
hey guys i thought this was an advanced forum so if you are here to say hello or say useless things, please don't. Also, what do you guys think is better, linear or barycentric interpolation for a z-buffer? personally, i use barycentric, mostly because the triangle renderer i use has them, but im pretty sure linear interpolation is more efficient. another question i have is do you guys have trig tables to speed up your engine? just curioushola

- gamemaker3121212
-
Scratcher
3 posts
3D on scratch, And how it's professionally accomplished
define go to (x)(y)(z)
go to x: ((fov * (x) / (z))) y: (( fov * (y) / (z)))
Last edited by gamemaker3121212 (Aug. 22, 2026 03:35:01)
- Raman15
-
Scratcher
100+ posts
3D on scratch, And how it's professionally accomplished
I'm waiting for scratch 4.0, let's hope ST changes their mind and adds 3D in a simple way?define go to (x)(y)(z)
go to x: ((fov * (x) / (z))) y: (( fov * (y) / (z)))
#1057Yesterday 17:28:48
- That-Synth
-
Scratcher
5 posts
3D on scratch, And how it's professionally accomplished
#1
sending this to griffpatch rn
sending this to griffpatch rn
#1058Today 08:53:54
- Ae86bluebear
-
Scratcher
6 posts
3D on scratch, And how it's professionally accomplished
define go to (x)(y)(z)
go to x: ((fov * (x) / (z))) y: (( fov * (y) / (z)))
when green flag clicked
forever
say [Idk what You're writing]
say [LOL]
play sound [YIPPEEEEE v]
if (pick random (1) to (9999)) then
move (9999) steps
end
end
- Discussion Forums
- » Advanced Topics
-
» 3D on scratch, And how it's professionally accomplished