LibGDX绘制线 [英] LibGDX draw line

查看:97
本文介绍了LibGDX绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用libGDX制作类似弹弓的东西。

I'm trying to make something like a slingshot using 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();

    }

这样做我将得到输出
< img src =https://i.stack.imgur.com/P2qy4.pngalt =在此处输入图像说明>
这看起来很糟糕,如果我在我的Android手机上调试,则logcat是垃圾邮件消息

Doing this i will have the output This looks awful, and if I debug on my android phone , the logcat is spammed by the message

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,当我不... ...

And lags, I have like 30 fps when i touch the screen , and 60 when i don't...

我还需要绘制一点厚度的线条,所以当线条更大时,我将不得不做得更厚,给人一种很酷的外观。

I also need to draw the line with a little bit of thickness, so when the line is bigger, i will have to make it thicker, to give a cool look.

在libgdx中绘制简单线条的最佳方法是什么?
如果我找不到任何答案,我可能会从一条线到另一条线画圆圈。这样看起来不错,但看起来不像弹弓...

Which is the best way to draw a simple line in libgdx ? If I won't find any answer probably i'm going to draw circles from a point of the line to the other..this would look ok , but won't look like a slingshot...

任何帮助?

推荐答案

我只是在帮手或utils类。我通常用它来调试和可视化最新情况。

I just have something like this in a helper or utils class. I usually use it for debugging and visualizing whats going on.

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

现在我可以在任何投影矩阵上轻松地从任何地方画一条线我希望。

Now I can easily draw a line from anywhere I like on any projection matrix I want.

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

你想在另一个外面画画 SpriteBatch 开始和结束。如果你想在每个帧中制作很多行,你最好在一个单独的静态方法中开始和结束ShapeRenderer,或者将它公开并根据你的需要自己做。

You want to draw outside the other SpriteBatch begin and end. And if you want to make a lot of lines each frame you are better off beginning and ending the ShapeRenderer in a separate static method or make it public and do it yourself depending on your needs.

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

Obviously you can create more methods for more shapes or more overloads.

这篇关于LibGDX绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆