Sign in to follow this  

Tile picking with moving camera

This topic is 829 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));

                
        try{
           Tile t = new Tile("cube","redcube.png");
           layers.get(0).setTileAt(t, (int)coords.x, (int)coords.y);
            
            System.out.println(coords);
        }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.

 

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Camera.html#unproject-com.badlogic.gdx.math.Vector3-float-float-float-float-

Share this post


Link to post
Share on other sites

This topic is 829 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this