By popular request (two people asked), here’s the code for my latest mesh experiments. What I’ve been trying to do is stick a bit of physics in to a mesh by defining some dynamical rules. If you look at the start of the code then there are a few options for different settings: what touching the screen means (method
) and how the vertices of the mesh react to each other (the idea is to model them either as connected by springs - NEWTON
(though HOOKE
would be more appropriate) or as a rubber sheet (TENSION
)). showpoints
is quite fun.
displayMode(FULLSCREEN)
--supportedOrientations(LANDSCAPE_LEFT)
--startRecording()
--stopRecording()
DRAG = 1
REPELL = 2
ATTRACT = 3
NEWTON = 1
TENSION = 2
function setup()
watch("debug")
method = REPELL
ode = TENSION
--showpoints = true
screen = mesh()
side = 50
spr = 1
fr = .1
ht = side*math.sqrt(3)/2
local n = math.floor(WIDTH/side)+1
local m = math.floor(HEIGHT/ht)+1
local ad,v,rpts,pt
local pts = {}
tri = {}
for i=1,m do
rpts = {}
ad = (1-i%2)*side/2
for j = 1,n do
v = vec2(ad + (j-1)*side,(i-1)*ht)
pt = {point = v, velocity = vec2(0,0), neighbours = {}}
if j ~= 1 then
table.insert(pt.neighbours,rpts[j-1])
table.insert(rpts[j-1].neighbours,pt)
end
if i ~= 1 then
table.insert(pt.neighbours,pts[i-1][j])
table.insert(pts[i-1][j].neighbours,pt)
if i%2 == 1 then
if j ~= 1 then
table.insert(pt.neighbours,pts[i-1][j-1])
table.insert(pts[i-1][j-1].neighbours,pt)
table.insert(tri,{pt,rpts[j-1],pts[i-1][j-1]})
table.insert(tri,{pt,pts[i-1][j-1],pts[i-1][j]})
end
else
if j ~= n then
table.insert(pt.neighbours,pts[i-1][j+1])
table.insert(pts[i-1][j+1].neighbours,pt)
table.insert(tri,{pt,pts[i-1][j],pts[i-1][j+1]})
end
if j ~= 1 then
table.insert(tri,{pt,rpts[j-1],pts[i-1][j]})
end
end
end
table.insert(rpts,pt)
end
table.insert(pts,rpts)
end
points = {}
for k,v in ipairs(pts) do
for l,u in ipairs(v) do
table.insert(points,u)
end
end
local n
for k,v in ipairs(points) do
n = 0
for l,u in ipairs(v.neighbours) do
n = n + 1
end
if n == 6 then
v.fixed = false
else
v.fixed = true
end
end
img = image(WIDTH,HEIGHT)
pushStyle()
setContext(img)
fill(0, 107, 255, 255)
noSmooth()
rect(0,0,WIDTH,HEIGHT)
font("Noteworthy-Bold")
fontSize(160)
fill(255, 255, 255, 255)
local fm = fontMetrics()
local h = 1.8*fm.xHeight
fill(0, 255, 21, 255)
text("Words",WIDTH/2,HEIGHT/2 + h)
text("are",WIDTH/2,HEIGHT/2)
text("flexible",WIDTH/2,HEIGHT/2 - h)
setContext()
popStyle()
screen.texture = img
texc = {}
for k,v in ipairs(tri) do
for l,u in ipairs(v) do
table.insert(texc,vec2(u.point.x/WIDTH,u.point.y/HEIGHT))
end
end
screen.texCoords = texc
--[[
screen = mesh()
local a = {}
local b = {}
local c = {}
a.point = vec2(WIDTH/2,HEIGHT/2) + vec2(0,200)
b.point = vec2(WIDTH/2,HEIGHT/2) + vec2(-200,0)
c.point = vec2(WIDTH/2,HEIGHT/2) + vec2(200,0)
a.velocity = vec2(0,0)
b.velocity = vec2(0,0)
c.velocity = vec2(0,0)
a.neighbours = {b,c}
b.neighbours = {a,c}
c.neighbours = {b,a}
a.fixed = false
b.fixed = false
c.fixed = false
tri = {{a,b,c}}
points = {a,b,c}
--]]
end