How to draw a line in LibGDX without ShapeRenderer

146 Views Asked by At

I am using LibGDX and have an ArrayList of multiple points which I want to connect. I am aware there are several methods with ShapeRenderer that work, however im running a SpriteBatch at the same time so what do I do now to draw a line with two Vectors. (If it exists, I'd also like the function that draws multiple lines at once with an Àrray or Vector2 as a parameter, though it isn't a problem as otherwise I'd manage with a for-loop probably).

I am also aware I can use Pixmaps but they don't seem to work correctly. Here is my attempt:

// point1 and point2 are of type Vector2
Pixmap pixmap = new Pixmap(point2.x - point1.x, 2, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.drawLine(point1.x, point1.y, point2.x, point2.y);

In response to a possible solution that involves using ShapeRenderer at the same time, this problem arises (the second image uses the points with pixmaps, the first the ShapeRenderer with lines) With Shape Renderer:

With SpriteBatch and Pixmap points

The code used for the first image is the following:


        for(int i = 1; i < dotPositions.size(); i++) {
            sr.line(dotPositions.get(i-1), dotPositions.get(i));
        }

The code used for the second image is the following:

        Pixmap pixmap = new Pixmap(2, 2, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.WHITE);
        pixmap.fillCircle(2, 2, 2);
        Texture texture = new Texture(pixmap);

        for(int i = 1; i < dotPositions.size(); i++) {
            batch.draw(texture, dotPositions.get(i).x, dotPositions.get(i).y);
        }

In both cases dotPositions is an ArrayList<Vector2> with the same values.

1

There are 1 best solutions below

0
eisa.exe On

If anyone in the future may be interested, I found the solution and all I had to do was use ShapeRender.setProjectionMatrix(cam.combined) to sync it with the SpriteBatch.