[SOLVED YAY ] Collision with .tmx ( wiki(ok) ) - Find (x,y) of a/all tiles

Err, hi. Sorry for another topic, but im really lost, and i cant move foward.

I researched many wiki, youtube…
I saw many deprecated stuff, and i was able to finnally draw that .tmx thing :D;

But, i have some questions, i was wondering if any can help me, i already researched and researched and researched and found nothing, nothing at all.

Ok, the question is, supposing that i have a plane in that position, how can i check for collisions near him?

I mean, how can i get all x,y tile positions?
I tried some methods, but i got nothing, all i got was some hexadecimal memory stuff, or i found the id,which doesnt help much ?

Anyway, all i need to know is how to get the X,Y positions of the tiles, must be really really simple/ Obvious, because i just cant find any info related.

Do i need to create properties at the program to get x,y ? idk

obs, im using “TILED” program.

public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;

    @Override
    public void create() {
        batch = new SpriteBatch();
        plane = new Texture(Gdx.files.internal("plane.jpg"));
        planeSprite = new Sprite(plane);

        xplane = 270;
        yplane = 610;


        map = new TmxMapLoader().load("GrassLevel2.tmx");
        
        
        //Check/Positions of 1 Tile!
        TiledMapTileSet tileSet = map.getTileSets().getTileSet("Grass");
        
        
        System.out.println(tileSet.getTile(1));
        
       
        //
        
        mr = new OrthogonalTiledMapRenderer(map);

        camera = new OrthographicCamera(512, 512);
        camera.position.x = 270;
        camera.position.y = 10;
        camera.update();

    }

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


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

    }

Load the layer into memory and iterate through x and y.

What I did with my pacman game is, make an extra invisible layer which determines which tiles are blocked, load the blockable layer into memory, then check the tile ID to see if you can pass through it.

Load the layer into memory and iterate through x and y ?

 map = new TmxMapLoader().load("GrassLevel2.tmx");
        
        
        //Check/Positions of 1 Tile!
        TiledMapTileSet tileSet = map.getTileSets().getTileSet("Grass");
        map.getLayers().get(0). ?
        

How can i do that ???

This doesnt work :

Figuring out what tile is at a location

You may need to know what tile is at a certain location, for example if the user touches it, or if you have actors with pathfinding and you need to know which tiles are traversible or not.

You can do this by accessing the appropriate tiles array for the layer you are interested in :

   int tileid = map.layers.get(0).tiles[y][x];
Note: here the y and x are swapped around. Also, the x and y is the coordinate of the tile in the map, not in the world or screen so you'll have to do some calculations.

Note 2: Also, just to confuse things more, the map is stored in a y-down coordinate system, whereas your screen and world coordinates are y-up. That's why in the example we subtract the y from the map height.

For example, to find the tile at a certain screen location (for a touch for example) you would :

I just found out, after changing the words in google a nice tutorial, explaining how to :smiley:

Was a topic on this forum.

So , i started working on it, and figuring out how things worked. It was all fine , except…
Except when i ask the print the tiles positions, it miss some tiles.
Idk why, its just jump a y or a x;

For instance, i dont have a tile at pos(2;2) (as the editor “TILED” says), but it does print Cell 2 :: 2 :: 2

What am i doing wrong???

package alphacolourlibgdx;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapRenderer;
import com.badlogic.gdx.maps.tiled.*;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author André Vinícius Lopes
 */
public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;

    @Override
    public void create() {
        batch = new SpriteBatch();
        plane = new Texture(Gdx.files.internal("plane.jpg"));
        planeSprite = new Sprite(plane);

        xplane = 270;
        yplane = 610;
        
        //FileHandle internal = Gdx.files.internal("grassLevelTide.tide");
        
        //map = new TideMapLoader().load("grassLevelTide.tide");
        map = new TmxMapLoader().load("GrassLevel3.tmx");


        mr = new OrthogonalTiledMapRenderer(map);

        camera = new OrthographicCamera(512, 512);
        camera.position.x = 250;
        camera.position.y = 10;
        camera.update();

        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) {
        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) {
                    continue; // There is no cell
                }
                if (cell.getTile() == null) {
                    continue; // No tile inside cell
                }

                if (cell.getTile() != null) {
                    System.out.println(cell.getTile().getId() + " :: " + x + ";" + y);

                }

                //System.out.println(cell.getTile().getId()); // Get the ID.

            }
        }
    }

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


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


    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

Ok, in tiled the y is inverted.
In Tiled the Cartesian Graphic starts at top left corner…

When you print with libgdx, it starts at bottom left corner.

Kinda stupid of me. But after a lot of debugging and thinking, i did it!!! :smiley:

Oh boy, jimmt will be kinda proud of me. :stuck_out_tongue: