繁体   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