Hola! I’m having troubles with (I think) the LibGDX camera. I think it has something to do with pixels vs world units, but I’m really not sure. I have spent the last 2 days trying to research and figure this out, but it’s time for a question.
Here are the config dimensions:
// My tiles are 32 x 32 so these dimensions are multiples of 32.
config.width = 600;
config.height = 416;
Basically, I have a tilemap, which is created and rendered like so:
This designs the map:
/**
* Sample map. This map is the same size as the screen.
*/
public int[][] map =
{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0},
{2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 0, 0, 0, 2, 2, 2, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0},
{2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 0, 0, 0, 2, 2, 2, 2}
};
Here’s how the map is created and x, y positions are placed:
/**
* Creates and initializes tiles for map.
*
* @param MyGame myGame
* @param array map
*/
public void loadMap(MyGame myGame, int[][] map) {
int startX = 0;
int startY = 0;
int distance = 32;
int row = map.length - 1;
int col = map[0].length;
// Create our tiles depending on the given map's parameters.
for (int y = row; y > 0; y--) {
for (int x = 0; x < col; x++) {
if(map[y][x] == 1) {
myGame.tileLoader.createObjects(myGame, "tileTypeOne", startX, startY);
}
if(map[y][x] == 2) {
myGame.tileLoader.createObjects(myGame, "tileTypeTwo", startX, startY);
}
startX += distance;
}
startY += distance;
startX = 0;
}
And here is how the map is rendered:
/**
* Renders map.
*
* @param MyGame myGame
* @param array map
*/
public void renderMap(MyGame myGame, int[][] map) {
int row = map.length - 1;
int col = map[0].length;
for (int y = row; y > 0; y--) {
for (int x = 0; x < col; x++) {
for (int i = 0; i < myGame.tileLoader.tiles.size(); i++) {
myGame.tileLoader.tiles.get(i).drawTile(
myGame.renderer.batch,
myGame.tileLoader.tiles.get(i).getX(),
myGame.tileLoader.tiles.get(i).getY(),
myGame
);
myGame.tileLoader.tiles.get(i).rectangle.x = myGame.tileLoader.tiles.get(i).getX();
myGame.tileLoader.tiles.get(i).rectangle.y = myGame.tileLoader.tiles.get(i).getY();
}
}
}
}
Ok, up to this point works. The map is created and rendered correctly. I have a player object, with a circle for a hitbox. Also, all the tiles have a rectangle for a hit box. When the screen is stationary (not scrolling around) and the player is moving around the screen, this collision check works:
Vector2 point = new Vector2(myGame.gameObjectLoader.player.getX(), myGame.gameObjectLoader.player.getY());
for(int i = 0; i < myGame.tileLoader.tiles.size(); i++) {
if (myGame.tileLoader.tiles.get(i).rectangle.contains(point)) {
System.out.println("We have a collision");
}
}
However, when I change the arrow buttons to translate the camera as opposed to translating the player (so the player is always in the center of the screen), I’m not sure what’s going on with the tiles, as their x, y positions are all out of whack…so when the player is at 300, 300, and tile[0]'s position is identical, there is no collision being detected even though they’re right on top of each other. I have a feeling this has something to do with how I’m using the camera, so any help would be great. Here’s how I am doing the camera:
// Initialize
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
viewport = new ExtendViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Render
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
mapRenderer.renderMap(myGame, mapEditor.map);
batch.end();
/**
* Since these objects use a ShapeRenderer we must draw them after sprite batch has ended,
* otherwise they will block the sprite batch from being rendered.
*/
drawAdditionalObjectsOnGameScreenThatDontUseSpriteBatch();
And here’s how I move the camera:
f(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
camera.translate(1f,0f);
Let me know if I should add anymore information to this post. And thanks!