2015-06-07 128 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號線之後將其覆蓋。是的,這是一個蹩腳但又快又髒的小代碼解決方案。