簡體   English   中英

Box2D主體是否帶有紋理?

[英]Box2D Body with Texture?

我知道如何將Sprite應用於Box2d實體,但是有沒有辦法對其應用紋理? 基本上,我想要做的就是擁有一個紋理,例如32x32,然后在整個身體上重復該紋理,就像這張圖片中的地面一樣:

在此處輸入圖片說明

LibGDX有可能嗎?

編輯:

我的最新嘗試:

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

並在渲染中:

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

但它並沒有畫任何東西。

導入不同形狀的關卡后,我得到以下結果:

在此處輸入圖片說明

只有一個多邊形(顯示在紅色圓圈內部)可以獲取紋理。 整個級別作為JSON文件導入

是的,這在libgdx中非常有可能。

您只需要為此創建一個多邊形區域

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

這里的textureRegion是要重復的區域。 頂點和三角形定義了區域的形狀。

此多邊形區域是重復的紋理,從頂點和三角形形成紅色。 您可以使用多邊形批處理渲染此區域,就像我們使用精靈批處理進行渲染一樣。

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