Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » 3D in scratch and the way YOU can learn it too
- Johnliu201624
-
Scratcher
22 posts
3D in scratch and the way YOU can learn it too
How YOU and I mean YOU can learn 3D in scratch TOO.
Heres the code.
This may be a little bit complicated to show all at once so lets break it down.
First you do a forever loop and just render.
Then you do move camera. The Move Camera is well to move the camera.
OK lets get to the rendering.
So the rendering is just based on 2 formulas to give a 2D plane a 3D view. First, its the classic x and y divided by z. Then its the scary rotation formula:
x′=xcosθ−ysinθ
y′=xsinθ+ycosθ
OK, this might be a little complicated and scary but here's the explanation.
first equation just rotates x and second just rotates y also. the θ is basically the angle its rotating at.
THATS ALL! ITS KINDA COMPLICATED BUT ITS FINE FOR WELL MORE ADVANCED SCRATCHERS BUT YOU CAN DO IT TOO!!!
Heres the code.
when green flag clicked
set [Camera X v] to (0)
set [Camera Y v] to (0)
set [Camera Z v] to (0)
set [Camera Yaw v] to (0)
set [Camera Pitch v] to (0)
set [Near Plane v] to (1)
set [Focal Length v] to (240)
set [Move Speed v] to (5)
forever
if <key [left arrow v] pressed?> then
change [Camera Yaw v] by (-2)
end
if <key [right arrow v] pressed?> then
change [Camera Yaw v] by (2)
end
if <key [up arrow v] pressed?> then
change [Camera Pitch v] by (2)
end
if <key [down arrow v] pressed?> then
change [Camera Pitch v] by (-2)
end
if <key [w v] pressed?> then
change [Camera X v] by ((sin of (Camera Yaw)) * (Move Speed))
change [Camera Z v] by ((cos of (Camera Yaw)) * (Move Speed))
end
if <key [s v] pressed?> then
change [Camera X v] by ((0)-((sin of (Camera Yaw)) * (Move Speed)))
change [Camera Z v] by ((0)-((cos of (Camera Yaw)) * (Move Speed)))
end
if <key [a v] pressed?> then
change [Camera X v] by ((sin of ((Camera Yaw) - (90))) * (Move Speed))
change [Camera Z v] by ((cos of ((Camera Yaw) - (90))) * (Move Speed))
end
if <key [d v] pressed?> then
change [Camera X v] by ((sin of ((Camera Yaw) + (90))) * (Move Speed))
change [Camera Z v] by ((cos of ((Camera Yaw) + (90))) * (Move Speed))
end
erase all
Draw 3D Line (-50) (0) (200) (50) (0) (200)
end
define Draw 3D Line (WX1) (WY1) (WZ1) (WX2) (WY2) (WZ2)
set [X1 v] to ((WX1) - (Camera X))
set [Y1 v] to ((WY1) - (Camera Y))
set [Z1 v] to ((WZ1) - (Camera Z))
set [X2 v] to ((WX2) - (Camera X))
set [Y2 v] to ((WY2) - (Camera Y))
set [Z2 v] to ((WZ2) - (Camera Z))
set [Temp X v] to (((X1) * (cos (Camera Yaw))) + ((Z1) * (sin (Camera Yaw))))
set [Temp Z v] to (((Z1) * (cos (Camera Yaw))) - ((X1) * (sin (Camera Yaw))))
set [X1 v] to (Temp X)
set [Z1 v] to (Temp Z)
set [Temp X v] to (((X2) * (cos (Camera Yaw))) + ((Z2) * (sin (Camera Yaw))))
set [Temp Z v] to (((Z2) * (cos (Camera Yaw))) - ((X2) * (sin (Camera Yaw))))
set [X2 v] to (Temp X)
set [Z2 v] to (Temp Z)
set [Temp Y v] to (((Y1) * (cos (Camera Pitch))) - ((Z1) * (sin (Camera Pitch))))
set [Temp Z v] to (((Y1) * (sin (Camera Pitch))) + ((Z1) * (cos (Camera Pitch))))
set [Y1 v] to (Temp Y)
set [Z1 v] to (Temp Z)
set [Temp Y v] to (((Y2) * (cos (Camera Pitch))) - ((Z2) * (sin (Camera Pitch))))
set [Temp Z v] to (((Y2) * (sin (Camera Pitch))) + ((Z2) * (cos (Camera Pitch))))
set [Y2 v] to (Temp Y)
set [Z2 v] to (Temp Z)
if <<(Z1) < (Near Plane)> and <(Z2) < (Near Plane)>> then
stop [this script v]
end
if <(Z1) < (Near Plane)> then
set [T v] to (((Near Plane) - (Z1)) / ((Z2) - (Z1)))
set [X1 v] to ((X1) + (((X2) - (X1)) * (T)))
set [Y1 v] to ((Y1) + (((Y2) - (Y1)) * (T)))
set [Z1 v] to (Near Plane)
end
if <(Z2) < (Near Plane)> then
set [T v] to (((Near Plane) - (Z2)) / ((Z1) - (Z2)))
set [X2 v] to ((X2) + (((X1) - (X2)) * (T)))
set [Y2 v] to ((Y2) + (((Y1) - (Y2)) * (T)))
set [Z2 v] to (Near Plane)
end
set [SX1 v] to (((X1) / (Z1)) * (Focal Length))
set [SY1 v] to (((Y1) / (Z1)) * (Focal Length))
set [SX2 v] to (((X2) / (Z2)) * (Focal Length))
set [SY2 v] to (((Y2) / (Z2)) * (Focal Length))
pen up
go to x:(SX1) y:(SY1)
pen down
go to x:(SX2) y:(SY2)
pen up
This may be a little bit complicated to show all at once so lets break it down.
First you do a forever loop and just render.
Then you do move camera. The Move Camera is well to move the camera.
OK lets get to the rendering.
So the rendering is just based on 2 formulas to give a 2D plane a 3D view. First, its the classic x and y divided by z. Then its the scary rotation formula:
x′=xcosθ−ysinθ
y′=xsinθ+ycosθ
OK, this might be a little complicated and scary but here's the explanation.
first equation just rotates x and second just rotates y also. the θ is basically the angle its rotating at.
THATS ALL! ITS KINDA COMPLICATED BUT ITS FINE FOR WELL MORE ADVANCED SCRATCHERS BUT YOU CAN DO IT TOO!!!
Last edited by Johnliu201624 (Yesterday 17:57:40)
- BrainGamer1234
-
Scratcher
100+ posts
3D in scratch and the way YOU can learn it too
How YOU and I mean YOU can learn 3D in scratch TOO.
Heres the code.
when green flag clicked
set to (0)
set to (0)
set to (0)
set to (0)
set to (0)
set to (1)
set to (240)
set to (5)
forever
if <key pressed?> then
change by (-2)
end
if <key pressed?> then
change by (2)
end
if <key pressed?> then
change by (2)
end
if <key pressed?> then
change by (-2)
end
if <key pressed?> then
change by ((sin(Camera Yaw))*(Move Speed))
change by ((cos(Camera Yaw))*(Move Speed))
end
if <key pressed?> then
change by (-1*((sin(Camera Yaw))*(Move Speed)))
change by (-1*((cos(Camera Yaw))
… snip
you need to use [scratchblocks], not [scratch]. I can't read it.
Last edited by BrainGamer1234 (Dec. 4, 2026 02:56:08)
- Johnliu201624
-
Scratcher
22 posts
3D in scratch and the way YOU can learn it too
people are here already
thats crazy
thats crazy
- Discussion Forums
- » Advanced Topics
-
» 3D in scratch and the way YOU can learn it too
