1
\$\begingroup\$

Hello I am making a game in libgdx. I want to draw a line/curve of the path a object have taken.

First a thought I could save all positions of the objects each iteration and draw line between them using the Shaperenderer. This is however inefficient because the number of saved positions for a object becomes really large, and I have alot of objects.

My second thought was to draw a line between only the previous position and the current position on to a pixmap, and then draw the pixmap to the screen. This way I only need to save the old position because I don't clear the pixmap. The problem is that the world is infinitely big, and the pixmap cant cover the entire world (and it is bad to make a really big pixmap?). Also when I zoom out of the world the lines becomes really small and I cant see them. When I use the shaperenderer, the lines are always one pixel wide independent of the zoom.

So is there a smart way where I can draw the path my objects have taken without saving all previous positions, and where the line stays constant width?

\$\endgroup\$
2
\$\begingroup\$

Why not save the position every 2.5 seconds? It will reduce the number of units needed to save variable

\$\endgroup\$
0
\$\begingroup\$

In either cases you'll run out of memory.

For lines you need to discard old lines, or simplify the lines as you go, or both.

For pixmaps you'll need to make multiple pixmaps in a grid and either discard them when they go off the screen or write them to the filesystem and reload them when you come back. And eventually running out of disk space if your world is infinitely big, might take years, but you will.

And as for zooming out pixmaps, you'll have to generate mipmaps (http://en.wikipedia.org/wiki/Mipmap) yourself by using the MAX or OR function so that when merging pixels if any of the 4 pixels are marked the resulting pixel is marked as well so they don't fade as you zoom out.

There is no easy solution for this as the amount of memory and CPU/GPU processing power are finite.

\$\endgroup\$
  • \$\begingroup\$ Ok thanks. I think I will try the pixmaps in a grid thing. Is there a way to draw the path using shaperenderer, and then never clearing what the shaperenderer has drawn? \$\endgroup\$ – lijas Jan 30 '15 at 18:32
  • \$\begingroup\$ I created a mipmap using "tpath = new Texture(Pixmap, Boolean usemipmap);", but as you said, the lines fade out when zooming out. What did you mean by using MAX or OR funcitons? \$\endgroup\$ – lijas Jan 30 '15 at 18:57
  • \$\begingroup\$ You have to generate the smaller images yourself. You take the largest pixmap, then create a quarter-sized (1/2 width x 1/2 height) pixmap and for each 2x2 pixels in the larger one you write the most opaque pixel (assuming line pixels are opaque and no-line pixels are transparent). So that the lines don't fade. Repeat until you're down to and including the last 1x1 pixmap. Then use MipMapTextureData to upload your custom mipmapped texture. So you don't average the pixels, if there's a line in the 2x2 pixels you write a line to the 1 pixel in the smaller pixmap. \$\endgroup\$ – Stephane Hockenhull Jan 31 '15 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.