# Libgdx 2D Game, Random generated World of random size, how to get mouse coordinates? [closed]

I'm a noob and English is not my mothertongue, so please bear with me!

I'm generating a map for a Sidescroller out of a 2D-array. That is, the array holds different values and I create blocks based on that value.

Now, my problem is to match mouse coordinates on screen with the actual block the mouse is pointing at.

public class GameScreen implements Screen
{
private static final int WIDTH = 100;
private static final int HEIGHT = 70;

private OrthographicCamera camera;
private Rectangle glViewport;

private Spritebatch spriteBatch;

private Map map;
private Block block;


...

@Override
public void show()
{
camera = new OrthographicCamera(WIDTH, HEIGHT);
camera.position.set(WIDTH/2, HEIGHT/2, 0);

glViewport = new Rectangle(0, 0, WIDTH, HEIGHT);

map = new Map(16384, 256);
map.printTileMap(); // Debugging only

spriteBatch = new SpriteBatch();
}

@Override
public void render(float delta)
{
// Clear previous frame
Gdx.gl.glClearColor(1, 1, 1, 1 );
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

GL30 gl = Gdx.graphics.getGL30();
//    gl.glViewport((int) glViewport.x, (int) glViewport.y, (int) glViewport.width, (int) glViewport.height);

spriteBatch.setProjectionMatrix(camera.combined);
camera.update();

spriteBatch.begin();

// Draw Map
this.drawMap();

//    spriteBatch.flush();
spriteBatch.end();
}

private void drawMap()
{
for(int a = 0; a < map.getHeight(); a++)
{
// Bounds check (y)
if(camera.position.y + camera.viewportHeight < a)// || camera.position.y - camera.viewportHeight > a)
break;

for(int b = 0; b < map.getWidth(); b++)
{
// Bounds check (x)
if(camera.position.x + camera.viewportWidth < b)// || camera.position.x > b)
break;

// Dynamic rendering via BlockManager
int id = map.getTileMap()[a][b];
Block block = BlockManager.map.get(id);

if(block != null) // Check if Air
{
block.setPosition(b, a);
spriteBatch.draw(block.getTexture(), b, a, 1 ,1);
}
}
}
}


As you can see, I don't use the viewport anywhere. Not sure if I need it somewhere down the road.

So, the map is 16384 blocks wide. One block is 16 pixels in size.

if(Gdx.input.isButtonPressed(Input.Buttons.LEFT))
{
Vector3 mousePos = new Vector3();
mousePos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(mousePos);

System.out.println(Math.round(mousePos.x)); // *16); // Debugging
// TODO: round
//   map.getTileMap()[mousePos.x][mousePos.y] = 2; // Draw at mouse position

}


I confused myself somewhere down the road I fear. What I want to do is, update the "block" (or rather the information in the Map/2D-Array) so that in the next render() there is another block. Basically drawing on the spriteBatch g

So if anyone could point me in the right direction this would be highly appreciated. Thanks!

## closed as off-topic by Anko, Kromster, Jesse Dorsey♦Dec 16 '14 at 18:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Anko, Kromster, Jesse Dorsey
If this question can be reworded to fit the rules in the help center, please edit the question.

• In summary, you'd like to convert your mouse position's pixel coordinates to tilemap coordinates. (Am I getting that right?) This reminds me of this answer that I wrote a while ago. The illustrations might be helpful here. – Anko Aug 17 '14 at 9:27
• @Anko Yup, basically. I just didn't think of it as a tilemap, as there are so many tiled-generated map-questions out there, so sorry. But in my case simply dividing by the tile-size doesn't work. That was the first thing I tried, so there must be something amiss with map <-> camera <-> mouse or I get something wrong :/ – Solom Aug 17 '14 at 10:23
• I'm pretty sure your draw call is using the wrong values. spriteBatch.draw(block.getTexture(), b, a, 1 ,1) should be spriteBatch.draw(block.getTexture(), b * 16, a * 16, 1 ,1), otherwise you're drawing to (0, 0), (1, 0), ... and blocks overlap. – ashes999 Aug 17 '14 at 10:27
• @ashes999: Thanks, you may be right. But if I do it like this, I can't scroll to the end of the map :/ I only get like a tenth of it or so :( PS: The textures didn't overlap, they were neatly adjacently placed. – Solom Aug 17 '14 at 10:29
• @Solom you need to separate your question into multiple, separate issues, including a) drawing properly and b) getting mouse coordinates. For the latter, look up the InputProcessor class, which has touch/click events. – ashes999 Aug 17 '14 at 13:41