jgtrosh

walk.love

Sep 14th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. SX = 800; SY = 500
  2. fov = 360
  3. minz = 0.1
  4.  
  5. function rotateY(point, angle)
  6.     local rad = angle*math.pi/180
  7.     local cosa = math.cos(rad)
  8.     local sina = math.sin(rad)
  9.     local z = point.z*cosa - point.x*sina
  10.     local x = point.z*sina + point.x*cosa
  11.     return {x=x, y=point.y, z=z}
  12. end
  13.  
  14. function project(point)
  15.     local factor = fov/point.z
  16.     local x = point.x*factor + SX/2
  17.     local y = -point.y*factor + SY/2
  18.     return {x=x, y=y, z=1}
  19. end
  20.  
  21. function intersect(v1, v2)
  22.     if v2.z == v1.z then return {x=0, y=0, z=0} end
  23.     xs = (v2.x-v1.x)/(v2.z-v1.z)
  24.     ys = (v2.y-v1.y)/(v2.z-v1.z)
  25.     return {
  26.         x = v1.x+xs*(minz-v1.z),
  27.         y = v1.y+ys*(minz-v1.z),
  28.         z = minz
  29.     }
  30. end
  31.  
  32. function love.load()
  33.     i = 0
  34.     love.graphics.setMode(SX, SY)
  35.     pos = {x=0, y=-1.6, z=15}
  36.     verticeso = {
  37.         { -5,   0,  0}, { -5,   7,  0}, { 5,   7,  0}, { 5,   0,  0}, -- front wall
  38.         { -5,   0, 10}, { -5,   7, 10}, { 5,   7, 10}, { 5,   0, 10}, --  back wall
  39.         {-10,   0, -5}, {-10,   0, 15}, {10,   0, 15}, {10,   0, -5}, -- ground square
  40.         {-.5,   0,  0}, {-.5, 2.5,  0}, {.5, 2.5,  0}, {.5,   0,  0}, -- door
  41.         { -5, 3.5,  0}, { -5, 3.5, 10}, { 5, 3.5, 10}, { 5, 3.5,  0}  -- first floor
  42.     }
  43.     bones = {
  44.         { 1,  2}, { 2,  3}, { 3,  4}, { 4, 16}, {13, 1}, -- front wall (door)
  45.         { 5,  6}, { 6,  7}, { 7,  8}, { 8,  5},          --  back wall
  46.         { 1,  5}, { 2,  6}, { 3,  7}, { 4,  8},          -- front/back
  47.         { 9, 10}, {10, 11}, {11, 12}, {12,  9},          -- ground square
  48.         {13, 14}, {14, 15}, {15, 16},                    -- door
  49.         {17, 18}, {18, 19}, {19, 20}, {20, 17}           -- first floor
  50.     }
  51.     surfaces = {
  52.         {17, 18, 19, 20}, {1, 2, 6, 5}, {5, 6, 7, 8}, {3, 4, 8, 7},
  53.         {2, 6, 7, 3}, {1, 2, 14, 13}, {14, 15, 3, 2}, {16, 15, 3, 4},
  54.         {9, 10, 11, 12}
  55.     }
  56.     vertices = {}
  57.     plots = {}
  58.     for i = 1, #verticeso do
  59.         v = verticeso[i]
  60.         table.insert(vertices, {
  61.             x = v[1]+pos.x,
  62.             y = v[2]+pos.y,
  63.             z = v[3]+pos.z
  64.         })
  65.         vertices[i] = rotateY(vertices[i], 10)
  66.         table.insert(plots, project(vertices[i]))
  67.     end
  68.     --for k, v in pairs(plots) do print(k, v.x) end
  69. end
  70.  
  71. function love.update()
  72.     if love.keyboard.isDown("w") then
  73.         for i = 1, #vertices do
  74.             vertices[i].z = vertices[i].z-6*dt
  75.         end
  76.     end
  77.     if love.keyboard.isDown("s") then
  78.         for i = 1, #vertices do
  79.             vertices[i].z = vertices[i].z+6*dt
  80.         end
  81.     end
  82.     if love.keyboard.isDown("a") then
  83.         for i = 1, #vertices do
  84.             vertices[i] = rotateY(vertices[i], 50*dt)
  85.         end
  86.     end
  87.     if love.keyboard.isDown("d") then
  88.         for i = 1, #vertices do
  89.             vertices[i] = rotateY(vertices[i], -50*dt)
  90.         end
  91.     end
  92.     plots = {}
  93.     for i = 1, #vertices do
  94.         table.insert(plots, project(vertices[i]))  
  95.     end
  96. end
  97.  
  98. function love.draw()
  99.     love.graphics.setColor(255, 255, 255, 150)
  100. -- code for bones
  101. --  for i = 1, #bones do
  102. --      b = bones[i]
  103. --      v0 = vertices[b[1]]
  104. --      v1 = vertices[b[2]]
  105. --      if v0.z > minz or v1.z > minz then
  106. --          p0 = plots[b[1]]
  107. --          p1 = plots[b[2]]
  108. --          if v0.z < v1.z then v0, v1, p0, p1 = v1, v0, p1, p0 end
  109. --          if v0.z > minz and v1.z < minz then
  110. --              p1 = project(intersect(v0, v1))
  111. --          end
  112. --          love.graphics.line(p0.x, p0.y, p1.x, p1.y)
  113. --      end
  114. --  end
  115.     for i = 1, #surfaces do
  116.         s = surfaces[i]
  117.         if #s > 2 then --minimum 3 sides
  118.             vs = {}
  119.             visible = false
  120.             cut = false
  121.             for iv = 1, #s do
  122.                 table.insert(vs, vertices[s[iv]])
  123.                 if vertices[s[iv]].z > minz
  124.                 then visible = true
  125.                 else cut = true end
  126.             end
  127.             if visible then
  128.                 if cut then
  129.                     vsc = {}
  130.                     for iv = 1, #vs do
  131.                         v1 = {x=vs[  iv].x, y=vs[  iv].y, z=vs[  iv].z}
  132.                         if iv == #vs then iv = 0 end
  133.                         v2 = {x=vs[iv+1].x, y=vs[iv+1].y, z=vs[iv+1].z}
  134.                         if v1.z > minz then table.insert(vsc, {x=v1.x, y=v1.y, z=v1.z}) end
  135.                         if v1.z < v2.z then v1, v2 = v2, v1 end
  136.                         if v1.z > minz and v2.z < minz then
  137.                             table.insert(vsc, intersect(v1, v2))
  138.                         end
  139.                     end
  140.                     vs = vsc
  141.                 end
  142.                 polygon = {}
  143.                 for iv = 1, #vs do
  144.                     v = project(vs[iv])
  145.                     table.insert(polygon, v.x)
  146.                     table.insert(polygon, v.y)
  147.                 end
  148.                 love.graphics.polygon("fill", polygon)
  149.             end
  150.         end
  151.     end
  152. end
Advertisement
Add Comment
Please, Sign In to add comment