Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pico-8 cartridge // http://www.pico-8.com
- version 18
- __lua__
- -- three dee!
- -- by @johanpeitz
- -- next up
- -- camera movement
- -- camera rotation
- function _init()
- tick=0
- scene={}
- cam={
- x=0,
- y=1,
- z=2,
- pitch=0,
- yaw=0,
- roll=0,
- troll=0
- }
- add(scene,make_plane())
- cube=make_cube()
- cube.scale=0.5
- cube.y=-0.25
- add(scene,cube)
- cube=make_cube()
- cube.scale=0.25
- cube.y=-0.625
- cube.ecol=10
- add(scene,cube)
- cube=make_cube()
- cube.scale=0.5
- cube.y=-1
- cube.ecol=8
- add(scene,cube)
- end
- function make_plane()
- local p={
- x = 0,
- y = 0,
- z = 0,
- pitch = 0,
- yaw = 0,
- roll = 0,
- scale = 1,
- ecol = 7,
- ncol = 6
- }
- p.nodes={
- { -0.5, 0, -0.5},
- { 0.5, 0, -0.5},
- { 0.5, 0, 0.5},
- { -0.5, 0, 0.5},
- }
- p.edges={
- {1,2},
- {2,3},
- {3,4},
- {4,1},
- {3,1},
- {2,4},
- }
- return p
- end
- function make_cube()
- local c={
- x = 0,
- y = 0,
- z = 0,
- pitch = 0,
- yaw = 0,
- roll = 0,
- scale = 1,
- ecol = 11,
- ncol = 3
- }
- c.nodes={
- { -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},
- { 0.5, 0.5, 0.5},
- }
- c.edges={
- {1,2},
- {2,4},
- {4,3},
- {3,1},
- {5,6},
- {6,8},
- {8,7},
- {7,5},
- {1,5},
- {2,6},
- {3,7},
- {4,8},
- }
- return c
- end
- function _update()
- if (btn(❎)) cube.yaw+=0.02
- if (btn(⬆️)) cam.z-=0.05
- if (btn(⬇️)) cam.z+=0.05
- if btn(⬅️) then
- cam.x+=0.05
- cam.troll=0.05
- elseif btn(➡️) then
- cam.x-=0.05
- cam.troll=-0.05
- else
- cam.troll=0
- end
- cam.roll+=0.1*(cam.troll-cam.roll)
- end
- function _draw()
- tick+=1
- cls()
- for m in all(scene) do
- to_cam(m)
- to_scrn(m)
- draw_model(m)
- end
- print("∧"..flr(stat(1)*100).."%",0,0,3)
- end
- -->8
- -- 3d stuff
- function to_cam(m)
- pitch = m.pitch + cam.pitch
- roll = m.roll + cam.roll
- yaw = m.yaw + cam.yaw
- local cosa = cos(roll)
- local sina = sin(roll)
- local cosb = cos(yaw)
- local sinb = sin(yaw)
- local cosc = cos(pitch)
- local sinc = sin(pitch)
- local axx = cosa*cosb
- local axy = cosa*sinb*sinc - sina*cosc
- local axz = cosa*sinb*cosc + sina*sinc
- local ayx = sina*cosb
- local ayy = sina*sinb*sinc + cosa*cosc
- local ayz = sina*sinb*cosc - cosa*sinc
- local azx = -sinb
- local azy = cosb*sinc
- local azz = cosb*cosc
- for n in all(m.nodes) do
- local px=n[1]
- local py=n[2]
- local pz=n[3]
- n.cx = cam.x + m.x + m.scale*(axx*px + axy*py + axz*pz)
- n.cy = cam.y + m.y + m.scale*(ayx*px + ayy*py + ayz*pz)
- n.cz = cam.z + m.z + m.scale*(azx*px + azy*py + azz*pz)
- end
- end
- function to_scrn(m)
- for n in all(m.nodes) do
- n.sx=64+n.cx*64/n.cz
- n.sy=64+n.cy*64/n.cz
- end
- end
- function draw_model(m)
- for e in all(m.edges) do
- local n1=m.nodes[e[1]]
- local n2=m.nodes[e[2]]
- if n1.cz>0.01 and n2.cz>0.01 then
- line(n1.sx,n1.sy,
- n2.sx,n2.sy,m.ecol)
- --pset(n1.sx,n1.sy,m.ncol)
- --pset(n2.sx,n2.sy,m.ncol)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement