Skip to content

Instantly share code, notes, and snippets.

@weeble
Last active Mar 26, 2022
Embed
What would you like to do?
-- https://twitter.com/weeble/status/1507516219313868803
function density(
px,py,ox,oy,r,s
)
local dx=px-ox
local dy=py-oy
-- Urgh d^2 overflows Pico8's
-- 16.16bit fixed point really
-- easily. Don't let it.
if abs(dx)>r then
return 0
end
if abs(dy)>r then
return 0
end
local d2=dx*dx+dy*dy
local r2=r*r
if d2>r2 then
return 0
end
local t=1-d2/r2
return s*t*t
end
function _draw()
cls(0)
t={}
function e(x,y)
-- Memoise this because calling it
-- less is hard and my brain is bad
local idx=200*y+x
local cached=t[idx]
if cached!=nil then
return cached
end
local d=0
for item in all(origins) do
d=d+density(
x,y,item[1],item[2],item[3],item[4]
)
end
t[idx]=d
return d
end
function blob(x,y,size)
if size==1 then
local v=e(x,y)
if v>0.5 then
pset(x,y,1)
end
else
local v1=e(x,y)
local v2=e(x+size,y)
local v3=e(x,y+size)
local v4=e(x+size,y+size)
if size<=8 then
if max(max(v1,v2),max(v3,v4))<0.5 then
return nil
elseif min(min(v1,v2),min(v3,v4))>0.5 then
fillp(0x0f0f)
rectfill(x,y,x+size,y+size,2)
fillp(0x0000)
rect(x,y,x+size,y+size,3)
return nil
end
end
size\=2
blob(x,y,size)
blob(x+size,y,size)
if size>=2 then
blob(x,y+size,size)
blob(x+size,y+size,size)
end
end
end
blob(0,0,128)
--print(stat(1))
end
function _init()
-- {x,y,radius,density}
origins={
{32,64,48,1},
{96,70,48,1},
{64,0,80,1.8}
}
velocities={
{0.3,0.1},
{-0.2,0.4},
{0.1,-0.5}
}
end
function up(n)
origins[n][1]+=velocities[n][1]
origins[n][2]+=velocities[n][2]
local dx=origins[n][1]-64
local dy=origins[n][2]-64
-- Wild guess at gravity, kinda works
-- Prolly should handle dx==dy==0
local str=1/(dx*dx+dy*dy)
velocities[n][1]-=dx*str
velocities[n][2]-=dy*str
-- Clamp to avoid escape
origins[n][1]=mid(-200,origins[n][1],200)
origins[n][2]=mid(-200,origins[n][2],200)
velocities[n][1]=mid(-32,velocities[n][1],32)
velocities[n][2]=mid(-32,velocities[n][2],32)
end
function _update60()
up(1)
up(2)
up(3)
if btn(⬆️) then
origins[1][2]-=10
end
if btn(⬇️) then
origins[1][2]+=10
end
if btn(⬅️) then
origins[1][1]-=10
end
if btn(➡️) then
origins[1][1]+=10
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment