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