Created
May 5, 2018 01:26
-
-
Save 2DArray/89ef900f313ce199c15c83e022c115ee to your computer and use it in GitHub Desktop.
Pico8 Fluid Sim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fluid={} | |
poke(0x5f2d,1) | |
function spawndrop(x,y) | |
// {x, y, old_x, old_y, hit_count} | |
add(fluid,{x,y,x,y,1}) | |
end | |
function sortfluid() | |
for i=2,#fluid do | |
local drop=fluid[i] | |
for j=i-1,0,-1 do | |
if j>0 and fluid[j][1]>drop[1] then | |
fluid[j+1]=fluid[j] | |
else | |
fluid[j+1]=drop | |
break | |
end | |
end | |
end | |
end | |
function solvefluid() | |
local count=#fluid | |
for i,drop1 in pairs(fluid) do | |
for j=i+1,count do | |
drop2=fluid[j] | |
if drop2[1]>drop1[1]+5 then | |
break | |
end | |
local dx=drop2[1]-drop1[1] | |
local dy=drop2[2]-drop1[2] | |
local dist=max(dx*dx+dy*dy,1) | |
if dist<25 then | |
drop1[5]+=1 | |
drop2[5]+=1 | |
dist=sqrt(dist) | |
local push=(dist-5)*.5/dist | |
dx*=push | |
dy*=push | |
drop2[1]-=dx | |
drop2[2]-=dy | |
drop1[1]+=dx | |
drop1[2]+=dy | |
end | |
end | |
end | |
end | |
function movefluid() | |
local mx=stat(32)-64 | |
local my=stat(33)-64 | |
for i,drop in pairs(fluid) do | |
local startx=drop[1] | |
local starty=drop[2] | |
drop[4]-=.12/drop[5] | |
local dx=mx-drop[1] | |
local dy=my-drop[2] | |
if abs(dx)+abs(dy)<40 then | |
if dx*dx+dy*dy<400 then | |
local dist=max(sqrt(dx*dx+dy*dy),1) | |
drop[3]+=dx/dist*.4 | |
drop[4]+=dy/dist*.4 | |
end | |
end | |
drop[1]+=(drop[1]-drop[3])*.98 | |
drop[2]+=(drop[2]-drop[4])*.98 | |
if abs(drop[2])>64 then | |
drop[2]=64*sgn(drop[2]) | |
drop[4]=0 | |
end | |
if abs(drop[1])>64 then | |
drop[1]=64*sgn(drop[1]) | |
drop[3]=0 | |
end | |
drop[3]=startx | |
drop[4]=starty | |
drop[5]=1 | |
end | |
end | |
function _update() | |
if #fluid<270 then | |
spawndrop(-50+rnd(100),-64) | |
end | |
sortfluid() | |
solvefluid() | |
movefluid() | |
end | |
function _draw() | |
camera(-64,-64) | |
cls() | |
for i,drop in pairs(fluid) do | |
circ(drop[1],drop[2],4,13) | |
end | |
for i,drop in pairs(fluid) do | |
circfill(drop[1],drop[2],3,1) | |
end | |
pset(stat(32)-64,stat(33)-64,7) | |
print("cpu: "..flr(stat(1)*100).."%",-63,-63,5) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment