-
Notifications
You must be signed in to change notification settings - Fork 0
/
threedeeExample.p8
166 lines (148 loc) · 4.17 KB
/
threedeeExample.p8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
pico-8 cartridge // http://www.pico-8.com
version 29
__lua__
cube = {
{
{-1,-1,-1}, -- points
{-1,-1,1},
{1,-1,1},
{1,-1,-1},
{-1,1,-1},
{-1,1,1},
{1,1,1},
{1,1,-1},
{-0.5,-0.5,-0.5}, -- inside
{-0.5,-0.5,0.5},
{0.5,-0.5,0.5},
{0.5,-0.5,-0.5},
{-0.5,0.5,-0.5},
{-0.5,0.5,0.5},
{0.5,0.5,0.5},
{0.5,0.5,-0.5}
},
{
{1,2}, -- lines
{2,3},
{3,4},
{4,1},
{5,6},
{6,7},
{7,8},
{8,5},
{1,5},
{2,6},
{3,7},
{4,8},
{8+1,8+2}, -- inside
{8+2,8+3},
{8+3,8+4},
{8+4,8+1},
{8+5,8+6},
{8+6,8+7},
{8+7,8+8},
{8+8,8+5},
{8+1,8+5},
{8+2,8+6},
{8+3,8+7},
{8+4,8+8},
{1,9},
{2,10},
{3,11},
{4,12},
{5,13},
{6,14},
{7,15},
{8,16}
}
}
function _init()
cam = {0,0,-2.5} -- Initialise the camera position
mult = 64 -- View multiplier
a = flr(rnd(3))+1 -- Angle for random rotation
t = flr(rnd(50))+25 -- Time until next angle change
end
function _update()
-- Handle the inputs
if btn(0) then cam[1] -= 0.1 end
if btn(1) then cam[1] += 0.1 end
if btn(2) then cam[2] += 0.1 end
if btn(3) then cam[2] -= 0.1 end
if btn(4) then cam[3] -= 0.1 end
if btn(5) then cam[3] += 0.1 end
t -= 1 -- Decrease time until next angle change
if t <= 0 then -- If t is 0 then change the random angle and restart the timer
t = flr(rnd(50))+25 -- Restart timer
a = flr(rnd(3))+1 -- Update angle
end
cube = rotate_shape(cube,a,0.01) -- Rotate our cube
end
function _draw()
cls() -- Clear the screen
print("t="..t,0,6*0) -- Print time until angle change
print("x="..cam[1],0,6*1) -- Print x, y, and z location of the camera
print("y="..cam[2],0,6*2)
print("z="..cam[3],0,6*3)
draw_shape(cube) -- Draw the cube
end
function draw_shape(s,c)
for l in all(s[2]) do -- For each line in the shape...
draw_line(s[1][l[1]], s[1][l[2]], c) -- Draw the line
end
end
function draw_line(p1,p2,c)
x0, y0 = project(p1) -- Get the 2d location of the 3d points...
x1, y1 = project(p2)
line(x0, y0, x1, y1, c or 11) -- And draw a line between them
end
function draw_point(p,c)
x, y = project(p) -- Get the 2d location of the 3d point...
pset(x, y, c or 11) -- And draw the point
end
function project(p)
x = (p[1]-cam[1])*mult/(p[3]-cam[3]) + 127/2 -- Calculate x and center it
y = -(p[2]-cam[2])*mult/(p[3]-cam[3]) + 127/2 -- Calculate y and center it
return x, y -- Return the two points
end
function translate_shape(s,t)
ns = {{},s[2]}
-- Copy the shape, but zero out the points and keep the lines
for p in all(s[1]) do -- For each point in the original shape...
add(ns[1],{p[1]+t[1],p[2]+t[2],p[3]+t[3]})
-- Add the displacement to the point and add it to our new shape
end
return ns -- Return the new shape
end
function rotate_shape(s,a,r)
ns = {{},s[2]}
-- Copy the shape, but zero out the points and keep the lines
for p in all(s[1]) do -- For each point in the original shape...
add(ns[1], rotate_point(p,a,r))
-- Rotate the point and add it to the new shape
end
return ns -- Return the new shape
end
function rotate_point(p,a,r)
-- Figure out which axis weヌ█▥re rotating on
if a==1 then
x,y,z = 3,2,1
elseif a==2 then
x,y,z = 1,3,2
elseif a==3 then
x,y,z = 1,2,3
end
_x = cos(r)*(p[x]) - sin(r) * (p[y]) -- Calculate the new x location
_y = sin(r)*(p[x]) + cos(r) * (p[y]) -- Calculate the new y location
np={}
-- Make new point and assign the new x and y to the correct axes
np[x] = _x
np[y] = _y
np[z] = p[z]
return np -- Return new point
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000