Bubble Universe

i figured caching sine values would speed it up, but i hadn’t bothered to think of a good way to do it in lua until now :]

local w = 320
local w2 = w/2
local rad = 70
local n = 50
local nc = 100
local step=2
local r = math.pi*2/n
local x,y,t = 0,0,0
local u,v,px,py

local res = 100

sint = {}
for i = 1, math.floor(math.pi*2*res) do
	sint[i] = math.sin(i/res)
end

function fsin(i)
	return sint[(math.floor(i*res)%#sint)+1]
end

function fcos(i)
	return sint[((math.floor((math.pi/2-i)*res))%#sint)+1]
end

draw.enableBuffer(2)
while true do
	if keys.getState(keys.esc) then break end
	draw.clear()
	for i = 0, n-1 do
		for c = 0, nc-1, step do
			u = fsin(i+y)+fsin(r*i+x)
			v = fcos(i+y)+fcos(r*i+x)
			x = u + t
			y = v
			px = u * rad + w2
			py = y * rad + w2
			draw.point(px, py, 
				colors.fromRGB(math.floor(63+i/n*192),
					math.floor(63+c/nc*192),168))
		end
	end
	t=t+0.03
	draw.blitBuffer()
end
draw.enableBuffer(false)
2 Likes

You can do what I do and define a lookup table for only 0 through pi/2 radians, and preprocess your inputs to compute approximate sines and cosines based on this restricted range. With this you can get away with using either a smaller or a higher-resolution look-up table.

yes, that’s how the res value works, the lookup functions scale the inputs with that as well :]

@pelrun I love this thing. Now that I have my Luckfox Lyra installed I messed with getting this going. There is enough memory for a proper framebuffer and I implimented it in python. It renders at about 40 FPS.

really cool when it is going fast.