Advertisement
nonogamer9

Spinning cubes for ComputerCraft

Jul 19th, 2024 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | Software | 0 0
  1. -- Constants
  2. local width = 51  -- Adjusted width for ComputerCraft monitor
  3. local height = 19  -- Adjusted height for ComputerCraft monitor
  4. local backgroundASCIICode = '.'
  5. local distanceFromCam = 50  -- Adjusted distance for better visibility
  6. local K1 = 20  -- Adjusted perspective constant
  7. local incrementSpeed = 0.2  -- Reduced increment speed for finer control
  8.  
  9. -- Variables
  10. local A, B, C = 0, 0, 0
  11. local cubeWidth = 5  -- Reduced width for the cubes
  12. local horizontalOffset
  13. local zBuffer = {}
  14. local buffer = {}
  15.  
  16. -- Initialize buffers
  17. for i = 1, width * height do
  18.     zBuffer[i] = 0
  19.     buffer[i] = backgroundASCIICode
  20. end
  21.  
  22. -- Calculate X, Y, Z coordinates
  23. local function calculateX(i, j, k)
  24.     return j * math.sin(A) * math.sin(B) * math.cos(C) - k * math.cos(A) * math.sin(B) * math.cos(C) +
  25.            j * math.cos(A) * math.sin(C) + k * math.sin(A) * math.sin(C) + i * math.cos(B) * math.cos(C)
  26. end
  27.  
  28. local function calculateY(i, j, k)
  29.     return j * math.cos(A) * math.cos(C) + k * math.sin(A) * math.cos(C) -
  30.            j * math.sin(A) * math.sin(B) * math.sin(C) + k * math.cos(A) * math.sin(B) * math.sin(C) -
  31.            i * math.cos(B) * math.sin(C)
  32. end
  33.  
  34. local function calculateZ(i, j, k)
  35.     return k * math.cos(A) * math.cos(B) - j * math.sin(A) * math.cos(B) + i * math.sin(B)
  36. end
  37.  
  38. -- Calculate for surface
  39. local function calculateForSurface(cubeX, cubeY, cubeZ, ch)
  40.     local x = calculateX(cubeX, cubeY, cubeZ)
  41.     local y = calculateY(cubeX, cubeY, cubeZ)
  42.     local z = calculateZ(cubeX, cubeY, cubeZ) + distanceFromCam
  43.  
  44.     local ooz = 1 / z
  45.     local xp = math.floor(width / 2 + horizontalOffset + K1 * ooz * x * 2)
  46.     local yp = math.floor(height / 2 + K1 * ooz * y)
  47.  
  48.     local idx = xp + yp * width
  49.     if idx >= 1 and idx <= width * height then
  50.         if ooz > zBuffer[idx] then
  51.             zBuffer[idx] = ooz
  52.             buffer[idx] = ch
  53.         end
  54.     end
  55. end
  56.  
  57. -- Main loop
  58. while true do
  59.     -- Clear buffers
  60.     for i = 1, width * height do
  61.         zBuffer[i] = 0
  62.         buffer[i] = backgroundASCIICode
  63.     end
  64.  
  65.     -- Draw cubes
  66.     local cubeSizes = {5, 3, 2}  -- Adjusted sizes for the cubes
  67.     local offsets = {-2, 1, 4}
  68.  
  69.     for index, size in ipairs(cubeSizes) do
  70.         horizontalOffset = offsets[index] * size
  71.         for cubeX = -size, size, incrementSpeed do
  72.             for cubeY = -size, size, incrementSpeed do
  73.                 calculateForSurface(cubeX, cubeY, -size, '@')
  74.                 calculateForSurface(size, cubeY, cubeX, '$')
  75.                 calculateForSurface(-size, cubeY, -cubeX, '~')
  76.                 calculateForSurface(-cubeX, cubeY, size, '#')
  77.                 calculateForSurface(cubeX, -size, -cubeY, ';')
  78.                 calculateForSurface(cubeX, size, cubeY, '+')
  79.             end
  80.         end
  81.     end
  82.  
  83.     -- Display the buffer
  84.     term.clear()
  85.     term.setCursorPos(1, 1)
  86.     for k = 1, width * height do
  87.         if k % width == 1 and k > 1 then
  88.             print() -- New line
  89.         end
  90.         write(buffer[k])
  91.     end
  92.  
  93.     -- Update angles
  94.     A = A + 0.05
  95.     B = B + 0.05
  96.     C = C + 0.01
  97.  
  98.     os.sleep(0.08) -- Sleep for a bit to control speed
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement