Why doesn't the polygon line up with the physics composite?

I am trying to create a segment of curve and extract from the path used to make the polygon a series of composite body shapes for the addBody call. After adjusting the path so that the polygon centre is the centre of the path, the polygon and the physics body don’t line up.

require("physics") physics.start() physics.setGravity(0,0) physics.setDrawMode("hybrid") -- rotates point around the centre by degrees -- rounds the returned coordinates using math.round() if round == true -- returns new coordinates object local function rotateAboutPoint( point, degrees, centre ) local pt = { x=point.x - centre.x, y=point.y - centre.y } pt = math.rotateTo( pt, degrees ) pt.x, pt.y = pt.x + centre.x, pt.y + centre.y return pt end math.rotateAboutPoint = rotateAboutPoint -- rotates a point around the (0,0) point by degrees -- returns new point object -- center: optional local function rotateTo( point, degrees, center ) if (center ~= nil) then return rotateAboutPoint( point, degrees, center ) else local x, y = point.x, point.y local theta = math.rad( degrees ) local pt = { x = x \* math.cos(theta) - y \* math.sin(theta), y = x \* math.sin(theta) + y \* math.cos(theta) } return pt end end math.rotateTo = rotateTo local function createCurvedBlockShapesAndPolygonPath( innerY, outerY, radius ) local shape, innerPoly, outerPoly = {}, {}, {} local innerPt, outerPt = {x=0,y=innerY}, {x=0,y=outerY} local centre = {x=0,y=0} local endIndex = (radius+1) \* 2 \* 2 local minX, maxX, minY, maxY = 100000000, -100000000, 100000000, -100000000 local function appendPoint( tbl, pt ) tbl[#tbl+1] = pt.x tbl[#tbl+1] = pt.y end local function checkBounds( x, y ) if (x \< minX) then minX = x elseif (x \> maxX) then maxX = x end if (y \< minY) then minY = y elseif (y \> maxY) then maxY = y end end local function generatePoints() appendPoint( innerPoly, innerPt ) appendPoint( outerPoly, outerPt ) for i=1, radius do innerPt = math.rotateTo( innerPt, 1, centre ) outerPt = math.rotateTo( outerPt, 1, centre ) appendPoint( innerPoly, innerPt ) appendPoint( outerPoly, outerPt ) checkBounds( innerPt.x, innerPt.y ) checkBounds( outerPt.x, outerPt.y ) end end generatePoints() local function adjustPoints( pts, x, y ) for i=1, #pts-1, 2 do pts[i] = pts[i] - x pts[i+1] = pts[i+1] - y end end local cx, cy = minX+(maxX-minX)/2, minY+(maxY-minY)/2 adjustPoints( innerPoly, cx, cy ) adjustPoints( outerPoly, cx, cy ) local function extractShape( inner, outer, i ) local partial = {} partial[#partial+1] = outer[i-1] partial[#partial+1] = outer[i] partial[#partial+1] = outer[i-3] partial[#partial+1] = outer[i-2] partial[#partial+1] = inner[i-3] partial[#partial+1] = inner[i-2] partial[#partial+1] = inner[i-1] partial[#partial+1] = inner[i] shape[#shape+1] = { shape=partial } end local function extractShapes() for i=4, #innerPoly, 2 do extractShape( innerPoly, outerPoly, i ) end end extractShapes() local function join( inner, outer ) local tbl = {} for i=1, #inner do tbl[#tbl+1] = inner[i] end for i=#outer-1, 1, -2 do tbl[#tbl+1] = outer[i] tbl[#tbl+1] = outer[i+1] end return tbl end local poly = join( innerPoly, outerPoly ) return shape, poly end local shape, poly = createCurvedBlockShapesAndPolygonPath( 150, 200, 90 ) local p = display.newPolygon( 210,210 , poly ) physics.addBody( p, "dynamic", unpack(shape) )

The math.* functions (included) were taken from my MathLib library.

Worked it out myself… This function needs correcting to this:

 local function generatePoints() appendPoint( innerPoly, innerPt ) appendPoint( outerPoly, outerPt ) checkBounds( innerPt.x, innerPt.y ) -- ADDED checkBounds( outerPt.x, outerPt.y ) -- ADDED for i=1, radius do innerPt = math.rotateTo( innerPt, 1, centre ) outerPt = math.rotateTo( outerPt, 1, centre ) appendPoint( innerPoly, innerPt ) appendPoint( outerPoly, outerPt ) checkBounds( innerPt.x, innerPt.y ) checkBounds( outerPt.x, outerPt.y ) end end generatePoints()

Worked it out myself… This function needs correcting to this:

 local function generatePoints() appendPoint( innerPoly, innerPt ) appendPoint( outerPoly, outerPt ) checkBounds( innerPt.x, innerPt.y ) -- ADDED checkBounds( outerPt.x, outerPt.y ) -- ADDED for i=1, radius do innerPt = math.rotateTo( innerPt, 1, centre ) outerPt = math.rotateTo( outerPt, 1, centre ) appendPoint( innerPoly, innerPt ) appendPoint( outerPoly, outerPt ) checkBounds( innerPt.x, innerPt.y ) checkBounds( outerPt.x, outerPt.y ) end end generatePoints()