簡體   English   中英

LibGDX繪制線

[英]LibGDX draw line

我正在嘗試使用libGDX制作類似彈弓的東西。

我的代碼

if (Gdx.input.isTouched()) {

        ShapeRenderer sr = new ShapeRenderer();
        sr.setColor(Color.BLACK);
        sr.setProjectionMatrix(camera.combined);

        sr.begin(ShapeType.Line);
        sr.line(player.getLeft().x, player.getLeft().y,
                Global.game_touch_position.x, Global.game_touch_position.y);
        sr.line(player.getRight().x, player.getRight().y,
                Global.game_touch_position.x, Global.game_touch_position.y);
        sr.end();

    }

這樣做我將得到輸出 在此輸入圖像描述 這看起來很糟糕,如果我在我的Android手機上調試,logcat會被郵件發送垃圾郵件

02-17 18:55:27.371: D/dalvikvm(7440): GC_CONCURRENT freed 1884K, 40% free 8287K/13635K, paused 15ms+2ms, total 40ms

並且滯后,當我觸摸屏幕時,我有30 fps,而當我不觸摸時,我有60 fps ...

我還需要繪制一點厚度的線條,所以當線條更大時,我必須使它更厚,以給出一個很酷的外觀。

在libgdx中繪制簡單線條的最佳方法是哪種? 如果我找不到任何答案,我可能會從一條線到另一條線畫圓圈。這樣看起來不錯,但看起來不像彈弓......

有幫助嗎?

我只是在helper或utils類中有這樣的東西。 我通常用它來調試和可視化最新進展。

private static ShapeRenderer debugRenderer = new ShapeRenderer();

    public static void DrawDebugLine(Vector2 start, Vector2 end, int lineWidth, Color color, Matrix4 projectionMatrix)
    {
        Gdx.gl.glLineWidth(lineWidth);
        debugRenderer.setProjectionMatrix(projectionMatrix);
        debugRenderer.begin(ShapeRenderer.ShapeType.Line);
        debugRenderer.setColor(color);
        debugRenderer.line(start, end);
        debugRenderer.end();
        Gdx.gl.glLineWidth(1);
    }

    public static void DrawDebugLine(Vector2 start, Vector2 end, Matrix4 projectionMatrix)
    {
        Gdx.gl.glLineWidth(2);
        debugRenderer.setProjectionMatrix(projectionMatrix);
        debugRenderer.begin(ShapeRenderer.ShapeType.Line);
        debugRenderer.setColor(Color.WHITE);
        debugRenderer.line(start, end);
        debugRenderer.end();
        Gdx.gl.glLineWidth(1);
    }

現在,我可以在任何我喜歡的任何投影矩陣上輕松地繪制一條線。

HelperClass.DrawDebugLine(new Vector2(0,0), new Vector2(100,100), camera.combined);

你想在其他SpriteBatch之外繪制開始和結束。 如果你想在每一幀中制作很多行,你最好在一個單獨的靜態方法中開始和結束ShapeRenderer,或者將它公開並根據你的需要自己做。

顯然,您可以為更多形狀或更多重載創建更多方法。

您可以通過調用Gdx.gl10.glLineWidth(width_in_pixels)來設置線條粗細。

ShapeRenderer有一個名為rectLine()的方法,它繪制一條具有指定厚度的線。 正是您正在尋找的線厚度問題。 您還需要將sr.begin(ShapeType.Line)更改為sr.begin(ShapeType.Filled)

if (Gdx.input.isTouched()) {

    ShapeRenderer sr = new ShapeRenderer();
    sr.setColor(Color.BLACK);
    sr.setProjectionMatrix(camera.combined);

    sr.begin(ShapeType.Filled);
    sr.rectLine(player.getLeft().x, player.getLeft().y,
            Global.game_touch_position.x, Global.game_touch_position.y, desired_thickness);
    sr.rectLine(player.getRight().x, player.getRight().y,
            Global.game_touch_position.x, Global.game_touch_position.y, desired_thickness);
    sr.end();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM