libgdx
Box2D
खोज…
Tiled Map से Box2D निकाय बनाएं
एक टाइल किए गए मानचित्र (.tmx) के भीतर बनाई गई वस्तुओं को केवल एक बॉक्स 2 डी दुनिया में बॉडी के रूप में लोड किया जा सकता है।
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);
}
}
निम्नलिखित कार्य बॉक्स 2 डी आकृति के लिए टाइल की गई वस्तु निर्देशांक को मैप करने में मदद करते हैं।
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;
}
और यह कार्य लिबीडीएक्स के आयत आकार में टाइल वाली वस्तु के केंद्र को मैप करने में मदद करता है।
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