Display.newPolygon path

I’ve got a whole group of polygons created using the display.newPolygon, and I want to look at their vertices to see which are touching. Is it possible to read the vertexes from the polygon’s path object and if so what format does it take, or do I need to append the vertex information to each polygon manually? 

I think you’ll have to keep that data in your object yourself.  I just tested didn’t see any public ally visible data on a polygon object.

You could do the following:

  1. Add this code to your library somewhere.

    – From: https://gist.github.com/HoraceBury/9431861 – converts a table of {x,y,x,y,…} to points {x,y} local function tableToPoints( tbl ) local pts = {} for i=1, #tbl-1, 2 do pts[#pts+1] = { x=tbl[i], y=tbl[i+1] } end return pts end – ensures that a list of coordinates is converted to a table of {x,y} points – returns a table of {x,y} points and the number of points, whether a display group or not local function ensurePointsTable( tbl ) if (type(tbl[1]) == “number”) then – list contains {x,y,x,y,…} coordinates - convert to table of {x,y} tbl = tableToPoints( tbl ) return tbl, #tbl else – table is already in {x,y} point format… – check for display group local count = tbl.numChildren if (count == nil) then count = #tbl end return tbl, count end end local function getBoundingCentroid( pts ) pts = ensurePointsTable( pts ) – EFM change a little local xMin, xMax, yMin, yMax = 100000000, -100000000, 100000000, -100000000 for i=1, #pts do local pt = pts[i] if (pt.x < xMin) then xMin = pt.x end if (pt.x > xMax) then xMax = pt.x end if (pt.y < yMin) then yMin = pt.y end if (pt.y > yMax) then yMax = pt.y end end local width, height = xMax-xMin, yMax-yMin local cx, cy = xMin+(width/2), yMin+(height/2) local output = { centroid = { x=cx, y=cy }, width = width, height = height, bounding = { xMin=xMin, xMax=xMax, yMin=yMin, yMax=yMax }, } return output end – Now my code: local function calculateVertexPosition( self, num )    local vertX = self._vertices[(num-1)*2+1]    local vertY = self._vertices[(num) * 2]    return { x = vertX + self.x - self._boundingCentroid.centroid.x, y = vertY + self.y - self._boundingCentroid.centroid.x } end

  2. Now try this:

    local halfW = display.contentWidth * 0.5 local halfH = display.contentHeight * 0.5 local vertices = { 0, 0, 90, 0, 45, 90 } local o = display.newPolygon( halfW, halfH, vertices ) o._vertices = vertices – Now you have the relative position of each vertex. o._boundingCentroid = getBoundingCentroid( vertices ) o.cvp = calculateVertexPosition local v = o:cvp( 1 ) local tmp = display.newCircle( v.x, v.y, 10 ) tmp:setFillColor(1,0,0) local v = o:cvp( 2 ) local tmp = display.newCircle( v.x, v.y, 10 ) tmp:setFillColor(0,1,0) local v = o:cvp( 3 ) local tmp = display.newCircle( v.x, v.y, 10 ) tmp:setFillColor(0,0,1)

Please note.  The above ‘solution’  Does not handle rotated objects.  You’ll need to understand and do some 2D calculations for that.

This is the result of running my code from above.

https://www.youtube.com/watch?v=8qUPspALWAY&feature=youtu.be&hd=1

Hi roaminggamer thanks for that! That’s some very useful code. And thanks for taking the time to produce such a comprehensive example!

You’re welcome and thanks to Horace (from me) for making his ‘centroid’ code available.  

I think you’ll have to keep that data in your object yourself.  I just tested didn’t see any public ally visible data on a polygon object.

You could do the following:

  1. Add this code to your library somewhere.

    – From: https://gist.github.com/HoraceBury/9431861 – converts a table of {x,y,x,y,…} to points {x,y} local function tableToPoints( tbl ) local pts = {} for i=1, #tbl-1, 2 do pts[#pts+1] = { x=tbl[i], y=tbl[i+1] } end return pts end – ensures that a list of coordinates is converted to a table of {x,y} points – returns a table of {x,y} points and the number of points, whether a display group or not local function ensurePointsTable( tbl ) if (type(tbl[1]) == “number”) then – list contains {x,y,x,y,…} coordinates - convert to table of {x,y} tbl = tableToPoints( tbl ) return tbl, #tbl else – table is already in {x,y} point format… – check for display group local count = tbl.numChildren if (count == nil) then count = #tbl end return tbl, count end end local function getBoundingCentroid( pts ) pts = ensurePointsTable( pts ) – EFM change a little local xMin, xMax, yMin, yMax = 100000000, -100000000, 100000000, -100000000 for i=1, #pts do local pt = pts[i] if (pt.x < xMin) then xMin = pt.x end if (pt.x > xMax) then xMax = pt.x end if (pt.y < yMin) then yMin = pt.y end if (pt.y > yMax) then yMax = pt.y end end local width, height = xMax-xMin, yMax-yMin local cx, cy = xMin+(width/2), yMin+(height/2) local output = { centroid = { x=cx, y=cy }, width = width, height = height, bounding = { xMin=xMin, xMax=xMax, yMin=yMin, yMax=yMax }, } return output end – Now my code: local function calculateVertexPosition( self, num )    local vertX = self._vertices[(num-1)*2+1]    local vertY = self._vertices[(num) * 2]    return { x = vertX + self.x - self._boundingCentroid.centroid.x, y = vertY + self.y - self._boundingCentroid.centroid.x } end

  2. Now try this:

    local halfW = display.contentWidth * 0.5 local halfH = display.contentHeight * 0.5 local vertices = { 0, 0, 90, 0, 45, 90 } local o = display.newPolygon( halfW, halfH, vertices ) o._vertices = vertices – Now you have the relative position of each vertex. o._boundingCentroid = getBoundingCentroid( vertices ) o.cvp = calculateVertexPosition local v = o:cvp( 1 ) local tmp = display.newCircle( v.x, v.y, 10 ) tmp:setFillColor(1,0,0) local v = o:cvp( 2 ) local tmp = display.newCircle( v.x, v.y, 10 ) tmp:setFillColor(0,1,0) local v = o:cvp( 3 ) local tmp = display.newCircle( v.x, v.y, 10 ) tmp:setFillColor(0,0,1)

Please note.  The above ‘solution’  Does not handle rotated objects.  You’ll need to understand and do some 2D calculations for that.

This is the result of running my code from above.

https://www.youtube.com/watch?v=8qUPspALWAY&feature=youtu.be&hd=1

Hi roaminggamer thanks for that! That’s some very useful code. And thanks for taking the time to produce such a comprehensive example!

You’re welcome and thanks to Horace (from me) for making his ‘centroid’ code available.  

Teeny tiny error that had me investigating why the dots were not properly placed - the second last line should be

[lua]

y = vertY + self.y - self._boundingCentroid.centroid.y } – not x but y

[/lua]

Teeny tiny error that had me investigating why the dots were not properly placed - the second last line should be

[lua]

y = vertY + self.y - self._boundingCentroid.centroid.y } – not x but y

[/lua]