2015-06-07 67 views
1

libgdx的ARC功能,而不是画弧的绘制扇形(即有2条线连接到圆弧的起点)libgdx拉弧曲线

shapeRenderer.begin(ShapeType.Line); 
shapeRenderer.arc(x, y, radius, 30, 120); 
shapeRenderer.end(); 

是否有这个问题的解决方案,使libgdx可以绘制类似于html5 canvas arc函数的弧线曲线吗?

回答

2

阅读源代码,这似乎内置行为:

/** 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) { 
    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); 

    if (shapeType == ShapeType.Line) { 
     check(ShapeType.Line, ShapeType.Filled, segments * 2 + 2); 

     renderer.color(colorBits); 
     renderer.vertex(x, y, 0);   <--- CENTER 
     renderer.color(colorBits); 
     renderer.vertex(x + cx, y + cy, 0); <--- LINE TO START POINT 
     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); 
     } 
     renderer.color(colorBits); 
     renderer.vertex(x + cx, y + cy, 0); <-- LINE TO END POINT 
...

最简单的可能就是复制整个功能,并且至少抛出了两个I标线:CENTERLINE TO END POINT ,以及各自上面的renderer.color(..行。

(您也可以删除LINE TO START POINT - 它出现以设置曲线的起点,但是这实际上也是在循环中完成的,因此它是多余的。)

的功能有第二(我同意,它应该正确地命名为“馅饼”或“楔形”),但是在这里你不需要这样做,因为它会完全相同。

如果你得到它的工作,你可以建议它libgdx的维护者,例如在libgdx's Contributions Forum

+0

感谢,肯定比Bezier曲线方法容易很多,我正在考虑 – ejectamenta

+1

似乎也许所有那些renderer.color(colorBits);循环中的语句也可能是不必要的,或者每个render.vertex需要在renderer.colour语句前面(奇怪的) – ejectamenta

+0

@ejectamenta:是的,我也注意到了。如果有某种缓存涉及哪些行可能无序排列,则这*可能是必需的,但要弄清楚,您需要深入了解源代码。 – usr2564301

1

最后,我子归的ShapeRenderer类

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); 
    } 
    } 
} 

然后调用它像这样

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

角度的怪异得到正确的,不知道这是我的代码或libGDX

+1

请注意,你的类没有正确地遵守'arc.setColor()'指令。 'color'的值应该通过调用'setColor()'来更新。不过,好工作。刚刚添加了一个工作impl。来源。 – EntangledLoops

0

我认为你和其他响应者已经发布了当前最好的解决方案。另一种“黑客”的人谁

  1. 不那么在乎性能
  2. 有一个静态的背景

将呈现在具有相同背景的绘制彩色绘制的2号线之后将其覆盖。是的,这是一个蹩脚但又快又脏的小代码解决方案。