Draw pictures with physical free line!

Actually I made it long long ago…
And today I will share it.
You can draw lines as you like, and they will has physical substance(right? I’m not good at English)

Code is here:


-- Freeline
-- Made by SoulWINTER 
-- All rights reversed

-- Use this function to perform your initial setup
function setup()
    bodies = {}
    lines = {}
    parameter.watch("1/DeltaTime")
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color
    background(40, 40, 50)
    stroke(255, 255, 255, 255)
    strokeWidth(5)
    for k,v in pairs(lines) do
        line(v.x1,v.y1,v.x2,v.y2)
    end
    for k,v in pairs(bodies) do
        drawBody(v)
    end   
    -- This sets the line thickness
    
    
    -- Do your drawing here
    
end

function touched(t)
    if t.state == MOVING then
        table.insert(lines,{x1=t.prevX,y1=t.prevY,x2=t.x,y2=t.y})
    end
    if t.state == ENDED then
     --   ab = ab + 1
      --  print(ab)
      print(#lines)
        for i = 1,#lines do
            for k,v in pairs(lines) do
                table.insert(bodies,physics.body(EDGE,vec2(v.x1,v.y1),vec2(v.x2,v.y2)))
                table.remove(lines,k)
            end
        end
    end
end


function drawBody(body)
    -- Push style and transform matrix so we can restore them after
    pushStyle()
    pushMatrix()
    
    strokeWidth(5)
    stroke(0, 255, 94, 255)
    translate(body.x, body.y)
    rotate(body.angle)
    
    -- Draw body based on shape type
    if body.shapeType == POLYGON then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points do
            a = points[j]
            b = points[(j % #points)+1]
            line(a.x, a.y, b.x, b.y)
        end
    elseif body.shapeType == CHAIN or body.shapeType == EDGE then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points-1 do
            a = points[j]
            b = points[j+1]
            line(a.x, a.y, b.x, b.y)
        end
    elseif body.shapeType == CIRCLE then
        strokeWidth(3.0)
        line(0,0,body.radius-3,0)
        ellipse(0,0,body.radius*2)
    end
    
    -- Restore style and transform
    popMatrix()
    popStyle()
end

I had written the code like yours earlier than you. And my code is better than yours. I use Chain to finish this and yours uses Edge. Your code will create more physics bodies than mine. I think it needs to be improved. Look these code below.
local body=physics.body(CHAIN,false,table.unpack(touchTab))

@DayLightTimer I wrote this just now. You can alter the line thickness and it’s color. If I wanted to take a few more minutes I could make this a lot better and do a lot more things. Does that mean mine is better and faster than yours. I don’t use any physics object and yours could also be improved. Someone with more coding experience can always write code better that someone without experience and everyone’s code could be improved. It actually took me longer to type this paragraph that it did to write the code.

function setup()
    parameter.integer("size",2,20,2)
    parameter.color("col",255)
    backingMode(RETAINED)
end

function draw()
    stroke(col)
    strokeWidth(size)
    if tx then
        if hx then
            line(hx,hy,tx,ty)
        end
        hx,hy=tx,ty
    end
end

function touched(t)
    if t.state==MOVING then
        tx,ty=t.x,t.y
    elseif t.state==ENDED then
        tx,ty,hx,hy=nil,nil,nil,nil
    end
end

@dave1707 You don’t understand what he meant. He tried to create a physics line as a body. It is not only for painting. His purpose is creating physics body instead of drawing.

@DayLightTimer I tested your code. It’s not as stable as mine. But thanks for your suggestion:)

@SoulWINTER My whole code includes deleting the same points. I didn’t add it to my code above. So when you tested my code, it will be unstable.