why doesn't this physics polygon body line up with the polyline display object?

I’m trying to create an octagonal shape, starting with its upper left corner of its bounding rectangle at 120, 120, with a corresponding physics body. The polyline and physics body appear to be the right shape, but not aligned on the same space. Why?

The octagon is bounded by a 50x50 rectangle, with 30px sides.

The docs say that the coords of the physics shape are relative to the display object, which has its origin at the center of the display object by default. But in this code, it looks like the physics object is using the first point of the line as its center, and the poly line isn’t where I’d expect it either.

What am I doing wrong?

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

physics.start()

display.setStatusBar (display.HiddenStatusBar)

physics.setDrawMode( “hybrid” )

square1 = display.newRect( 60, 180, 60, 60)
physics.addBody( square1, “static”, { density=3.0, friction=0.5, bounce=0.2 } )
square2 = display.newRect( 120, 180, 60, 60)
physics.addBody( square2, “static”, { density=3.0, friction=0.5, bounce=0.2 } )
square3 = display.newRect( 180, 180, 60, 60)
physics.addBody( square3, “static”, { density=3.0, friction=0.5, bounce=0.2 } )

xStart = 120
yStart = 120
local octaShape = display.newLine( xStart+10, yStart, xStart+40, yStart )
octaShape:append( xStart+50, yStart+10, xStart+50, yStart+40, xStart+40, yStart+50, xStart+10, yStart+50, xStart, yStart+40, xStart, yStart+10, xStart+10, yStart )
octaShape:setColor( 255, 102, 102, 255 )
octaShape.width = 3

octaPhysicsBody = { -15, -25, 15, -25, 25, -15, 25, 15, 15, 25, -15, 25, -25, 15, -25, -15 }
physics.addBody( octaShape, { density=1.5, friction=0.5, bounce=0.2, shape=octaPhysicsBody } )[/lua] [import]uid: 82378 topic_id: 20519 reply_id: 320519[/import]

Replace line 23 with this;
[lua]octaPhysicsBody = {0,0, 30,0, 40,10, 40,40, 30,50, 0,50, -10,40, -10,10 }[/lua]

Peach :wink: [import]uid: 52491 topic_id: 20519 reply_id: 80554[/import]

Thanks Peach for the reply. I did the same thing, just moved the physics body coords around until they matched. But I was looking more for the ‘why’ behind how it seemed to place them different than the docs said.

How is the x and y of a poly line object determined? Or can you not know this ahead of time, and it’s just trial and error? [import]uid: 82378 topic_id: 20519 reply_id: 80623[/import]

I believe that 0,0 is the top left of the shape in all situations like this, so it’s where you’d start then draw points clockwise.

So in this case 0,0 was the top left of your new shape, “octaShape”.

That said I have limited experience with custom physics bodies so I can’t be 100% certain on this but if you get further headaches with it please let me know and I’ll endeavor to track down some more solid info for you.

Peach :slight_smile: [import]uid: 52491 topic_id: 20519 reply_id: 80776[/import]