1

我目前正在使用 libgdx 和它的 box2d 扩展来制作开放世界平台游戏。我想从 tmx tilemap 渲染一个世界,并从中添加物理。tilemap 渲染得很好(使用OrthogonalTiledMapRenderer),但是当我尝试使用以下代码添加物理时,什么也没发生。

public void renderPhysicsMap(TiledMap map, World world, int layer, int tileSize) {
        TiledMapTileLayer mapLayer = (TiledMapTileLayer) map.getLayers().get(layer);
        MapObjects mapObjects = mapLayer.getObjects();
        for (MapObject mapObject : mapObjects) {
            Rectangle rect = ((RectangleMapObject) mapObject).getRectangle();
            BodyDef bodyDef = new BodyDef();
            bodyDef.type = BodyType.StaticBody;
            Body body = world.createBody(bodyDef);
            PolygonShape shape = new PolygonShape();
            shape.setAsBox((rect.width/2)/tileSize, (rect.height/2)/tileSize);
            Fixture fixture = body.createFixture(shape, 0.0f);
            fixture.setFriction(0.1f);
            Vector2 center = new Vector2();
            rect.getCenter(center);
            body.setTransform(center.scl(1/tileSize), 0);
        }
    }

经过一些调试(运行mapObjects.getCount())后,我发现大小为 0。这很奇怪,因为我知道这是一个有效的 tilemap(我可以很好地渲染它)。有谁知道这是为什么?

4

0 回答 0