수색…


기와지도에서 Box2D 본문 만들기

Tiled Map (.tmx) 내에서 생성 된 객체는 다음과 같이 Libgdx MapObject 클래스를 사용하여 Box2D 세계에 바디로로드 될 수 있습니다.

public void buildBuildingsBodies(TiledMap tiledMap, World world, String layer){
    MapObjects objects = tiledMap.getLayers().get(layer).getObjects();
    for (MapObject object: objects) {
        Rectangle rectangle = ((RectangleMapObject)object).getRectangle();

        //create a dynamic within the world body (also can be KinematicBody or StaticBody
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        Body body = world.createBody(bodyDef);

        //create a fixture for each body from the shape
        Fixture fixture = body.createFixture(getShapeFromRectangle(rectangle),density);
        fixture.setFriction(0.1F);

        //setting the position of the body's origin. In this case with zero rotation
        body.setTransform(getTransformedCenterForRectangle(rectangle),0);
    }
}

다음 함수는 Tiled 객체 좌표를 Box2D 모양으로 매핑하는 데 도움이됩니다.

public static final float TILE_SIZE = 16;
//Also you can get tile width with: Float.valueOf(tiledMap.getProperties().get("tilewidth",Integer.class));

public static Shape getShapeFromRectangle(Rectangle rectangle){
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(rectangle.width*0.5F/ TILE_SIZE,rectangle.height*0.5F/ TILE_SIZE);
    return polygonShape;
}

그리고이 함수는 Tiled 객체의 중심을 Libgdx의 직사각형 모양에 매핑하는 데 도움이됩니다.

public static Vector2 getTransformedCenterForRectangle(Rectangle rectangle){
    Vector2 center = new Vector2();
    rectangle.getCenter(center);
    return center.scl(1/TILE_SIZE);
}

따라서 첫 번째 함수는 다음과 같이 사용할 수 있습니다.

public static final float GRAVITY = 9.8F;

public void createBodies(AssetManager assetManager){
    TiledMap tiledMap = assetManager.get("tiledMap.tmx");
    //create a Box2d world will contain the physical entities (bodies)
    World world = new World(new Vector2(0,GRAVITY),true);

    String layerName = "BuildingsLayers";
    buildBuildingsBodies(tiledMap,world,layerName);
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow