【问题标题】:LibGDX draw lineLibGDX 画线
【发布时间】:2014-03-17 02:27:04
【问题描述】:

我正在尝试使用 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 中绘制简单线条的最佳方法是什么? 如果我找不到任何答案,我可能会从线的一点到另一点画圈……这看起来不错,但看起来不像弹弓……

有什么帮助吗?

【问题讨论】:

  • 我猜你收到垃圾邮件是因为每帧渲染都创建了ShapeRenderer 的实例。尝试在构造函数中创建一次。
  • 但我应该如何与思想划清界限?
  • 通常尝试在每个帖子中提出一个问题。 SO 就是围绕这一点设计的。无论如何,请参阅stackoverflow.com/questions/16680908/… 了解线条粗细。

标签: java libgdx


【解决方案1】:

我在 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,或者将其公开并根据您的需要自行完成。

显然,您可以为更多形状或更多重载创建更多方法。

【讨论】:

    【解决方案2】:

    您可以通过调用Gdx.gl10.glLineWidth(width_in_pixels)来设置线条粗细。

    【讨论】:

    • 我应该怎样让它看起来更好?
    • @Paul,您可以改为绘制纹理。例如,使用大小为[1px]x[16px] 的“线”图案将源纹理拉伸到[line_length]x[16px],并旋转它以适合起点和终点(使用Math.atan2)。我可以稍后提供一些代码。
    • 为什么我的手机上的线比较粗?我用了宽度 10
    【解决方案3】:

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

    【讨论】:

    • 哈哈,老问题!渲染方法中的 shaperenderer 也很糟糕。
    • 是的......在渲染方法中创建新对象对于游戏来说并不是最好的事情。我非常兴奋,以至于我真的知道如何回答一个我无法抗拒的关于 SO 的问题。即使问题是 10 个月大......
    • 我记得兄弟的感觉! :)
    • 顺便说一句,我认为你的答案是错误的,shapetype.filled 将绘制任何填充的形状,如圆形、矩形,而不是线条,真的,你不能填充线条。
    • 我现在在我的游戏中使用它。我只是比较了线条并填充了。填充绝对是您想要的 ShapeRenderer.rectLine()。它对 ShapeRenderer.line() 的工作方式与 shaptype.line 相同,但效率可能要低得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2016-10-17
    • 2015-11-20
    • 2012-05-20
    • 1970-01-01
    相关资源
    最近更新 更多