Libgdx 2D Game, Random generated World of random size, how to get mouse coordinates?
- by Solom
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.
One of my naive approaches was this:
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!