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