Drawing a line that becomes static

Im trying to draw a line that I can make static to react with objects. What I found online works great at drawing the line, but if the line is drawn to fast the physics body of the line goes way off. This is the code.

[lua]-- Draw a line
function runTouch(event)
if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)
tempLine:setColor(0,0,0)
–tempLine:setColor(math.random(255), math.random(255), math.random(255))
tempLine.name = ‘tempLine’
group:insert(tempLine)
prevX = event.x
prevY = event.y
–timer.cancel(timer1)

end
elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+.9
ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
–ractgangle_hit[i]:setColor(math.random(255), math.random(255), math.random(255))
ractgangle_hit[i]:setColor(0,0,0)
ractgangle_hit[i].width = 3
ractgangle_hit[i].name = ‘ractgangle_hit[i]’
group:insert(ractgangle_hit[i])

– Creates physic joints for line (Turn on draw mode to see the effects)
local Width = ractgangle_hit[i].width * 0.6
local Height = ractgangle_hit[i].height * 0.2

– Physic body for the line shape
lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}
lineShape.name = ‘lineShape’
physics.addBody(ractgangle_hit[i], “static”, { bounce = 0.1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1
elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
–Runtime:removeEventListener(“touch”, runTouch)
end
end
Runtime:addEventListener(“touch”, runTouch)[/lua]

If any one can point me to something better it would be much appreciated! [import]uid: 75779 topic_id: 30857 reply_id: 330857[/import]

Plug and play, please? :slight_smile: [import]uid: 52491 topic_id: 30857 reply_id: 123476[/import]

Okay here is plug and play. I turned physics hybrid on so you can see the physics of the line when you do a quick swipe. It will not allow for colisions with small objects. Ignore the ball
[lua]–(Copy paste friendly)

– Try swiping mouse over simulator to produce random colored lines

– Starts physics

local physics = require (“physics”)
physics.start(true)
physics.setGravity(10, 0)
physics.setDrawMode(‘hybrid’)

– Ball rolls on Line

local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(0, 255, 0)
ball.x = display.contentHeight/12
ball.y = display.contentWidth/4

physics.addBody(ball, {bounce=0.3, radius = 25, friction=0.5})

– Draw a line

local i = 1
local tempLine
local ractgangle_hit = {}
local prevX , prevY
local function runTouch(event)
if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)

– Random Colors for line

r = math.random (0, 255)
g = math.random ( 0, 255)
b = math.random (0, 255 )
tempLine:setColor(r,g, b)

prevX = event.x
prevY = event.y
end
elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+0.9
ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
ractgangle_hit[i]:setColor(r,g, b)
ractgangle_hit[i].width = 5

– Creates physic joints for line (Turn on draw mode to see the effects)

local Width = ractgangle_hit[i].width * 0.6
local Height = ractgangle_hit[i].height * 0.2

– Physic body for the line shape

local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}

physics.addBody(ractgangle_hit[i], “static”, { bounce = -1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1
elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
end
end
Runtime:addEventListener(“touch”, runTouch) [import]uid: 75779 topic_id: 30857 reply_id: 123536[/import]

Plug and play, please? :slight_smile: [import]uid: 52491 topic_id: 30857 reply_id: 123476[/import]

Okay here is plug and play. I turned physics hybrid on so you can see the physics of the line when you do a quick swipe. It will not allow for colisions with small objects. Ignore the ball
[lua]–(Copy paste friendly)

– Try swiping mouse over simulator to produce random colored lines

– Starts physics

local physics = require (“physics”)
physics.start(true)
physics.setGravity(10, 0)
physics.setDrawMode(‘hybrid’)

– Ball rolls on Line

local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(0, 255, 0)
ball.x = display.contentHeight/12
ball.y = display.contentWidth/4

physics.addBody(ball, {bounce=0.3, radius = 25, friction=0.5})

– Draw a line

local i = 1
local tempLine
local ractgangle_hit = {}
local prevX , prevY
local function runTouch(event)
if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)

– Random Colors for line

r = math.random (0, 255)
g = math.random ( 0, 255)
b = math.random (0, 255 )
tempLine:setColor(r,g, b)

prevX = event.x
prevY = event.y
end
elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+0.9
ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
ractgangle_hit[i]:setColor(r,g, b)
ractgangle_hit[i].width = 5

– Creates physic joints for line (Turn on draw mode to see the effects)

local Width = ractgangle_hit[i].width * 0.6
local Height = ractgangle_hit[i].height * 0.2

– Physic body for the line shape

local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}

physics.addBody(ractgangle_hit[i], “static”, { bounce = -1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1
elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
end
end
Runtime:addEventListener(“touch”, runTouch) [import]uid: 75779 topic_id: 30857 reply_id: 123536[/import]