[100%Solved] tmx (x,y) doesnt match libgdx(x,y)-How to? -Already tried several

Hi!
Ok, this time i seriously got halt.
Idk whats happening, i tried messing with unit scale, etc.

The thing is, each tile of mine is 32 pix -width and 16 pix Height

The tmx is 9tile per 9tile;

When i get the x/y pos of each tile, it doesnt match the x/y of the libgdx cartesian graphic.
What should i do now?
I know the code below is somewhat not in good O.O practice, but its just for learning TMX

Please help!

public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private Rectangle planeRect;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;
    private ArrayList<OrtoPoint> collisionPoints = new ArrayList<>();
    private BitmapFont bf;

    @Override
    public void create() {
        batch = new SpriteBatch();

        //Plane
        plane = new Texture(Gdx.files.internal("plane16.jpg"));
        planeSprite = new Sprite(plane);

        xplane = 10;
        yplane = 10;
        planeRect = new Rectangle(xplane, yplane, 16, 16);


        //BitmapFont
        bf = new BitmapFont();
        bf.setColor(Color.MAGENTA);


        //MapWorkout
        map = new TmxMapLoader().load("GrassLevelOne.tmx");
        mr = new OrthogonalTiledMapRenderer(map);

        //Camera 
        camera = new OrthographicCamera(900, 900);
        
        camera.position.set(900 / 2, 900 / 2, 0);
        camera.update();
        camera.apply(Gdx.gl10);



        try {
            whatevername(map);
        } catch (Exception ex) {
            Logger.getLogger(AlphaColourLibgdx.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    // First, we need to get the layer in which you create the tiles.
// In this case I assume you call the layer 'collision' layer in Tiled.
    private int computeCollisionLayerIndex(TiledMap map) {
        for (int i = 0; i < map.getLayers().getCount(); i++) {
            // This is used to get the layer with the name "collision" from all layers
            // (The layers name in Tiled)
            if (map.getLayers().get(i).getName().equalsIgnoreCase("collision")) {
                return i;
            }
        }
        return -1;
    }

// Now we surround getting the layer from the index
// with a little error handling:
    public void whatevername(TiledMap map) throws Exception {
        int collisionLayerIndex = computeCollisionLayerIndex(map);
        if (collisionLayerIndex != -1) {
            MapLayer layer = map.getLayers().get(collisionLayerIndex);

            if (layer instanceof TiledMapTileLayer) {
                getCollisionTilesFrom((TiledMapTileLayer) layer);
            } else {
                throw new Exception("");
            }

        } else {
            throw new Exception("");
        }

    }

// And finally we print the ID of each tile in the layer
// I don't know what you would do with the tiles in the end,
// and since I don't know what tile ID's your tiles have,
// I simply print them.
    public void getCollisionTilesFrom(TiledMapTileLayer layer) {

        int tileHeight = (int) layer.getTileHeight();
        int tileWidth = (int) layer.getTileWidth();
        int tileQuantity = 0;

        System.out.println("TileHeight : " + tileHeight);
        System.out.println("TileWidth : " + tileWidth);

        for (int x = 0; x < layer.getWidth(); x++) {
            for (int y = 0; y < layer.getHeight(); y++) {
                TiledMapTileLayer.Cell cell = layer.getCell(x, y);

                if ((cell != null) && (cell.getTile() != null)) {
                    System.out.println(cell.getTile().getId() + " x:" + x + " y:" + y);
                    collisionPoints.add(new OrtoPoint(x, y, tileWidth, tileHeight));
                    tileQuantity++;
                }
            }
        }
        System.out.println("Tile Qnt :: " + tileQuantity);

    }

    @Override
    public void render() {
        GL10 gl = Gdx.gl10;
        gl.glClearColor(1, 1, 1, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        mr.setView(camera);
        mr.render();


        batch.begin();
        planeSprite.setPosition(xplane, yplane);
        planeSprite.draw(batch, 1);

        bf.draw(batch, "[o]",1,1); // Doesnt Match 

        batch.flush();
        batch.end();

        boolean moveRight = Gdx.input.isKeyPressed(Input.Keys.DPAD_RIGHT);
        boolean moveLeft = Gdx.input.isKeyPressed(Input.Keys.DPAD_LEFT);

        boolean moveUp = Gdx.input.isKeyPressed(Input.Keys.DPAD_UP);
        boolean moveDown = Gdx.input.isKeyPressed(Input.Keys.DPAD_DOWN);

        if (moveUp == true) {
            yplane = yplane + 5;
        }

        if (moveDown == true) {
            yplane = yplane - 5;
        }

        if (moveRight == true) {
            xplane = xplane + 5;
        }

        if (moveLeft == true) {
            xplane = xplane - 5;
        }

        for (int k = 0; k < collisionPoints.size(); k++) {
            OrtoPoint ortoAux = collisionPoints.get(k);
            Rectangle collisionRectangle = ortoAux.getCollisionRectangle();
            int x = ortoAux.getX();
            int y = ortoAux.getY();

            if (collisionRectangle.overlaps(planeRect)) {
                System.out.println("Collided : " + "(" + x + ";" + y + ")"); // All positions are collinding FIX NEEDED 
            }

        }

    }

....

Are you using more than one tile sheet in TileD?

hey!!! thanks for the reply :smiley:
More than one Tile Sheet?

Well right now im using a grass.jpg 32x16 and redCollision32x16
Also two layers, one for collision which i use the redCollision Tiles and one for Grass, which i use grass.jpg.

And just one .tmx file :stuck_out_tongue:

So each tile sheet is just one image? Or are they combined in one image?

im using two different images.
One Jpeg called grass.jpeg and one Jpeg called redsomething.jpeg

X / Y position is not the actual coordinate where it is drawn rather it is the position. Does that help?

yes!
But, im having a hard time trying to find where is the pos that is being drawn.

I was able after some time to move the camera properly and make the tmx being drawn at (0,0), i mean, it starts the drawing at 0,0;

Heres a SS of Tiled

http://img189.imageshack.us/img189/8700/tiledtwotiles.jpg

public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private Rectangle planeRect;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;
    private ArrayList<OrtoPoint> collisionPoints = new ArrayList<>();
    private BitmapFont bf;

    @Override
    public void create() {
        batch = new SpriteBatch();

        //Plane
        plane = new Texture(Gdx.files.internal("plane16.jpg"));
        planeSprite = new Sprite(plane);
        xplane = 10;
        yplane = 10;
        planeRect = new Rectangle(xplane, yplane, 16, 16);
      
        
        //BitmapFont
        bf = new BitmapFont();
        bf.setColor(Color.MAGENTA);


        //MapWorkout
        map = new TmxMapLoader().load("GrassLevelTwo.tmx");
        mr = new OrthogonalTiledMapRenderer(map,1f);

        //Camera 
        camera = new OrthographicCamera(500, 500);
        //camera.setToOrtho(false,32,32);
        
        camera.position.set(500 / 2, 500 / 2, 0);
        camera.update();

        try {
            mapTheCollision(map);
        } catch (Exception ex) {
            Logger.getLogger(AlphaColourLibgdx.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

You could multiply the x position by the width of tiles and y position by height of tiles to get an accurate position of the tiles on screen

Yay it worked.
I had tried that before but, i did with another Rectangle and was bugging, now it did really worked :stuck_out_tongue:

I have a question, if you would not be troubled to answer…
What id i had drawn the tmx in 200,200 or 300,300 , in another x,y position?

How would i calculate the tile positions then ?

  for (int x = 0; x < layer.getWidth(); x++) {
            for (int y = 0; y < layer.getHeight(); y++) {
                TiledMapTileLayer.Cell cell = layer.getCell(x, y);

                if ((cell != null) && (cell.getTile() != null)) {
                    System.out.println("ID=" + cell.getTile().getId() + " " + "x=" + x * tileWidth + ";" + "y=" + y * tileHeight);
                    collisionPoints.add(new OrtoPoint(x * tileWidth, y * tileHeight, tileWidth, tileHeight));
                    tileQuantity++;
                }
            }
        }
 for (int k = 0; k < collisionPoints.size(); k++) {
            OrtoPoint ortoAux = collisionPoints.get(k);
            
            Rectangle collisionRectangle = ortoAux.getCollisionRectangle();
            
            int x = ortoAux.getX();
            int y = ortoAux.getY();

            planeRect = new Rectangle(xplane, yplane, plane.getWidth(),plane.getHeight());
            
            if(planeRect.overlaps(collisionRectangle))
            {
               System.out.println("Overlaps!!" + System.currentTimeMillis());  
            }
            
            
            if (xplane == x && yplane == y) {
                System.out.println("X;Y Collision" + x);
                bf.draw(batch,"X",xplane,yplane);
            }

         


        }
        

Just add the offset to the result of the position as returned by

[quote]You could multiply the x position by the width of tiles and y position by height of tiles to get an accurate position of the tiles on screen
[/quote]
So:


	int tileWidth = 16;
	int tileHeight = 32;
	
	/**returns a vector of cordinate based tile (not in map units)
	 * @param tileX x position of the tile. in map units
	 * @param tileY y position of the tile. in map units
	 * @param offsetX offset based off x cordinate
	 * @param offsetY offset based off of y codinate
	 * @return
	 */
	public Vector2 position(int tileX, int tileY, int offsetX, int offsetY){
		int x, y;
		
		x = tileX * tileWidth + offsetX;
		y = tileY * tileHeight + offsetY;
		
		return new Vector2(x, y);
	}

should work for your task

ah, so im supposed to know that offset right? because the mr.render() dont give the x,y position to draw.

You can definitely set the position of the map. I don’t exactly remember how, but the map will be drawn at 0,0 if you don’t set coordinate.

hm, idk how to get that working.

The only parameter that i can set is the camera.

Not sure if thats how to change it.

https://code.google.com/p/libgdx-users/wiki/Tiles#Render

tmp.set(x,y);

that should help, just spend time looking into this, it won’t be hard to find

Jesus, how did you find that?
I love yOU!!!

I looked for that in everywhere!

Just googled “tilemap set position libgdx”

i had googled it :

tmx position libgdx

or

tile position libgdx

No wonder i wasnt finding :stuck_out_tongue:

Well, you got it now. Goodluck with your game