LibGDX - TiledMap cutting off after each 33rd tile

Hi all,

Pretty strange problem here, I am reading in a TiledMap and parsing through the Layers then creating a Box2D box around the collision layers.

Problem is, it reads in the tiles fine and puts a box around them, but the 33rd box seems to just be ignored and then shifts every box infront of that along one.

It fits to the size of my camera fine but as I want the camera to be scrolling along the map this issues occurs. It seems to ignore everything 33rd tile.

Imgur

Here’s what it looks like.

Source code for reading in TiledMap:

package com.adamdowson.sideflyer.level;

    import com.badlogic.gdx.maps.tiled.TiledMap;
    import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
    import com.badlogic.gdx.maps.tiled.TmxMapLoader;
    import com.badlogic.gdx.math.Vector2;
    import com.badlogic.gdx.physics.box2d.Body;
    import com.badlogic.gdx.physics.box2d.BodyDef;
    import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
    import com.badlogic.gdx.physics.box2d.FixtureDef;
    import com.badlogic.gdx.physics.box2d.PolygonShape;
    import com.badlogic.gdx.physics.box2d.World;

    public class LevelLoader
    {
       private World world;
       
       private TiledMap map;
       private TiledMapTileLayer collisionLayer;
       
       private int tileWidth, tileHeight;
       private String levelName;
       
       //private Array<Rectangle> collisionTiles = new Array<Rectangle>();
       
       public LevelLoader(World world, String levelName)
       {
          this.world = world;
          this.levelName = levelName;
          
          map = loadMapFile();
          loadTileLayers();
          initTiles();
       }
       
       private void initTiles()
       {      
          for (int x = 0; x < tileWidth; x++)
          {
             for (int y = 0; y < tileHeight; y++)
             {
                if (collisionLayer.getCell(x, y) != null)
                {   
                   createCollisionTile(x, y);
                }
             }
          }
       }
       
       private void createCollisionTile(int xPos, int yPos)
       {
          System.out.println(xPos + ", " + yPos);
          
          Vector2 tileCentre = new Vector2((xPos / 32) + 0.5f, (yPos / 32) + 0.5f);
          
          BodyDef body = new BodyDef();
          body.type = BodyType.StaticBody;
          body.position.set(xPos, yPos);
          Body tileBody = world.createBody(body);
          PolygonShape shape = new PolygonShape();
          shape.setAsBox(0.5f, 0.5f, tileCentre, 0);
          FixtureDef tileFixture = new FixtureDef();
          tileFixture.shape = shape;
          tileBody.createFixture(tileFixture);
       }
       
       private void loadTileLayers()
       {
          collisionLayer = (TiledMapTileLayer) map.getLayers().get("coll");
          tileWidth = collisionLayer.getWidth();
          tileHeight = collisionLayer.getHeight();
       }
       
       private TiledMap loadMapFile()
       {
          String levelDir = "level/" + levelName + ".tmx";
          
          return new TmxMapLoader().load(levelDir);
       }
       
       public TiledMap getMap()
       {
          return map;
       }
    }

This stood out to me: (just from quick scan-through)

try making the /32 into /32f, to avoid integer division.

EDIT: might also be something with rounding, if the 32f screws it up more, or doesn’t fix it, could also try [icode]Math.floor(xPos / 32f) + 0.5f[/icode]

Hi, thanks for your help, I tried forcing the float and that worked, the only issue is that it screwed the scaling.

Math.floor seemed to give the same result.

Could you post a screenshot of the “screwed scaling”?