简体   繁体   English

Box2D主体是否带有纹理?

[英]Box2D Body with Texture?

I know how to apply sprite to a Box2d body, but is there a way to apply a texture to it? 我知道如何将Sprite应用于Box2d实体,但是有没有办法对其应用纹理? Basically what I am trying to do is to have one texture, let's say 32x32, and then just repeat it all over the body, like the ground in this image: 基本上,我想要做的就是拥有一个纹理,例如32x32,然后在整个身体上重复该纹理,就像这张图片中的地面一样:

在此处输入图片说明

Is this possible in LibGDX? LibGDX有可能吗?

EDIT: 编辑:

My latest try: 我的最新尝试:

Fixture fixture = body.createFixture(fixtureDef);
        Vector2 mTmp = new Vector2();
        PolygonShape shape = (PolygonShape) fixture.getShape();
        int vertexCount = shape.getVertexCount();
        float[] vertices = new float[vertexCount * 2];
        for (int k = 0; k < vertexCount; k++) {
            shape.getVertex(k, mTmp);
            mTmp.rotate(body.getAngle()* MathUtils.radiansToDegrees);
            mTmp.add(body.getPosition()); 
            vertices[k * 2] = mTmp.x * PIXELS_PER_METER;
            vertices[k * 2 + 1] = mTmp.y * PIXELS_PER_METER;
        }
        short triangles[] = new EarClippingTriangulator().computeTriangles(vertices).toArray();

        Texture texture = new Texture(Gdx.files.internal("data/block.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        TextureRegion textureRegion = new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());

        PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangles);

        poly = new PolygonSprite(region);

and in rendering: 并在渲染中:

polyBatch.begin();
        poly.draw(polyBatch);
        polyBatch.end();

but it doesn't draw anything. 但它并没有画任何东西。

After importing different shape of level, I get this result: 导入不同形状的关卡后,我得到以下结果:

在此处输入图片说明

Only one polygon ( shown inside of red circle ) gets the texture. 只有一个多边形(显示在红色圆圈内部)可以获取纹理。 Whole level is imported as a JSON file 整个级别作为JSON文件导入

Yes This is very much possible in libgdx. 是的,这在libgdx中非常有可能。

You just need to create a polygon region for that 您只需要为此创建一个多边形区域

PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangles);

Here textureRegion is the region that you want to repeat. 这里的textureRegion是要重复的区域。 vertices and triangles define the shape of the region. 顶点和三角形定义了区域的形状。

This polygon region is a repeated texture that is form red from the vertices and triangles. 此多边形区域是重复的纹理,从顶点和三角形形成红色。 You can render this region using polygon batch just same as we do it with sprite batch. 您可以使用多边形批处理渲染此区域,就像我们使用精灵批处理进行渲染一样。

UPDATE UPDATE

PolygonShape shape = (PolygonShape) fixture.getShape();
int vertexCount = shape.getVertexCount();
float[] vertices = new float[vertexCount * 2];
for (int k = 0; k < vertexCount; k++) {
    shape.getVertex(k, mTmp);
    mTmp.rotate(body.getAngle()* MathUtils.radiansToDegrees);
    mTmp.add(bodyPos); 
    vertices[k * 2] = mTmp.x * PIXELS_PER_METER;
    vertices[k * 2 + 1] = mTmp.y * PIXELS_PER_METER;
}
short triangles[] = new EarClippingTriangulator()
        .computeTriangles(vertices)
        .toArray();
PolygonRegion region = new PolygonRegion(
        textureRegion, vertices, triangles);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM