Skip to content

Instantly share code, notes, and snippets.

@mrange
Last active September 27, 2023 11:14
Show Gist options
  • Save mrange/b94f6e7799b8b475697e8cfb8d91f213 to your computer and use it in GitHub Desktop.
Save mrange/b94f6e7799b8b475697e8cfb8d91f213 to your computer and use it in GitHub Desktop.
Rotating cube in TIC-80
BPM =80
function v3(x,y,z)
return {x,y,z}
end
model = {
vertices = {
v3(-1,-1,-1),
v3( 1,-1,-1),
v3(-1, 1,-1),
v3( 1, 1,-1),
v3(-1,-1, 1),
v3( 1,-1, 1),
v3(-1, 1, 1),
v3( 1, 1, 1),
},
normals = {
v3(-1, 0, 0),
v3( 1, 0, 0),
v3( 0,-1, 0),
v3( 0, 1, 0),
v3( 0, 0,-1),
v3( 0, 0, 1),
},
lines = {
1,2,
2,4,
4,3,
3,1,
5,6,
6,8,
8,7,
7,5,
1,5,
2,6,
3,7,
4,8,
},
triangles = {
10,1,2,4,6,
10,3,1,4,6,
11,5,6,8,5,
11,7,5,8,5,
}
}
function to_screen(ww,p)
local w = ww*10/(10+p[3])
local x = (p[1]*w+1)*68+52
local y = (p[2]*w+1)*68
return {x,y}
end
function transform(t, vs)
local a = t
local b = math.sqrt(0.5)*a
local c = math.cos(a)
local s = math.sin(a)
local cc= math.cos(b)
local ss= math.sin(b)
local nvs = {}
for i = 1,#vs do
local v = vs[i]
local x0 = v[1]
local y0 = v[2]
local z0 = v[3]
local x1 = c*x0+s*y0
local y1 = -s*x0+c*y0
local z1 = z0
local x2 = cc*x1+ss*z1
local y2 = y1
local z2 = -ss*x1+cc*z1
nvs[i] = {x2,y2,z2}
end
return nvs
end
function TIC()
local t = time()/1000.0
cls(15);
local sa = t
sa = math.pi*2*sa*0.25*BPM/60
local sx = 4
local sy = 40+40*math.sin(sa)
spr(0,sx,sy,0,1,0,0,16,6)
spr(96,sx+128,sy,0,1,0,0,13,6)
local b = math.pi*2*t*BPM/60.0
local ww = 0.2*math.pow(0.5+0.5*math.sin(b), 10.0)+0.5
local points = {}
local vertices = transform(t, model.vertices)
for i=1,#vertices do
points[i] = to_screen(ww, vertices[i])
end
local normals = transform(t, model.normals)
local triangles = model.triangles
for i=1,#triangles,5 do
local a = triangles[i]
local b = points[triangles[i+1]]
local c = points[triangles[i+2]]
local d = points[triangles[i+3]]
local e = normals[triangles[i+4]]
if e[3] > 0 then
tri(b[1],b[2],c[1],c[2],d[1],d[2],a)
end
end
local lines = model.lines
for i=1,#lines,2 do
local f = points[lines[i]]
local t = points[lines[i+1]]
local v = vertices[lines[i]]
local w = vertices[lines[i+1]]
local c = ((v[2]+w[2])+2)*2
line(f[1],f[2],t[1],t[2],11)
end
local a = t
a = a*0.5*BPM/60.0
local y = a-math.floor(a)
y = y-0.5
y = y*y
y = 20+300*y
x = 80+80*math.sin(a)
print("Beat dis TCB!!!",x, y,(8*t)%16)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment