简体   繁体   English

LibGdX平铺的地图Box2d与多边形地图对象的碰撞

[英]LibGdX Tiled Map Box2d collision with Polygon Map Object

Im trying to create a small Side scroller for Android with LibGdx Tiled and Box2D. 我正在尝试使用LibGdx Tiled和Box2D为Android创建一个小的Side滚动器。 I'm using Object Layer to get the collision between Player and the World. 我正在使用“对象层”来获得Player和World之间的碰撞。 This is working fine if i use Rectangles for the Object Layers. 如果我将矩形用于对象层,则效果很好。 But when I'm trying to use Polygones the collision isn't working. 但是,当我尝试使用多边形时,碰撞不起作用。 What am I doing wrong. 我究竟做错了什么。 (Sry for spelling mistakes) (对拼写错误表示歉意)

Here is my code: 这是我的代码:

for(MapObject object : map.getLayers().get(5).getObjects()){
            if(object instanceof RectangleMapObject) {
                Rectangle rect = ((RectangleMapObject) object).getRectangle();

            bdef.type = BodyDef.BodyType.StaticBody;
            bdef.position.set((rect.getX() + rect.getWidth() / 2) / AoF.PPM, (rect.getY() + rect.getHeight() / 2) / AoF.PPM);

            body = world.createBody(bdef);

            shape.setAsBox(rect.getWidth() / 2 / AoF.PPM, rect.getHeight() / 2 / AoF.PPM);
            fdef.shape = shape;
            body.createFixture(fdef);
        }
        if(object instanceof PolygonMapObject){
           float[] vertices = ((PolygonMapObject) object).getPolygon().getTransformedVertices();
            float[] worldVertices = new float[vertices.length];

            for (int i = 0; i < vertices.length; ++i) {
                worldVertices[i] = vertices[i] / AoF.PPM;
            }

            shape.set(worldVertices);
            bdef.type = BodyDef.BodyType.StaticBody;

            body = world.createBody(bdef);
            fdef.shape = shape;
            body.createFixture(fdef);
        }

I finaly fixed it myself. 我最终自己修复了它。

for(MapObject object : map.getLayers().get(5).getObjects()){

         Shape shape;
        if (object instanceof RectangleMapObject) {
            shape = getRectangle((RectangleMapObject)object);
        }
        else if (object instanceof PolygonMapObject) {
            shape = getPolygon((PolygonMapObject)object);
        }
        else if (object instanceof PolylineMapObject) {
            shape = getPolyline((PolylineMapObject)object);
        }
        else if (object instanceof CircleMapObject) {
            shape = getCircle((CircleMapObject)object);
        }
        else {
            continue;
        }
            bdef.type = BodyDef.BodyType.StaticBody;
            body = world.createBody(bdef);
            fdef.shape = shape;
            body.createFixture(fdef);
        }


private static PolygonShape getRectangle(RectangleMapObject rectangleObject) {
        Rectangle rectangle = rectangleObject.getRectangle();
        PolygonShape polygon = new PolygonShape();
        Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) / AoF.PPM,
                (rectangle.y + rectangle.height * 0.5f ) / AoF.PPM);
        polygon.setAsBox(rectangle.width * 0.5f /AoF.PPM,
                rectangle.height * 0.5f / AoF.PPM,
                size,
                0.0f);
        return polygon;
    }

    private static CircleShape getCircle(CircleMapObject circleObject) {
        Circle circle = circleObject.getCircle();
        CircleShape circleShape = new CircleShape();
        circleShape.setRadius(circle.radius / AoF.PPM);
        circleShape.setPosition(new Vector2(circle.x / AoF.PPM, circle.y / AoF.PPM));
        return circleShape;
    }

    private static PolygonShape getPolygon(PolygonMapObject polygonObject) {
        PolygonShape polygon = new PolygonShape();
        float[] vertices = polygonObject.getPolygon().getTransformedVertices();

        float[] worldVertices = new float[vertices.length];

        for (int i = 0; i < vertices.length; ++i) {
            System.out.println(vertices[i]);
            worldVertices[i] = vertices[i] / AoF.PPM;
        }

        polygon.set(worldVertices);
        return polygon;
    }

    private static ChainShape getPolyline(PolylineMapObject polylineObject) {
        float[] vertices = polylineObject.getPolyline().getTransformedVertices();
        Vector2[] worldVertices = new Vector2[vertices.length / 2];

        for (int i = 0; i < vertices.length / 2; ++i) {
            worldVertices[i] = new Vector2();
            worldVertices[i].x = vertices[i * 2] / AoF.PPM;
            worldVertices[i].y = vertices[i * 2 + 1] / AoF.PPM;
        }

        ChainShape chain = new ChainShape();
        chain.createChain(worldVertices);
        return chain;
    }

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

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