if you move your finger around, eventually the lines will start glitch in and getting bigger/wrong angles.
--# Main
-- Trail
-- Use this function to perform your initial setup
function setup()
ball = physics.body(POLYGON,vec2(-25,-25),vec2(-25,25),vec2(25,25),vec2(25,-25))
ball.position = vec2(WIDTH/2,HEIGHT/2)
flor = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
img = image(100,20)
setContext(img)
stroke(255)
strokeWidth(10)
line(10,10,90,10)
setContext()
trail = Trail(50,1,100,ball,"..")
bpos = vec2()
Fps = 0
speed = 3.5
time = 0
end
function touched(t)
bpos = vec2(t.x,t.y)
speed = t.y/100
if t.state == ENDED then end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
time = time + DeltaTime*speed
-- This sets the line thickness
strokeWidth(5)
fill(205)
if ball then
--ball.position = vec2(WIDTH/2,HEIGHT/2)
rect(ball.x-25,ball.y-25,50,50)
ball.linearVelocity = (bpos-ball.position)*10
end
-- Do your drawing here
line(0,0,WIDTH,0)
--bpos = vec2(WIDTH/2,HEIGHT/2)+vec2(math.sin(time)*200,math.cos(time)*200)
trail:draw()
Fps=1/DeltaTime
text(math.ceil(Fps),150,50)
end
--# Trail
Trail = class()
function Trail:init(startsize,endsize,length,body,tex)
-- you can accept and set parameters here
self.body = body
self.tsegs = {}
self.count = math.ceil(length/4)
print(self.count)
self.m = mesh()
for i=1,self.count do
self.tsegs[i] = {}
self.tsegs[i].r = self.m:addRect(0,0,1,1)
self.tsegs[i].p = body.position
self.tsegs[i].a = 0
self.tsegs[i].c = i
end
self.m.texture = img
self.id = 0
self.vlen = self.count*32
self.td = 0
end
function Trail:draw()
local b = self.body
self.td = self.td + 1
b.p = b.position
local d = self.tsegs[self.id]
if self.id<self.count then
self.id = self.id + 1
else
self.id = 1
end
if self.id>=2 then
if self.id-2==0 then d = self.tsegs[self.count] else d = self.tsegs[self.id-2] end
else
if self.id == 1 then
d = self.tsegs[self.count-1]
end
if self.id == 0 then
d = self.tsegs[self.count-2]
end
end
local v
v = self.tsegs[self.id]
v.p = b.position
self.vlen = 0
local av = (b.p-d.p)
v.a = math.atan2(av.y,av.x)
self.m:setRect(v.r,d.p.x-av.x,d.p.y-av.y,av:len(),20,v.a)
self.m:draw()
end
function Trail:touched(touch)
-- Codea does not automatically call this method
end
I’ve been trying to debug it in my spare time but it’s just not working, any help is much appreciated.