I have a problem with my Tiled Maps that i have loaded into my game following this guide; https://github.com/libgdx/libgdx/wiki/Tile-maps. All is fine and dandy, i have created a collisionsystem by following a youtube tutorial, pick the tile cell right, left, above or below you depending on which direction you are going, and then check the properties of that tile, if its blocked its blocked.
This works, but, its all scewed. The collision doesnt happen where it should on the map but at other positions, like if the map would actualy be larger then the rendered representation.
Here is a picture to illustrate; https://www.dropbox.com/s/k4mxqh2j2h92uo1/faultystuff.png
The player there is standing at position 8, 8(position.x /32, and position.y /32), that position in Tiled would show the player further down if i translate the position. But the actual position on the map is rather at 8, 12. The thing is, i can continue to walk up a bit and run into this blocked tile out in the void when the character is about to run into tile 8, 12. I have the same problem at other parts of the map, but it seems that it counts 4 tiles wrong, the center pillar is blocked about 3 tiles to the left as well, and the lower part there continues left and right a bit.
I have tried adding a scale as the wiki page does to the map renderer but that didnt solve anything since it flipped out and made the map ultra small.
I dont know the causes to problems like this. The map rendering is not different from the wiki, the camera is orthographic set to the width and height of the screen and follows the characters with a few problems. The collision is the following from the youtube tutorial;
float oldX = position.x, oldY = position.y;
boolean collisionX = false, collisionY = false;
// Move in X.
position.x = (position.x + velocity.x * Gdx.graphics.getDeltaTime() * SPEED);
// Check collision in X.
if(velocity.x < 0){ // going left
collisionX = collidesLeft();
} else if(velocity.x > 0) {// going right
collisionX = collidesRight();
}
// React to x collision
if(collisionX) {
position.x = oldX;
velocity.x = 0;
}
// Move in Y.
position.y = (position.y + velocity.y * Gdx.graphics.getDeltaTime() * SPEED);
// Check collision in Y.
increment = colLayer.getTileHeight();
increment = getHeight() < increment ? getHeight() / 2 : increment / 2;
if(velocity.y < 0) {// going down
collisionY = collidesBottom();
} else if(velocity.y > 0) {// going up
collisionY = collidesTop();
}
// React to y collision
if(collisionY) {
position.y = oldY;
velocity.y = 0;
}
}
private boolean isCellBlocked(float x, float y) {
Cell cell = colLayer.getCell((int) (x / colLayer.getTileWidth()), (int) (y / colLayer.getTileHeight()));
return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey("blocked");
}
public boolean collidesRight() {
/*for(float step = 0; step <= getHeight(); step += increment)
if(isCellBlocked(position.x + getWidth(), position.y + step)){
System.out.println("Right");
return true;}*/
if(isCellBlocked(position.x + getWidth(), position.y)){
System.out.println("Right");
return true;
}else return false;
}
/* there are 3 of these omitted due to being the same */