Bodies doesn't collide

Discuss issues specific the Java port of Box2D
Posts: 1
Joined: Wed Jul 22, 2015 11:35 am

Bodies doesn't collide

Postby Jotask » Fri Jul 24, 2015 12:30 pm

Hi, my bodies doesn't collide with others, and I don't know how fixed it.

I have a player and hi collide without any problem with other, but when I want create other bodies they doesn't collide and only detect the collision if the object collided is the same than them.

Here is the code for create the world, is created from a tiledMap and that class extends from dynamics.World. And they have a setContactListener(new ContactListener(){methods} ); :

Code: Select all

   private void createCollisions() {
      int layerIndex = map.getLayerIndex("collision");
      for (int tileX = 0; tileX < map.getWidth(); tileX++) {
         for (int tileY = 0; tileY < map.getHeight(); tileY++) {
            int tileID = map.getTileId(tileX, tileY, layerIndex);
            if(tileID != 0 && map.isBorder(tileX, tileY, layerIndex)){
               // Create box2d bodie for this tile
               Vector2 position = tileToWorld(tileX, tileY);
               position.add(map.getTileWidth() / 2, map.getTileHeight() / 2);

               Vec2 physicsPosition = toPhysicsVector(position);
               createBoxBody(physicsPosition, map.getTileWidth(), map.getTileHeight(), BodyType.STATIC);

Here is the code for the player body creation:

Code: Select all

      BodyDef bodyDef = new BodyDef();
      Vec2 tmp = toPhysicsVector(spawn);
      bodyDef.position.set(tmp.x, tmp.y);
      bodyDef.type = BodyType.DYNAMIC;
      bodyDef.linearDamping = 0.5f;
      FixtureDef fixture = new FixtureDef();
      CircleShape shape = new CircleShape();
      shape.m_radius = radius / World.PTM;
      fixture.shape = shape;
      body = world.createBody(bodyDef);

Ok this two bodies (world boundary and the player) collides with out any problem, but now if i want create another body for a enemy for example, they doesn't collide with any player bodie or world body and jus go down for every throw the floor

This is the code I use for create this body

Code: Select all

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      bd.position = new Vec2(x, y);
      CircleShape shape = new CircleShape();
      FixtureDef fixture = new FixtureDef();
      fixture.shape = shape;
      b = this.createBody(bd);

Any idea why the new bodies doesn't collide with the oldest? But if I a create two new bodies, they collide with the other, but no with the player or the world bodies

Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest