This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
minecraftwarlock's profile picture

Program only rendering once then clearing

Started by minecraftwarlock, 28 June 2015 - 07:38 AM
minecraftwarlock #1
Posted 28 June 2015 - 09:38 AM
The following code i meant to show the camera zooming through a 3D star field but only renders once and then clears.


os.loadAPI("CCG/renderer")
local mon = peripheral.wrap("left")
mon.setTextScale(0.5)
local w, h = mon.getSize()
local ren = renderer.create(mon, 1, 1, w, h)
ren:clear()
local stars = {}
local speed = 20
for i = 1, 128 do
stars[i] = {math.random(w), math.random(h), math.random() + 0.001}
end
local lastFrame = os.clock()
local delta = 0
while true do
local thisFrame = os.clock()
delta = thisFrame - lastFrame
lastFrame = thisFrame

ren:clear()

for i = 1, #stars do
  stars[i][3] = stars[i][3] - speed * delta

  local x = stars[i][1]
  local y = stars[i][2]
  local z = stars[i][3]

  if z <= 0 then
   stars[i] = {math.random(w), math.random(h), math.random() + 0.001}
  end

  ren:pixel(x / z, y / z, 1, 1, 1, " ")
end

ren:update()
sleep(0.5)
end
Bomb Bloke #2
Posted 28 June 2015 - 10:40 AM
os.loadAPI("CCG/renderer")

Kinda hard to comment without access to the rest of the source involved here!

But let's see. "delta" is usually going to be 0.05, and given the "speed" of 20, that means a given star's "z" value is going to decrease by 1 on every iteration. Because these "z" values are pretty much always going to be less than 1 anyway, that means you'll be generating new stars pretty much "always". Even a speed of 1 would only see your stars lasting up to a second! I'd decrease it to something like 0.25, that way they should persist for up to a few seconds.

With that change in place, I'd still expect to see your stars spending much of their time (about 75%) off-screen - for example, if x is 20 and z is 0.25, then it looks like you're going to try to render to column 80. This article covers the technique I reckon you're after.