【问题标题】:LibGdX Tiled Map Box2d collision with Polygon Map ObjectLibGdX Tiled Map Box2d 与多边形地图对象的碰撞
【发布时间】:2017-08-21 21:24:59
【问题描述】:

我正在尝试使用 LibGdx Tiled 和 Box2D 为 Android 创建一个小型侧滚动条。 我正在使用对象层来获取玩家和世界之间的碰撞。 如果我使用矩形作为对象层,这工作正常。 但是当我尝试使用多边形时,碰撞不起作用。 我究竟做错了什么。 (抱歉拼写错误)

这是我的代码:

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

【问题讨论】:

    标签: java libgdx box2d


    【解决方案1】:

    我终于自己修好了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多