Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGDX draw line

Tags:

java

libgdx

I'm trying to make something like a slingshot using libGDX.

My code

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

    }

Doing this i will have the output enter image description here 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

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.

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...

Any help?

like image 440
Boldijar Paul Avatar asked Feb 17 '14 17:02

Boldijar Paul


2 Answers

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

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.

like image 131
Madmenyo Avatar answered Sep 19 '22 13:09

Madmenyo


You could set line thickness by calling Gdx.gl10.glLineWidth(width_in_pixels).

like image 38
desertkun Avatar answered Sep 18 '22 13:09

desertkun