Bug in physics.addBody with display.newLine( )

Hi,
if I write

local BordoDW = display.newLine(0,200,400,200)  
BordoDW:append(410,200,600,200)  
BordoDW:setColor( 255, 102, 102, 255 )  
BordoDW.width = 3   
physics.addBody( BordoDW, "static", { friction=0.5, bounce=0.3 } )  

there are some strange behaviors in physical.
Please use

physics.setDrawMode( "hybrid" )  

to see.

Thanks
Marco
[import]uid: 7518 topic_id: 1912 reply_id: 301912[/import]

Hi, Marco. By default, physics.addBody() creates a rectangular body that matches the rectangular area of your texture or bitmap.

This is convenient in many cases, but if you don’t want a big rectangle for the body, you need to specify either a circular radius or a set of (x,y) coordinates for the actual collision boundary.

See here: http://developer.anscamobile.com/content/game-edition-physics-bodies#Polygon_bodies

A newLine is actually a rectangular texture that has a lot of transparency and some vector rendering on it, so if you want a body that more closely tracks the shape of the line itself, you’ll need to specify it. But you can probably re-use the values of the points you’re drawing the line with (or at least nearby values to those points). [import]uid: 3007 topic_id: 1912 reply_id: 5640[/import]

Thank you Evank for explanation!

Marco
[import]uid: 7518 topic_id: 1912 reply_id: 5649[/import]

I’ve added a bit more explanation in the documentation:

http://developer.anscamobile.com/content/game-edition-physics-bodies#Default_rectangular_bodies [import]uid: 3007 topic_id: 1912 reply_id: 5877[/import]

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]