Converting thick line into rectangular body

Sorry if this has been asked before (I’ve been absent…), but I have a little code which lets me draw a straight line, with width=25.

I’d like to convert what looks like a rectangle being stretched out into a rectangular physics body.

Has anyone done this?

Of course, I’m working on the problem, so once I get something usable, I’ll post it…

Thanks,

Matt. [import]uid: 8271 topic_id: 14855 reply_id: 314855[/import]

There i a nice code from gerald…
maybe this will help you…
http://developer.anscamobile.com/code/gerald-anderson [import]uid: 9592 topic_id: 14855 reply_id: 54858[/import]

Thanks @AlenB, here’s my finished code to draw a 20px width line and convert it into a solid object…

[lua]require (“physics”)
local ui = require(“ui”)

physics.start()
physics.setDrawMode ( “hybrid” ) – Uncomment in order to show in hybrid mode
physics.setGravity( 0, 9.8 * 2)

physics.start()

local bats = {}

function main()
display.setStatusBar( display.HiddenStatusBar )
Runtime:addEventListener(“touch”, drawBat)
end

local function angleBetween ( srcObj, dstObj )
local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y
local angleBetween = math.deg( math.atan( yDist/xDist ) )
if (srcObj.x < dstObj.x) then
angleBetween = angleBetween+90
else
angleBetween = angleBetween-90
end
return angleBetween
end

local function distanceBetween( point1, point2 )
local xfactor = point2.x-point1.x ; local yfactor = point2.y-point1.y
local distanceBetween = math.sqrt((xfactor*xfactor) + (yfactor*yfactor))
return distanceBetween
end

local function createBatsBody( line, ending, type, props )
local length = distanceBetween( line, ending )
local angle = angleBetween( line, ending ) + 90

local rect = display.newRect( 0, 0, length, line.width )
rect.rotation = angle
rect.x, rect.y = line.x + (ending.x-line.x)/2, line.y + (ending.y-line.y)/2

props.shape = shape

physics.addBody( rect, type, props )
line:removeSelf()
return rect
end

local delayRemoveLine = function( event )
event:removeSelf()
end

function drawBat( event )
if (event.phase == “began”) then
local bat = display.newLine( event.x, event.y, event.x, event.y )
bat.width = 20
table.insert( bats, 1, bat )
elseif (event.phase == “moved”) then
local bat = display.newLine( bats[1].x, bats[1].y, event.x, event.y )
bat.width = 20
bats[1]:removeSelf()
bats[1] = bat
else
if (#bats == 1) then
bats[1] = createBatsBody( bats[1], event, “static”, {} )
else
bats[1] = createBatsBody( bats[1], event, “dynamic”, {} )
end

bats[1].timer = delayRemoveLine
timer.performWithDelay( 5000, bats[1] )
end
end

main()[/lua] [import]uid: 8271 topic_id: 14855 reply_id: 54884[/import]

Hi horacebury,

thx for posting your code…

I am trying to convert the free drawn line into dynamic body…
but this will not work because the line fall into small parts…

What do you think would be the best approach to create a dynamic curved line…

Thx

[import]uid: 9592 topic_id: 14855 reply_id: 56716[/import]

Take the code above and use it to convert each straight line piece of your curved line into a physics object. That’s how I’ve done it. I won’t post my solution, because I don’t have it here, but also because you can probably come up with a better implementation - mine is rubbish, right now… [import]uid: 8271 topic_id: 14855 reply_id: 56909[/import]