adding physics to straight drawn lines

Hi, I’m pretty new to Corona, and am having trouble with adding physics to lines being drawn by the user, I’m basing the line generation off of this code

[lua] function drawLine( event )
if(event.phase == “ended”) then
line = display.newLine(event.xStart, event.yStart, event.x, event.y)
line:setColor(255,0,0)
line.width = 5
end
end
Runtime:addEventListener(“touch”, drawLine) [/lua]

so all of my lines will be different lengths, directions,rotations and whatnot, my question is how do I set up a physics shape around the line that wont include extra space? I’ve tried setting up my own shape for it but i can’t seem to get it right, or even close to right [import]uid: 80180 topic_id: 13856 reply_id: 313856[/import]

http://developer.anscamobile.com/code/draw-line

I saw this in the code exchange. Never tried it but the description seems to fit what you are looking for. [import]uid: 31262 topic_id: 13856 reply_id: 50973[/import]

Yeah i have seen that code before, and at first it seemed like a very indirect way of giving the line physics, but the more i look at it the more i think it might be the answer. Thanks! [import]uid: 80180 topic_id: 13856 reply_id: 51044[/import]

Unfortunately, what your looking for is very difficult. I have been trying to find another way too. [import]uid: 23689 topic_id: 13856 reply_id: 53601[/import]

[lua]local lines = {}
local myLines = {}
local prevX,prevY

require ( “physics” )
physics.start()
physics.setGravity( 0, 7.5 )

local function removeLine(myLine)
for i=1,#myLine do
myLine[i]:removeSelf()
myLine[i] = nil
end
end

local i = 1
local function drawLine(e)
if “began” == e.phase then
myLines[i] = {}
prevX = e.x
prevY = e.y
elseif “moved” == e.phase then
if prevX then
myLines[i][#myLines[i] + 1] = display.newLine(prevX,prevY,e.x,e.y)
myLines[i][#myLines[i]].width = 3
physics.addBody(myLines[i][#myLines[i]], “static”, {density = 1.0, friction = 0.3, bounce = 0.2})
end
prevX = e.x
prevY = e.y
elseif “ended” == e.phase then
prevX = nil
prevY = nil
i = i + 1
–you can get to individual lines using this.
removeLine(myLines[#myLines-1])
end
end
Runtime:addEventListener(“touch”,drawLine)[/lua]

different line code, but that line is better :slight_smile:

you really should use google before posting… [import]uid: 79135 topic_id: 13856 reply_id: 53634[/import]