jig487

lineFollower

Oct 16th, 2021 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.06 KB | None | 0 0
  1. local ar = peripheral.find("arController")
  2.  
  3. local draw = require("drawingAPI")
  4. --[[
  5.     bindOutputMode(mode)
  6.     drawTri(p1,p2,p3,c,pr)
  7. ]]
  8.  
  9. local function multiplyVerts(mat, vert) --m == matrix, v == vertice list
  10.     if #mat ~= 16 then
  11.         error("Matrix must have length of 16 (4x4 matrix)")
  12.     end
  13.     local result = {}
  14.     for i = 1, #vert, 4 do --Iterate through each vertice
  15.         for k = 1, 4 do --Multiply said vertice by the given matrix. result[1 -> 4] are equal to a 4x1 matrix.
  16.             result[i+k-1] =
  17.               ( mat[k*4-3] * vert[i]   )
  18.             + ( mat[k*4-2] * vert[i+1] )
  19.             + ( mat[k*4-1] * vert[i+2] )
  20.             + ( mat[k*4]   * vert[i+3] )
  21.         end
  22.     end
  23.     return result
  24. end
  25.  
  26. local function drawSolidObj(pr, vL) --iL == index List. vL == vertex list. cL == color list
  27.     local cL = iL.colorList
  28.         --OPTIMIZATION here?. Probably should convert to 1D at least
  29.     for i = 1, #iL, 3 do --Try to draw a triangle
  30.         drawTri( vL[i], vL[i+1], vL[i+2], cL[(i+2)/3], pr )
  31.     end
  32. end
  33.  
  34. local function makeRotation(eulers)
  35.     local x = math.rad(eulers.x)
  36.     local y = math.rad(eulers.y)
  37.     local z = math.rad(eulers.z)
  38.  
  39.     local sx = math.sin(x)
  40.     local sy = math.sin(y)
  41.     local sz = math.sin(z)
  42.    
  43.     local cx = math.cos(x)
  44.     local cy = math.cos(y)
  45.     local cz = math.cos(z)
  46.          
  47.     return
  48.     {
  49.         cy * cz, -cy * sz, sy, 0,
  50.         (sx * sy * cz) + (cx * sz), (-sx * sy * sz) + (cx * cz), -sx * cy, 0,
  51.         (-cx * sy * cz) + (sx * sz), (cx * sy * sz) + (sx * cz), cx * cy, 0,
  52.         0, 0, 0, 1, }
  53. end
  54.  
  55. local function makeTranslation(translation)
  56.     return
  57.     {
  58.         1, 0, 0, translation.x,
  59.         0, 1, 0, translation.y,
  60.         0, 0, 1, translation.z,
  61.         0, 0, 0, 1, }
  62. end
  63.  
  64. local function makeScale(scale)
  65.     return
  66.     {
  67.         scale.x, 0, 0, 0,
  68.         0, scale.y, 0, 0,
  69.         0, 0, scale.z, 0,
  70.         0, 0, 0, 1 }        
  71. end
  72.  
  73. local function newSqr()
  74.     local objData = {
  75.         scale = vector.new(1,1,1),
  76.         loc = vector.new(0,0,0),
  77.         rot = vector.new(0,0,0),
  78.         colorList = { 0xE56399, 0x7F96FF, },
  79.         indexList = { 1,3,2, 3,4,2, },
  80.         vertices = {
  81.             -0.5, -0.5, -0.5, 1,
  82.              0.5, -0.5, -0.5, 1,
  83.             -0.5,  0.5, -0.5, 1,
  84.              0.5,  0.5, -0.5, 1, } }
  85.         for i,val in ipairs(objData.indexList) do
  86.             objData.indexList[i] = val*4-3
  87.         end
  88.     return objData
  89. end
  90.  
  91. local oL = {
  92.     sqr1 = newSqr()
  93. }
  94.  
  95. oL.sqr1.scale = vector.new(10,10,10)
  96.  
  97. local rotInc = vector.new(3,3,3)
  98.  
  99. for i = 1, 100 do
  100.     local projected = {}
  101.     oL.sqr1.rot = oL.sqr1.rot:add(rotInc)
  102.  
  103.     for name,objectData in pairs(oL) do
  104.         projected[name] = multiplyVerts(makeTranslation(objectData.loc), makeRotation(objectData.rot), makeScale(objectData.scale), objectData.vertices)
  105.         projected[name].colorList = oL.colorList
  106.     end
  107.  
  108.     sleep(0.8)
  109.     ar.clear()
  110.  
  111.     for name,vL in pairs(projected) do
  112.         draw.drawSolidObj(ar,vL)
  113.     end
  114. end
Add Comment
Please, Sign In to add comment