libgdx drawing arc curve

后端 未结 4 1683
南旧
南旧 2020-12-19 23:01

The arc function of libgdx instead of drawing a arc draws a pie segment (ie. has 2 lines connecting to the arc\'s origin)

shapeRenderer.begin(ShapeType.Line)         


        
4条回答
  •  温柔的废话
    2020-12-19 23:30

    In the end I sub classed the ShapeRenderer Class

    public class Arc extends ShapeRenderer{
    
        private final ImmediateModeRenderer renderer;
        private final Color color = new Color(1, 1, 1, 1);
    
        public Arc(){
            renderer = super.getRenderer();
        }
    
        /** Draws an arc using {@link ShapeType#Line} or {@link ShapeType#Filled}. */
        public void arc (float x, float y, float radius, float start, float degrees) {
        int segments = (int)(6 * (float)Math.cbrt(radius) * (degrees / 360.0f));
    
        if (segments <= 0) throw new IllegalArgumentException("segments must be > 0.");
        float colorBits = color.toFloatBits();
        float theta = (2 * MathUtils.PI * (degrees / 360.0f)) / segments;
        float cos = MathUtils.cos(theta);
        float sin = MathUtils.sin(theta);
        float cx = radius * MathUtils.cos(start * MathUtils.degreesToRadians);
        float cy = radius * MathUtils.sin(start * MathUtils.degreesToRadians);
    
        for (int i = 0; i < segments; i++) {
            renderer.color(colorBits);
            renderer.vertex(x + cx, y + cy, 0);
            float temp = cx;
            cx = cos * cx - sin * cy;
            cy = sin * temp + cos * cy;
            renderer.color(colorBits);
            renderer.vertex(x + cx, y + cy, 0);
        }
      }
    }
    

    Then calling it like so

        Arc a = new Arc();
        a.setProjectionMatrix(cam.combined);
        a.begin(ShapeType.Line);
        a.arc(10, 10, 10, 30, 120);
        a.end();
    

    The angle's are weird to get right, not sure if that's my code or libGDX

提交回复
热议问题