Box2d Body 填充了纹理 Libgdx

Box2d Body filled with a texture Libgdx

我的游戏中有一个简单的三角形(障碍物),我想反复用纹理填充它。我查看了其他 2 个主题,但找不到可行的解决方案。我怎样才能用一个小图像填充这个三角形(假设它被反复调用 "brickTexture.png" ? 这是在 Obstacle.java

中创建障碍物 box2dbody 的代码
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);

这是三角形

这可以通过使用 类 PolygonRegionPolygonSpriteBatch 轻松实现,并且适用于任何多边形形状(不是只是三角形)。

创建多边形区域:

// 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);

然后你需要使用PolygonSpriteBatch渲染到想要的位置(我假设是body位置):

polygonSpriteBatch.draw(polygonRegion, x, y);

确保您使用 重复 包裹加载您的纹理,以便它可以平铺:

texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

Class 文档:PolygonRegion, EarClippingTriangulator, PolygonSpriteBatch