# LibGDX - Textures rendering at wrong position

Update 2:

Let me further explain my problem since I think that i didn't make it clear enough: The Y-coordinates on the bottom of my screen should be 0. Instead it is the height of my screen. That means the "higher" i touch/click the screen the less my y-coordinate gets. Above that the origin is not inside my screen, atleast not the 0 y-coordinate.

Original post:

I'm currently developing a tower defence game for fun by using LibGDX. There are places on my map where the player is or is not allowed to put towers on. So I created different ArrayLists holding rectangles representing a tile on my map. (towerPositions)

for(int i = 0; i < map.getLayers().getCount(); i++) {
curLay = (TiledMapTileLayer) map.getLayers().get(i);

//For all Cells of current Layer
for(int k = 0; k < curLay.getWidth(); k++) {
for(int j = 0; j < curLay.getHeight(); j++) {
curCell = curLay.getCell(k, j);

//If there is a actual cell
if(curCell != null) {
tileWidth = curLay.getTileWidth();
tileHeight = curLay.getTileHeight();
xTileKoord = tileWidth*k;
yTileKoord = tileHeight*j;

switch(curLay.getName()) {

//If layer named "TowersAllowed" picked
case "TowersAllowed":

// ... AND SO ON


If the player clicks on a "allowed" field later on he has the opportunity to build a tower of his coice via a menu. Now here is the problem: The towers render, but they render at wrong position. (They appear really random on the map, no certain pattern for me)

for(Rectangle curRect : towerPositions) {
if(curRect.contains(xCoord, yCoord)) {

//Using a certain tower in this example (left the menu out
if(gameControl.createTower("towerXY")) {

//RenderObject is just a class holding the Texture and x/y coordinates
}
}
}


Later on i render it:

game.batch.begin();
for(int i = 0; i < renderList.size() ; i++) {
game.batch.draw(renderList.get(i).myTexture, renderList.get(i).x, renderList.get(i).y);
}
game.batch.end();


regards

This is a well-known feature of libGDX: the Y-axis has 0 at the bottom of the screen, not the top. (This is to be more consistent with 3D coordinate systems, which do likewise.)

There are a couple of ways to "fix" this.

1. Use a camera. You can transform the view.
2. In your render method, calculate the Y position as screen.height - y - sprite.height. This is easier, and often, faster.

NB: I spent about two days trying the camera method last week, and it seems to draw inverted. I recommend the second option. You can get the screen height from Gdx.graphics.getHeight(), and the sprite height from the Texture instance.

• The Y-coordinate is at the bottom the height of the screen for me, not 0. – ACluelessGuy Jul 20 '14 at 14:57
• My problem is that that the y-coordinates behave like they were upside down (if i go up the y coords decrease). Above that the y-coordinate of 0 is not in the screen. The top still has a y-coordinate of 116 .. – ACluelessGuy Jul 20 '14 at 15:03
• Try my solution and see for yourself. If you want a second opinion, hit up Google and look for libgdx y-axis. – ashes999 Jul 20 '14 at 17:25
• First, thanks for your help :) But that was NOT my problem. Nevermind I figured out the problem, I wrote: xKoord = camera.unproject(touchPos).x; yKoord = camera.unproject(touchPos).y - The first operation already unprojects the whole cam, the 2nd unprojects it twice ... I hate those problems ._. How can i mark this as solved? – ACluelessGuy Jul 20 '14 at 17:28
• @ACluelessGuy click the button below to add an answer, document your own answer to the solution, and then click the grey checkbox next to your answer. – ashes999 Jul 21 '14 at 9:31