I have a simple triangular shape (obstacle) in my game and i would like to fill it with a texture repeatedly. I have looked at the other 2 topics but couldn't find a working solution. How can i fill this triangle with a small image (assume that its called "brickTexture.png" repeatedly? Here is the code for creating the box2dbody of the obstacle in Obstacle.java
BodyDef bdef = new BodyDef();
bdef.position.set(obstaclePosition);
bdef.type = BodyDef.BodyType.StaticBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
PolygonShape triangle = new PolygonShape();
float vertices1[] ={-50 / PPM, 100 / PPM,
50 / PPM, 100 / PPM,
0 / PPM, 0 / PPM};
triangle.set(vertices1);
fdef.shape = triangle;
b2body.createFixture(fdef);
And this is the triangle
This can easily be achieved by using the classes PolygonRegion and PolygonSpriteBatch and will work for any polygonal shape (not just triangles).
Create the polygon region:
// this will calculate the triangles given your vertices
short triangles[] = new EarClippingTriangulator().computeTriangles(vertices1).toArray();
// use your texture region
PolygonRegion polygonRegion = new PolygonRegion(textureRegion, vertices1, triangles);
Then you need to use the PolygonSpriteBatch to render at the desired position (I presume body position):
polygonSpriteBatch.draw(polygonRegion, x, y);
Make sure you load your texture with Repeat wrap so it can tile:
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
Class docs: PolygonRegion, EarClippingTriangulator, PolygonSpriteBatch