Advertisement

Tile picking with moving camera

Started by September 04, 2015 12:38 AM
0 comments, last by jHaskell 9 years, 4 months ago

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");
        }
    }

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-

This topic is closed to new replies.

Advertisement