Adding physics body to a line

Hello,

Does anyone know how i can add a physic body to this line? I want to draw the line and correspond to object hitting it, making it “static”. Any help? Right now it is just a normal line that is drawn and is erased after releasing every other one. All i need is to add a physic body to it. I want to make the whole line static. Can someone please help, i really need this. Thank you SO MUCH .Help is very much APPRECIATED.

[code]

local lines = {}
local myLines = {}
local prevX,prevY

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
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) [import]uid: 66985 topic_id: 14381 reply_id: 314381[/import]

How you would add physics to the lines begin created is like so (your code, slightly modified);

[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]], “dynamic”, {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]

However I don’t know if this is the result you’re after - it looks cool but because you’re making heaps of little lines, naturally it doesn’t look like one line with physics, of course.

Also, you’ve got an error you might want to look at as soon as you draw the first line :wink:

Peach [import]uid: 52491 topic_id: 14381 reply_id: 53182[/import]

Thank you so much PEACH!!! I REALLY REALLY APPRECIATE IT! I really owe you a HUGE THANK YOU! Theres is only 1 problem that i’m seeing, do you know how we can change the size of the static/dynamic bodies that appear? To make them fit the line ? Thanks SO MUCH! [import]uid: 66985 topic_id: 14381 reply_id: 53230[/import]

Haha, I didn’t know how much you’d like the initial example as there’s so many little pieces, but I’m glad you do :slight_smile:

I’m not totally sure on your question; I could read it a few ways.

However, try changing [lua]“dynamic”[/lua] to [lua]“static”[/lua] and see what that does :wink:

Peach [import]uid: 52491 topic_id: 14381 reply_id: 53336[/import]