Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | None | 0 0
  1. pico-8 cartridge // http://www.pico-8.com
  2. version 18
  3. __lua__
  4. -- three dee!
  5. -- by @johanpeitz
  6.  
  7. -- next up
  8. -- camera movement
  9. -- camera rotation
  10.  
  11. function _init()
  12.  tick=0
  13.  
  14.  scene={}
  15.  
  16.  cam={
  17.   x=0,
  18.   y=1,
  19.   z=2,
  20.   pitch=0,
  21.   yaw=0,
  22.   roll=0,
  23.   troll=0
  24.  }
  25.  
  26.  add(scene,make_plane())
  27.  
  28.  cube=make_cube()
  29.  cube.scale=0.5
  30.  cube.y=-0.25
  31.  add(scene,cube)
  32.  
  33.  cube=make_cube()
  34.  cube.scale=0.25
  35.  cube.y=-0.625
  36.  cube.ecol=10
  37.  add(scene,cube)
  38.  
  39.  cube=make_cube()
  40.  cube.scale=0.5
  41.  cube.y=-1
  42.  cube.ecol=8
  43.  add(scene,cube)
  44.  
  45. end
  46.  
  47.  
  48. function make_plane()
  49.  local p={
  50.   x = 0,
  51.   y = 0,
  52.   z = 0,
  53.   pitch = 0,
  54.   yaw = 0,
  55.   roll = 0,
  56.   scale = 1,
  57.   ecol = 7,
  58.   ncol = 6
  59.  }
  60.  p.nodes={
  61.   { -0.5, 0, -0.5},
  62.   {  0.5, 0, -0.5},
  63.   {  0.5, 0,  0.5},
  64.   { -0.5, 0,  0.5},
  65.  }
  66.  p.edges={
  67.   {1,2},
  68.   {2,3},
  69.   {3,4},
  70.   {4,1},
  71.   {3,1},
  72.   {2,4},
  73.  }
  74.  
  75.  return p
  76. end
  77.  
  78. function make_cube()
  79.  local c={
  80.   x = 0,
  81.   y = 0,
  82.   z = 0,
  83.   pitch = 0,
  84.   yaw = 0,
  85.   roll = 0,
  86.   scale = 1,
  87.   ecol = 11,
  88.   ncol = 3
  89.  }
  90.  
  91.  c.nodes={
  92.   { -0.5, -0.5, -0.5},
  93.   { -0.5, -0.5,  0.5},
  94.   { -0.5,  0.5, -0.5},
  95.   { -0.5,  0.5,  0.5},
  96.   {  0.5, -0.5, -0.5},
  97.   {  0.5, -0.5,  0.5},
  98.   {  0.5,  0.5, -0.5},
  99.   {  0.5,  0.5,  0.5},
  100.  }
  101.  
  102.  c.edges={
  103.   {1,2},
  104.   {2,4},
  105.   {4,3},
  106.   {3,1},
  107.   {5,6},
  108.   {6,8},
  109.   {8,7},
  110.   {7,5},
  111.   {1,5},
  112.   {2,6},
  113.   {3,7},
  114.   {4,8},
  115.  }
  116.  
  117.  return c
  118. end
  119.  
  120.  
  121. function _update()
  122.  if (btn()) cube.yaw+=0.02
  123.  
  124.  if (btn(⬆️)) cam.z-=0.05
  125.  if (btn(⬇️)) cam.z+=0.05
  126.  
  127.  if btn(⬅️) then
  128.   cam.x+=0.05
  129.   cam.troll=0.05
  130.  elseif btn(➡️) then
  131.   cam.x-=0.05
  132.   cam.troll=-0.05
  133.  else
  134.   cam.troll=0
  135.  end
  136.  
  137.  cam.roll+=0.1*(cam.troll-cam.roll)
  138.  
  139. end
  140.  
  141.  
  142. function _draw()
  143.  tick+=1
  144.  
  145.  cls()
  146.  
  147.  for m in all(scene) do
  148.   to_cam(m)
  149.   to_scrn(m)
  150.   draw_model(m)
  151.  end
  152.  
  153.  print("∧"..flr(stat(1)*100).."%",0,0,3)
  154. end
  155. -->8
  156. -- 3d stuff
  157.  
  158. function to_cam(m)
  159.  pitch = m.pitch + cam.pitch
  160.  roll  = m.roll  + cam.roll
  161.  yaw   = m.yaw   + cam.yaw
  162.  
  163.  local cosa = cos(roll)
  164.  local sina = sin(roll)
  165.  local cosb = cos(yaw)
  166.  local sinb = sin(yaw)
  167.  local cosc = cos(pitch)
  168.  local sinc = sin(pitch)
  169.  
  170.  local axx = cosa*cosb
  171.  local axy = cosa*sinb*sinc - sina*cosc
  172.  local axz = cosa*sinb*cosc + sina*sinc
  173.  
  174.  local ayx = sina*cosb
  175.  local ayy = sina*sinb*sinc + cosa*cosc
  176.  local ayz = sina*sinb*cosc - cosa*sinc
  177.  
  178.  local azx = -sinb
  179.  local azy = cosb*sinc
  180.  local azz = cosb*cosc
  181.  
  182.  for n in all(m.nodes) do
  183.   local px=n[1]
  184.   local py=n[2]
  185.   local pz=n[3]
  186.   n.cx = cam.x + m.x + m.scale*(axx*px + axy*py + axz*pz)
  187.   n.cy = cam.y + m.y + m.scale*(ayx*px + ayy*py + ayz*pz)
  188.   n.cz = cam.z + m.z + m.scale*(azx*px + azy*py + azz*pz)
  189.  end
  190.  
  191. end
  192.  
  193.  
  194. function to_scrn(m)
  195.  for n in all(m.nodes) do
  196.   n.sx=64+n.cx*64/n.cz
  197.   n.sy=64+n.cy*64/n.cz
  198.  end
  199. end
  200.  
  201.  
  202. function draw_model(m)
  203.  for e in all(m.edges) do
  204.   local n1=m.nodes[e[1]]
  205.   local n2=m.nodes[e[2]]
  206.  
  207.   if n1.cz>0.01 and n2.cz>0.01 then
  208.    line(n1.sx,n1.sy,
  209.         n2.sx,n2.sy,m.ecol)
  210.      
  211.    --pset(n1.sx,n1.sy,m.ncol)
  212.    --pset(n2.sx,n2.sy,m.ncol)
  213.   end
  214.  
  215.  end
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement