physics.addBody on a display.newLine

I’m trying to apply physics attributes to a line. As far as I can tell this is not possible so I then moved on to trying to generate a triangle shape. I’m getting a crash when I try this route. To draw the line it captures the x,y of the first touch point and if you drag it makes the end of the line wherever you release. Here’s my current code that does not work:

[code]
local bkg = display.newImage( “paper_bkg.png”, true )
bkg.x = display.contentWidth/2
bkg.y = display.contentHeight/2

local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 ) – no gravity in any direction

local function spawnLine( event )
local phase = event.phase

if “ended” == phase then

local line = display.newLine( event.xStart,event.yStart, event.x,event.y )
local thirdLegX = event.xStart - 1
local thirdLegY = event.yStart - 1

triangleShape = { event.xStart,event.yStart, event.x,event.y, thirdLegX, thirdLegY }
physics.addBody( line, { friction=0.6, shape=triangleShape } )
line:setColor( 255, 102, 102, 255 )
line.width = 2

end
end

bkg:addEventListener( “touch”, spawnLine )

physics.setDrawMode( “hybrid” )
[/code] [import]uid: 13868 topic_id: 5287 reply_id: 305287[/import]

Hi, couple things:

  1. In your triangleShape table, you need to offset all of your x values by the value of event.xStart and your y values by event.yStart, to make your physics body appear directly over the line:

[lua]triangleShape = { 0,0, event.x-event.xStart,event.y-event.yStart, thirdLegX-event.xStart, thirdLegY- event.yStart}[/lua]

  1. Once I did that, I could draw as many lines as I wanted as long as I was drawing them in a downward motion. As soon as I tried to draw a line in an upward motion (i.e. where event.yStart is greater than event.y), I’d get a crash. So my guess is that at this point the physics engine can’t cope with the type of shape you’re trying to draw. You can fix this by reversing the first two sets of coordinates in triangleShape:

[lua]triangleShape = { event.x-event.xStart,event.y-event.yStart, 0,0, thirdLegX-event.xStart, thirdLegY- event.yStart}[/lua]

Now it’s only crashing if you draw a line where x changes a lot by y doesn’t, so my guess is that something similar is happening in those situations as well. So you’ll need to figure out an extra condition to check for in those situations (it’s only happening when event.xStart is significantly less than event.x).

Hope this helps.

Darren [import]uid: 1294 topic_id: 5287 reply_id: 17826[/import]