Jump to content
  • Advertisement
Sign in to follow this  

Tile picking with moving camera

This topic is 1196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a isometric tilemap engine. Everything has been working fine up until recently when I tried implementing a moving camera. The tile picking has been off if the camera is moved. I've tried offsetting by the camera's displacement and it's still not giving the desired results. If I only move the camera along the x-axis, the tile picking works as intended, if any y-axis movement is involved, it doesn't behave correctly. I'm not sure what's going on here. Any help would be appreciated.


This method is called when a place on the game world is clicked.

There is no correction for the camera in this method at the moment, I took it out due to it not working correctly. It's simply the base method that worked before the moving camera was implemented.

The first two lines of code give me the camera's displacement along a given axis. cam.viewportWidth/2 is the default camera x coordinate. This is also applied to the Y-Axis.


If you need any more information, let me know and I can supply it.

    public void getTileAt(OrthographicCamera cam, Vector2 mouse){
        camOffsetX = cam.viewportWidth/2-cam.position.x;
        camOffsetY = -(cam.viewportHeight/2-cam.position.y);
        System.out.println(camOffsetX + ":" + camOffsetY);
        mouse.x -= X_OFFSET;
        mouse.y = Math.abs(mouse.y - Gdx.graphics.getHeight());
        Vector2 mp = isoToCart(mouse);
        Vector2 coords = new Vector2((float)Math.floor(mp.x), (float)Math.floor(mp.y));

           Tile t = new Tile("cube","redcube.png");
           layers.get(0).setTileAt(t, (int)coords.x, (int)coords.y);
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("Coords out of bounds");

Share this post

Link to post
Share on other sites

You should use the camera unproject method to convert the screen location clicked on by the mouse into a world location, then use that world location to pick the tile.  Don't do the math yourself when it's already been done for you.



Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!