Skip to content

Instantly share code, notes, and snippets.

@qoh
Last active March 11, 2016 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qoh/8af866b8e6508006148e to your computer and use it in GitHub Desktop.
Save qoh/8af866b8e6508006148e to your computer and use it in GitHub Desktop.
--initialize
local timer = require "lua.timer"
local setColorFX = ts.func("FxDTSBrick", "setColorFX")
local fakeKillBrick = ts.func("FxDTSBrick", "fakeKillBrick")
local setColor = ts.func("FxDTSBrick", "setColor")
local group = SimSet("BrickGroup_46342")
local width, height = 64, 128
local grid = {}
for x = 0, width - 1 do
grid[x] = {}
for y = 0, height - 1 do
grid[x][y] = group:getObject((y * width + x + 1) % 8192)
end
end
--initialize state
-- local fixed = {}
-- local state = {}
-- _G.gfixed = fixed
-- _G.gstate = state
-- local dt = 0.1
--
-- for x = 0, width - 1 do
-- fixed[x] = {}
-- state[x] = {}
-- for y = 0, height - 1 do
-- if x == 0 or x == (width - 1) or y == 0 or y == (height - 1) then
-- fixed[x][y] = true
-- state[x][y] = {0, 0}
-- else
-- fixed[x][y] = false
-- state[x][y] = {math.exp((-(x - width / 2) ^ 2 - (y - height / 2) ^ 2) / width * 6) * 4, 0}
-- end
-- end
-- end
local fixed = {}
local state = {}
_G.gfixed = fixed
_G.gstate = state
local dt = 0.01
local pwidth = 0.1
local mfreq = 8
local direction = math.pi / -2
-- local slit = {[25] = true, [26] = true, [27] = true, [37] = true, [38] = true, [39] = true}
local slit = {[31] = true, [32] = true, [33] = true}
for x = 0, width - 1 do
fixed[x] = {}
state[x] = {}
for y = 0, height - 1 do
if x == 0 or x == (width - 1) or y == 0 or y == (height - 1) or (y == 64 and not slit[x]) then
fixed[x][y] = true
state[x][y] = {0, 0}
else
xl = (x - 32) / 32
yl = (y - 32) / 32
v = math.exp(-(xl ^ 2 + yl ^ 2) / pwidth) * 2
fixed[x][y] = false
state[x][y] = {v * math.sin((math.cos(direction) * xl +
math.sin(direction) * yl) * math.pi * mfreq),
v * math.cos((math.cos(direction) * xl +
math.sin(direction) * yl) * math.pi * mfreq)}
end
end
end
--run every iteration
local function update()
for i = 1, 128 do
for x = 0, width - 1 do
for y = 0, height - 1 do
if not fixed[x][y] then
state[x][y][1] = state[x][y][1] + state[x][y][2] * dt
end
end
end
for x = 0, width - 1 do
for y = 0, height - 1 do
if not fixed[x][y] then
state[x][y][2] = state[x][y][2] +
(state[(x + 1) % width][y ][1] +
state[(x - 1) % width][y ][1] +
state[x ][(y + 1) % height][1] +
state[x ][(y - 1) % height][1] -
4 * state[x][y][1]) * dt
end
end
end
end
for x = 0, width - 1 do
for y = 0, height - 1 do
setColor(grid[x][y], fixed[x][y] and 0 or math.max(0, math.min(63, state[x][y][1] * 64 + 32)))
end
end
_G.sched = timer(64, update)
end
ts.func("cancel")(_G.sched)
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment