Tile Map + box2D (static collision don't move)

Hi guys.
I am newbie to Box2D.
I am using cocos2d-x-3.8.
I managed to import tile map and apply collision which I made in Tiled Map file (tmx).
I used Rectangle type and it is static.
I moved the map using Parralax Node.
Unfortunately, when I tried to move my character this collision on ground doesn't move either....
I used PTM_RATIO 64.
Tile Map per pixel is 32 x 32.

Codes for collision rectangle from Tile Map.

Code: Select all

   auto position = Point(object["x"].asFloat() / PTMRATIO, object["y"].asFloat() / PTMRATIO);
   float width = object["width"].asFloat() / PTMRATIO;
   float height = object["height"].asFloat() / PTMRATIO;

   b2PolygonShape *rectshape = new b2PolygonShape();
   b2Vec2 vertices[4];
   int vindex = 4;

   vertices[0].x = position.x + 0.0f;
   vertices[0].y = position.y + 0.0f;

   vertices[1].x = position.x + 0.0f;
   vertices[1].y = position.y + height;

   vertices[2].x = position.x + width;
   vertices[2].y = position.y + height;

   vertices[3].x = position.x + width;
   vertices[3].y = position.y + 0.0f;

   rectshape->Set(vertices, vindex);

   auto fix = new FixtureDef();
   fix->fixture.shape = rectshape;
   fix->fixture.density = 10;
   fix->fixture.friction = 0.5;
   fix->fixture.restitution = 0.6;

Code to move character.

Code: Select all

   b2Vec2 force = b2Vec2(30, 30);
   bodyHero->ApplyLinearImpulse(force, bodyHero->GetPosition(), true);

Please help me guy.
I just did what you're trying to achive (with libgdx. If I got it right. Your "question" is not clear).

Your best bet is to forget the tile to box2d object conversion.
Put on an object layer, than draw your box2d objects on that layer. Thats your safest bet. Easier, and also grants you better performance, along some flexibility, with what you can do.

I don't know what langauge cocos2d uses, libgdx uses java,but you can try to copy the logic.

Code: Select all

 // The pixels per tile. If your tiles are 16x16, this is set to 16f
    private static float ppt = StaticVariables.TILE_SIZE;

    public static Array<Body> buildShapes(Map map, float pixels, World world) {
        ppt = pixels;
        MapObjects objects = map.getLayers().get("Ground").getObjects();

        Array<Body> bodies = new Array();

        for(MapObject object : objects) {

            if (object instanceof TextureMapObject) {
                continue;
            }

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

           try
           {
               BodyDef bd = new BodyDef();
               bd.type = BodyDef.BodyType.StaticBody;
               Body body = world.createBody(bd);
               FixtureDef fixtureDef = new FixtureDef();
               fixtureDef.shape = shape;
               fixtureDef.density = 0;
               fixtureDef.friction = 0.5f;
               fixtureDef.filter.categoryBits = StaticVariables.CATEGORY_SCENERY;
               fixtureDef.filter.maskBits = StaticVariables.MASK_SCENERY;
               body.createFixture(fixtureDef);


               bodies.add(body);

               shape.dispose();
           }
           catch (Exception e)
           {}
        }
        return bodies;
    }



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

    private static CircleShape getCircle(CircleMapObject circleObject) {
        Circle circle = circleObject.getCircle();
        CircleShape circleShape = new CircleShape();
        circleShape.setRadius(circle.radius / ppt);
        circleShape.setPosition(new Vector2(circle.x / ppt, circle.y / ppt));
        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] / ppt;
        }

        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] / ppt;
            worldVertices[i].y = vertices[i * 2 + 1] / ppt;
        }

        ChainShape chain = new ChainShape();
        chain.createChain(worldVertices);
        return chain;
    }
I am trying to make the static collision I made for objects in Tile Map files, to move along with TileMap.
It seems very hard.
I've done your way.
I didn't display the whole code as I think it is not necessary.
The box2D for static collision did get displayed, but when my character moved; it should be moved too.
Maybe I have to use camera to move along it.
but thanks anyway for answering my question.

Original thread: https://box2d.org/forum/viewtopic.php?t=10119