Search code examples
javaandroidlibgdx

libGdx: Draw Line on Stage and keep it there


I am working on an app, where I need to draw the trace of my Sprites. I tried to do it with a ShapeRenderer, and it draws the Lines correctly, but after the line was drawn, it gets erased again. So I tried to do it with a Polyline, and add the Points with each update, but after some time this polyline is too large to draw.

I have my code in an overriden Actor class, which on every draw call checks if the sprite has moved, and if so, the line gets drawn with the ShapeRenderer. Is there a way, to keep that line ?

Code:

ShapeRenderer renderer = StageActivity.stageListener.shapeRenderer;
renderer.setColor(color);
Gdx.gl.glLineWidth(strokeWidth);
line.add(sprite.look.getX());
line.add(sprite.look.getY());
renderer.polyline(getLineVertices());

The getLineVertices() Method is just converting my ArrayList to a float[] array.

EDIT:

Since the code above has very poor Performance with a lot of Lines, I tried out the Framebuffer which is essentially what I need (I think), but it does not get draw, what am I doing wrong ?

buffer.begin();
renderer.begin(ShapeRenderer.ShapeType.Line);
renderer.line(previousPoint.x, previousPoint.y, sprite.look.getX(), sprite.look.getY());
renderer.end();
buffer.end();
batch.draw(buffer.getColorBufferTexture(), 0, 0);

The Buffer is a global variable in my Actor.


Solution

  • I resolved my Issue with the framebuffer. It was not rendering, because I had to call batch.end() before calling framebuffer.getColorBufferTexture().