Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants
- local width = 51 -- Adjusted width for ComputerCraft monitor
- local height = 19 -- Adjusted height for ComputerCraft monitor
- local backgroundASCIICode = '.'
- local distanceFromCam = 50 -- Adjusted distance for better visibility
- local K1 = 20 -- Adjusted perspective constant
- local incrementSpeed = 0.2 -- Reduced increment speed for finer control
- -- Variables
- local A, B, C = 0, 0, 0
- local cubeWidth = 5 -- Reduced width for the cubes
- local horizontalOffset
- local zBuffer = {}
- local buffer = {}
- -- Initialize buffers
- for i = 1, width * height do
- zBuffer[i] = 0
- buffer[i] = backgroundASCIICode
- end
- -- Calculate X, Y, Z coordinates
- local function calculateX(i, j, k)
- return j * math.sin(A) * math.sin(B) * math.cos(C) - k * math.cos(A) * math.sin(B) * math.cos(C) +
- j * math.cos(A) * math.sin(C) + k * math.sin(A) * math.sin(C) + i * math.cos(B) * math.cos(C)
- end
- local function calculateY(i, j, k)
- return j * math.cos(A) * math.cos(C) + k * math.sin(A) * math.cos(C) -
- j * math.sin(A) * math.sin(B) * math.sin(C) + k * math.cos(A) * math.sin(B) * math.sin(C) -
- i * math.cos(B) * math.sin(C)
- end
- local function calculateZ(i, j, k)
- return k * math.cos(A) * math.cos(B) - j * math.sin(A) * math.cos(B) + i * math.sin(B)
- end
- -- Calculate for surface
- local function calculateForSurface(cubeX, cubeY, cubeZ, ch)
- local x = calculateX(cubeX, cubeY, cubeZ)
- local y = calculateY(cubeX, cubeY, cubeZ)
- local z = calculateZ(cubeX, cubeY, cubeZ) + distanceFromCam
- local ooz = 1 / z
- local xp = math.floor(width / 2 + horizontalOffset + K1 * ooz * x * 2)
- local yp = math.floor(height / 2 + K1 * ooz * y)
- local idx = xp + yp * width
- if idx >= 1 and idx <= width * height then
- if ooz > zBuffer[idx] then
- zBuffer[idx] = ooz
- buffer[idx] = ch
- end
- end
- end
- -- Main loop
- while true do
- -- Clear buffers
- for i = 1, width * height do
- zBuffer[i] = 0
- buffer[i] = backgroundASCIICode
- end
- -- Draw cubes
- local cubeSizes = {5, 3, 2} -- Adjusted sizes for the cubes
- local offsets = {-2, 1, 4}
- for index, size in ipairs(cubeSizes) do
- horizontalOffset = offsets[index] * size
- for cubeX = -size, size, incrementSpeed do
- for cubeY = -size, size, incrementSpeed do
- calculateForSurface(cubeX, cubeY, -size, '@')
- calculateForSurface(size, cubeY, cubeX, '$')
- calculateForSurface(-size, cubeY, -cubeX, '~')
- calculateForSurface(-cubeX, cubeY, size, '#')
- calculateForSurface(cubeX, -size, -cubeY, ';')
- calculateForSurface(cubeX, size, cubeY, '+')
- end
- end
- end
- -- Display the buffer
- term.clear()
- term.setCursorPos(1, 1)
- for k = 1, width * height do
- if k % width == 1 and k > 1 then
- print() -- New line
- end
- write(buffer[k])
- end
- -- Update angles
- A = A + 0.05
- B = B + 0.05
- C = C + 0.01
- os.sleep(0.08) -- Sleep for a bit to control speed
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement