With the help of the vector2D class from:
git://gist.github.com/1006414.git gist-1006414
Assuming the newLine you need to add physics to is only two points,
the function newLineWithShape below will also spit out a shape that fits:
[code]
local vector2d = require(“vector2d”)
local physics = require(“physics”)
display.setStatusBar( display.HiddenStatusBar )
physics.start()
physics.setGravity(0,0)
physics.setDrawMode(“hybrid”)
local usualLineRed = display.newLine(50,50,300,200)
usualLineRed:setColor( 255, 25, 25, 255 )
usualLineRed.width = 3
physics.addBody( usualLineRed, “static”, { friction=0.5, bounce=0.3 } )
local usualLineBlu = display.newLine(367,358,12,123)
usualLineBlu:setColor( 25, 25, 255, 255 )
usualLineBlu.width = 15
physics.addBody( usualLineBlu, “static”, { friction=0.5, bounce=0.3 } )
– Creates and returns both a line and a shape around it.
function newLineWithShape( x1,y1,x2,y2, width)
local line = display.newLine(x1,y1,x2,y2)
line.width = width
local lineShape = {}
local A = vector2D:new(x1,y1)
local B = vector2D:new(x2,y2)
local AtoB = vector2D:Sub(B,A)
local perpAtoB = AtoB:perpendicular()
perpAtoB:normalize()
local posHalfWidth = vector2d:Mult(perpAtoB, width/2)
local negHalfWidth = vector2d:Mult(perpAtoB, width/-2)
local A1 = posHalfWidth
local A2 = negHalfWidth
local B1 = vector2D:Add(A1,AtoB)
local B2 = vector2D:Add(A2,AtoB)
lineShape = { A1.x,A1.y, A2.x,A2.y, B2.x,B2.y, B1.x,B1.y }
return line,lineShape
end
fixedLine, fixedLineShape = newLineWithShape(75,75,400,100, 25)
physics.addBody( fixedLine, “static”, { friction=0.5, bounce=0.3, shape = fixedLineShape } )
fixedLine2, fixedLineShape2 = newLineWithShape(25,399,400,100, 55)
physics.addBody( fixedLine2, “static”, { friction=0.5, bounce=0.3, shape = fixedLineShape2 } )
[code]
[import]uid: 125741 topic_id: 1912 reply_id: 118761[/import]