4

I work at a truck game with libgdx and box2d. In my game 1 meter = 100 pixels. My 2d terrain is generated by me, and is made by points.

enter image description here

What I did, is made a polygonregion for the whole polygon and used texturewrap.repeat. The problem is that, my game size is scaled down by 100 times, to fit the box2d units.

So my camera width is 800 / 100 and height 480 / 100. (8x4.8 pixels)

How I created my polygon region

box = new Texture(Gdx.files.internal("box.png"));
    box.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    box.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

    TextureRegion region = new TextureRegion(box);

    psb = new PolygonSpriteBatch();

    float[] vertices = new float[paul.size];
    for (int i = 0; i < paul.size; i++) {
        vertices[i] = paul.get(i);
        if (i % 2 == 1)
            vertices[i] += 1f;

    }

    EarClippingTriangulator a = new EarClippingTriangulator();
    ShortArray sar = a.computeTriangles(vertices);
    short[] shortarray = new short[sar.size];
    for (int i = 0; i < sar.size; i++)
        shortarray[i] = sar.get(i);
    PolygonRegion pr = new PolygonRegion(region, vertices, shortarray);

    System.out.println(vertices.length + " " + shortarray.length);

    ps = new PolygonSprite(pr);

Now i'll just draw the polygonsprite to my polygonsprite batch. This will render the texture on the polygon repeatedly, but the picture will be 100 times bigger and is very streched. The left example is the one that i want to make, and the right one is the way that my game looks..

enter image description here

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • I've been struggling with this sort of issue too, haven't been able to figure out a solution for my texture to not be stretched. If I don't set the projection matrix on the polygonBatch then it'll render fine but then the texture doesn't "move" with the camera. The texture sits static as I pan the camera around. – lifo Feb 11 '15 at 12:34

3 Answers3

2

This PR was merged which looks like it does what you want: https://github.com/libgdx/libgdx/pull/3799

See RepeatablePolygonSprite.

NateS
  • 5,751
  • 4
  • 49
  • 59
  • Very nice to see this after so long time! Things changed a lot since I asked this question, and I hope I'll get free time again, to start working at this kind of games :) – Boldijar Paul Dec 20 '16 at 08:43
1

I am not completely sure if this will solve your problem (can't test it right now), but you need to set the texture coordinates of your TextureRegion to a higher value, probably your factor of 100.

So you could try to use region.setU2(100) and region.setV2(100). Since Texture Coordinates usually go from [0,1], the values higher than that will be outside. And because you set the TextureWrap to repeat, this will then repeat your texture over and over.

This way, the TextureRegion will alredy show your one texture repeated 100 times in x and y direction. If you then tell the PolygonSprite to use that region, it should show it as in the image you posted.

Hope that helps... :)

florianbaethge
  • 2,520
  • 3
  • 22
  • 29
1

You could create a new texture by code. Take your level size and fill it with your texture then delete to background the top side. Look at pixelmap. Maybe this will help you.

Edit:

TextureRegion doesn't repeat to fit the size you even use texture, or you use TiledDrawable.

Rareș Smeu
  • 95
  • 1
  • 11