Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » 3d
- justagamemaker_try_2
-
Scratcher
3 posts
3d
when green flag clicked
clear
pen down
set to 0
set to 0
forever
clear
change by 2
change by 3
// Define the 8 vertices of the cube
set to 100
// Cube vertices relative to center (x,y,z)
// Each vertex is a 3-item list
set to (list)
add (list 1 1 1) to
add (list 1 1 -1) to
add (list 1 -1 1) to
add (list 1 -1 -1) to
add (list -1 1 1) to
add (list -1 1 -1) to
add (list -1 -1 1) to
add (list -1 -1 -1) to
// Function to rotate a point
define rotate(x, y, z)
set to (cos(angleX))
set to (sin(angleX))
set to (cos(angleY))
set to (sin(angleY))
// Rotate around X
set to ((cosX * y) - (sinX * z))
set to ((sinX * y) + (cosX * z))
// Rotate around Y
set to ((cosY * x) + (sinY * z1))
set to ((-sinY * x) + (cosY * z1))
// Return rotated (x2, y1, z2)
return (list x2 y1 z2)
end
// Project 3D to 2D
define project(x, y, z)
set to 3
set to (distance / (distance + z))
set to (x * size * perspective)
set to (y * size * perspective)
return (list screenX screenY)
end
// Rotate and project all vertices, store results
set to (list)
repeat (8)
set to (item (repeatCount) of )
set to (call rotate (item 1 of vertex) (item 2 of vertex) (item 3 of vertex))
set to (call project (item 1 of rotated) (item 2 of rotated) (item 3 of rotated))
add (point) to
end
// Define edges by vertex indices
// Cube edges connect these vertex pairs:
// 1-2,1-3,1-5,2-4,2-6,3-4,3-7,4-8,5-6,5-7,6-8,7-8
set to (list)
add (list 1 2) to
add (list 1 3) to
add (list 1 5) to
add (list 2 4) to
add (list 2 6) to
add (list 3 4) to
add (list 3 7) to
add (list 4 8) to
add (list 5 6) to
add (list 5 7) to
add (list 6 8) to
add (list 7 8) to
// Draw the edges
pen up
repeat (length of edges)
set to (item (repeatCount) of edges)
set to (item 1 of edge)
set to (item 2 of edge)
set pen color to
set to (item p1Index of points)
set to (item p2Index of points)
go to x: (item 1 of p1) y: (item 2 of p1)
pen down
go to x: (item 1 of p2) y: (item 2 of p2)
pen up
end
wait (0.02) seconds
end
clear
pen down
set to 0
set to 0
forever
clear
change by 2
change by 3
// Define the 8 vertices of the cube
set to 100
// Cube vertices relative to center (x,y,z)
// Each vertex is a 3-item list
set to (list)
add (list 1 1 1) to
add (list 1 1 -1) to
add (list 1 -1 1) to
add (list 1 -1 -1) to
add (list -1 1 1) to
add (list -1 1 -1) to
add (list -1 -1 1) to
add (list -1 -1 -1) to
// Function to rotate a point
define rotate(x, y, z)
set to (cos(angleX))
set to (sin(angleX))
set to (cos(angleY))
set to (sin(angleY))
// Rotate around X
set to ((cosX * y) - (sinX * z))
set to ((sinX * y) + (cosX * z))
// Rotate around Y
set to ((cosY * x) + (sinY * z1))
set to ((-sinY * x) + (cosY * z1))
// Return rotated (x2, y1, z2)
return (list x2 y1 z2)
end
// Project 3D to 2D
define project(x, y, z)
set to 3
set to (distance / (distance + z))
set to (x * size * perspective)
set to (y * size * perspective)
return (list screenX screenY)
end
// Rotate and project all vertices, store results
set to (list)
repeat (8)
set to (item (repeatCount) of )
set to (call rotate (item 1 of vertex) (item 2 of vertex) (item 3 of vertex))
set to (call project (item 1 of rotated) (item 2 of rotated) (item 3 of rotated))
add (point) to
end
// Define edges by vertex indices
// Cube edges connect these vertex pairs:
// 1-2,1-3,1-5,2-4,2-6,3-4,3-7,4-8,5-6,5-7,6-8,7-8
set to (list)
add (list 1 2) to
add (list 1 3) to
add (list 1 5) to
add (list 2 4) to
add (list 2 6) to
add (list 3 4) to
add (list 3 7) to
add (list 4 8) to
add (list 5 6) to
add (list 5 7) to
add (list 6 8) to
add (list 7 8) to
// Draw the edges
pen up
repeat (length of edges)
set to (item (repeatCount) of edges)
set to (item 1 of edge)
set to (item 2 of edge)
set pen color to
set to (item p1Index of points)
set to (item p2Index of points)
go to x: (item 1 of p1) y: (item 2 of p1)
pen down
go to x: (item 1 of p2) y: (item 2 of p2)
pen up
end
wait (0.02) seconds
end
- Discussion Forums
- » Advanced Topics
-
» 3d