Does anyone have the old cloth simulation example?

Hi everyone!

Does anyone still have the code for the cloth simulation, which used to be part of the examples (a long time ago)?

Thanks!

@Elias - not sure if this is the one you are interested in. It was originally published by @luatee and @dave1707, not as part of the demos I think

-- Cloth02 by luatee and dave1707
-- from http://codea.io/talk/discussion/6492/cloth-example#latest

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    x1=15
    y1=15
    size=25
    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=10+x*size
            r.y=600-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
    for z=2,x1-1 do
            tab[z][1].type=DYNAMIC
        end
end

function draw()
    background(192, 225, 208, 255)
    stroke(0, 161, 255, 255)
    strokeWidth(2)
    for x=1,x1 do
        for y=1,y1 do
            if x>1 then
                line(tab[x-1][y].x,tab[x-1][y].y,tab[x][y].x,tab[x][y].y)
            end
            if y>1 then
                line(tab[x][y-1].x,tab[x][y-1].y,tab[x][y].x,tab[x][y].y)
            end
        end
    end  
    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)
    text("KB Memory used  "..collectgarbage("count")//1,WIDTH/2,HEIGHT-100)
end

function touched(t)
    if t.state==MOVING then
        for z=1,1 do
            tab[z][1].type=KINEMATIC
        end
        dx=dx+t.deltaX*2
        dy=dy+t.deltaY*2
        tab[1][1].linearVelocity=vec2(dx,dy)
    end
    if t.state==ENDED then
        dx,dy=0,0
        for z=1,1 do
            tab[z][1].type=STATIC
        end
        collectgarbage()
    end    
end

May be of interest anyway.

Here’s the one I wrote, not sure if this is what you’re after.

viewer.mode=FULLSCREEN

function setup()
    x1=20
    y1=30
    local tTab={} 
    local xs=1/(x1-1)
    local ys=1/(y1-1)
    for x=0,x1-2 do
        for y=0,y1-2 do
            table.insert(tTab,vec2(x*xs,y*ys))
            table.insert(tTab,vec2(x*xs+xs,y*ys))
            table.insert(tTab,vec2(x*xs+xs,y*ys+ys))            
            table.insert(tTab,vec2(x*xs,y*ys))
            table.insert(tTab,vec2(x*xs,y*ys+ys))
            table.insert(tTab,vec2(x*xs+xs,y*ys+ys))
        end
    end
    local size=30
    dx,dy=0,0
    tab={}
    for x=1,x1 do
        tab[x]={}
        for y=y1,1,-1 do
            local r=physics.body(CIRCLE,0)
            r.x=100+x*size
            r.y=y*size
            r.gravityScale=.5
            if x==1 and y==y1 then
                r.type=STATIC
            end 
            if y==y1 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  
    m=mesh()
    m.texCoords=tTab
    m.texture="Cargo Bot:Startup Screen"   
    m.vertices=tTab
    buf=m:buffer("position")
end

function draw()
    background(192, 224, 225, 255)
    fill(255)
    stroke(0, 161, 255, 255)
    strokeWidth(2)
    local n=1
    for x=1,x1-1 do
        for y=1,y1-1 do
            buf[n]=vec2(tab[x][y].x,tab[x][y].y)
            buf[n+1]=vec2(tab[x+1][y].x,tab[x+1][y].y)
            buf[n+2]=vec2(tab[x+1][y+1].x,tab[x+1][y+1].y)                              
            buf[n+3]=vec2(tab[x][y].x,tab[x][y].y)
            buf[n+4]=vec2(tab[x][y+1].x,tab[x][y+1].y)
            buf[n+5]=vec2(tab[x+1][y+1].x,tab[x+1][y+1].y) 
            n=n+6           
        end
    end  
    m:draw()
    pushStyle()
    stroke(255,0,0)
    fill(255,0,0)
    ellipse(tab[1][y1].x,tab[1][y1].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//1,WIDTH/2,HEIGHT-70)
    text("KB Memory used  "..collectgarbage("count")//1,WIDTH/2,HEIGHT-100)
    popStyle()
end

function touched(t)
    if t.state==MOVING then
        for z=1,x1-1 do
            tab[z][y1].type=DYNAMIC
        end
        dx=dx+t.deltaX*100
        dy=dy+t.deltaY*100
        tab[1][y1].linearVelocity=vec2(dx,dy)
    end
    if t.state==ENDED then
        dx,dy=0,0
        tab[1][y1].type=STATIC
    end    
end

@dave1707 - neat, could you point out the embedding tool used in this forum for code. Not obvious and I need to edit a couple of my posts. Thanks.

@Bri_G To format code, when you’re in the reply window, there’s a bunch of icons along the top of the window. Tap on the gear icon (right most), then select Preformatted Text. Then just paste your code where it says between the pair of the 3 dots.

1 Like

@dave1707 - thanks for the prompt. Modified code above but on another post didn’t have the option. Doing this on my phone, will try on Mac. Could be a time limit on editing options.

Edit: Tried editing out on my Mac, had to discard the partial edit I did on my phone, then carried out an edit on the not imbedded code. Worked fine. I’ll use my Mac in future. Thanks again. Use

@Bri_G If the pencil icon doesn’t show, try tapping the 3 dots below the text window. I think the pencil icon will show there and let you edit the text.

Thanks a ton to both of you! I’ve tried your examples and they look fantastic! :smile: