Drawing arcs around a point

I’m trying to draw a filled-in arc from a point represented by a ship’s x,y location. I have it working using some code posted previously to the forums in one case, when the angle is 0 and the anchor points are anchorX = 0, anchorY = 0.5

But when the arc(polygon) has to be rotated to match the rotation of the ship, it becomes misaligned.

So presumably I have to alter the anchor points in relation to the ship rotation to ensure that the arc originates at the proper point. I suspect it’s anchorX = math.cos(?) * ? and anchorY = math.sin(?) * ? but the answer isn’t obvious to me. Anyone have any suggestions? Thanks.

Here’s the function code:

function ship.drawArc(startAngle, widthAngle, radius ) startAngle = startAngle or 0 widthAngle = widthAngle or 90 radius = radius or 100 local vert = {} vert[#vert+1] = 0 vert[#vert+1] = 0 for i = 0, widthAngle do local a = (startAngle+i)\*math.pi/180 vert[#vert+1] = radius \* math.cos(a) vert[#vert+1] = radius \* math.sin(a) end local arc = display.newPolygon(0, 0, vert) arc:setFillColor(1,1,1) arc.strokeWidth = 1 arc:setStrokeColor( 1, 1, 1 ) arc.rotation = ship.rotation arc.anchorX, arc.anchorY = 0, .5 -- correct when ship faces 0 degrees return arc end

Well, I managed to resolve the issue. I’m not sure what the problem was but making the polygon arc a fixed size and rotating it worked properly. Putting the arc in a display group associated with the ship object may have helped. In case this helps anyone else…

function ship.drawArc(startAngle, widthAngle, radius ) startAngle = startAngle or 0 widthAngle = widthAngle or 90 radius = radius or 100 local vert = {} vert[#vert+1] = 0 vert[#vert+1] = 0 for i = 0, widthAngle do local a = (startAngle+i)\*math.pi/180 vert[#vert+1] = radius \* math.cos(a) vert[#vert+1] = radius \* math.sin(a) end local arc = display.newPolygon(0, 0, vert) arc:setFillColor( .2, .2, .2, .5 ) arc.strokeWidth = 1 arc:setStrokeColor( .2, .2, .2 ) arc.anchorX, arc.anchorY = 0, .5 return arc end local a = ship.x local b = ship.y local r =ship.getRotation() local startAngle = 338 local widthAngle = 44 local radius = 100 ship.wedgeGrp = ship.drawArc(startAngle, widthAngle, radius) ship.wedgeGrp.rotation = r ship.wedgeGrp:translate(a, b)

One way is to adjust the anchor points to always be at the point you want to rotate around.

The easier way is to render your polygon then place it inside a group (on it’s own) and make sure the point to rotate around is at 0,0. Then rotating the group will always rotate the poly around that point.

Well, I managed to resolve the issue. I’m not sure what the problem was but making the polygon arc a fixed size and rotating it worked properly. Putting the arc in a display group associated with the ship object may have helped. In case this helps anyone else…

function ship.drawArc(startAngle, widthAngle, radius ) startAngle = startAngle or 0 widthAngle = widthAngle or 90 radius = radius or 100 local vert = {} vert[#vert+1] = 0 vert[#vert+1] = 0 for i = 0, widthAngle do local a = (startAngle+i)\*math.pi/180 vert[#vert+1] = radius \* math.cos(a) vert[#vert+1] = radius \* math.sin(a) end local arc = display.newPolygon(0, 0, vert) arc:setFillColor( .2, .2, .2, .5 ) arc.strokeWidth = 1 arc:setStrokeColor( .2, .2, .2 ) arc.anchorX, arc.anchorY = 0, .5 return arc end local a = ship.x local b = ship.y local r =ship.getRotation() local startAngle = 338 local widthAngle = 44 local radius = 100 ship.wedgeGrp = ship.drawArc(startAngle, widthAngle, radius) ship.wedgeGrp.rotation = r ship.wedgeGrp:translate(a, b)

One way is to adjust the anchor points to always be at the point you want to rotate around.

The easier way is to render your polygon then place it inside a group (on it’s own) and make sure the point to rotate around is at 0,0. Then rotating the group will always rotate the poly around that point.