@Kirl Heres my cloth example where I use mesh instead of lines. When I use lines, the fps is about 10, with mesh it’s about 50. I’m using random colors for each mesh. I guess I could use a single image and break it down into all the meshes. In this one, I use 2 triangles per rectangle.
EDIT: In setup, with a CIRCLE size of 0, the cloth passes thru itself. Increasing the size of CIRCLE makes the cloth act stiffer and prevents it from passing thru itself.
EDIT: I wasn’t expecting this to run this fast since I was recalculating the mesh values each draw cycle.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
x1=20
y1=30
size=30
dx,dy=0,0
tab={}
for x=1,x1 do
tab[x]={}
for y=1,y1 do
local r=physics.body(CIRCLE,0)
r.x=100+x*size
r.y=HEIGHT-50-y*size
r.gravityScale=.5
if y==1 then
r.type=STATIC
end
tab[x][y]=r
r=nil
end
end
jVert={}
for x=1,x1 do
for y=2,y1 do
local j=physics.joint(ROPE,tab[x][y-1],tab[x][y],
tab[x][y-1].position,tab[x][y].position,size)
table.insert(jVert,j)
j=nil
end
end
jHorz={}
for x=2,x1 do
for y=1,y1 do
local j=physics.joint(ROPE,tab[x-1][y],tab[x][y],
tab[x-1][y].position,tab[x][y].position,size)
table.insert(jHorz,j)
j=nil
end
end
end
function draw()
background(192, 225, 208, 255)
stroke(0, 161, 255, 255)
strokeWidth(2)
mTab={}
for x=1,x1 do
for y=1,y1 do
if x>1 and y>1 then
table.insert(mTab,vec2(tab[x][y-1].x,tab[x][y-1].y))
table.insert(mTab,vec2(tab[x-1][y-1].x,tab[x-1][y-1].y))
table.insert(mTab,vec2(tab[x-1][y].x,tab[x-1][y].y))
table.insert(mTab,vec2(tab[x][y-1].x,tab[x][y-1].y))
table.insert(mTab,vec2(tab[x-1][y].x,tab[x-1][y].y))
table.insert(mTab,vec2(tab[x][y].x,tab[x][y].y))
end
end
end
m=mesh()
m.vertices=mTab
setColors()
m.colors=cols
setColors()
m:draw()
stroke(255,0,0)
fill(255,0,0)
ellipse(tab[1][1].x,tab[1][1].y,10)
text("Cloth example",WIDTH/2,HEIGHT-10)
text("Slide your finger to move the red dot",WIDTH/2,HEIGHT-40)
text("Rate "..1//DeltaTime,WIDTH/2,HEIGHT-70)
end
function touched(t)
if t.state==MOVING then
for z=1,x1-1 do
tab[z][1].type=DYNAMIC
end
dx=dx+t.deltaX*200
dy=dy+t.deltaY*200
tab[1][1].linearVelocity=vec2(dx,dy)
end
if t.state==ENDED then
dx,dy=0,0
tab[1][1].type=STATIC
collectgarbage()
end
end
function setColors()
if cols==nil then
cols={}
for z=1,#mTab,6 do
col=vec4(math.random(),math.random(),math.random(),1)
for q=1,6 do
table.insert(cols,col)
end
end
end
end