Reputation: 41
I am creating a game with an isometric perspective using LibGdx. My code is based on this example: Isometric Tile Picking
I need to center a tile in the screen. I tried with camera.lookAt() but it does not work.
public boolean tap(float screenX, float screenY, int count, int button) {
touch.set(screenX, screenY, 0);
cam.unproject(touch);
touch.mul(invIsotransform);
pickedTileX = (int) touch.x;
pickedTileY = (int) touch.y;
cam.lookAt(pickedTileX, pickedTileY, 0);
Any Idea? Thanks!
Upvotes: 1
Views: 948
Reputation: 41
I've finally found a solution to my problem by combining the response of Basim Khajwal with the same formula used in the render map algorithm.
float x_pos = (pickedTileX * tileWidth / 2.0f) + (pickedTileY * tileWidth / 2.0f);
float y_pos = -(pickedTileX * tileHeight / 2.0f) + (pickedTileY * tileHeight / 2.0f);
touch.set( x_pos, y_pos, cam.position.z);
cam.position.set( touch.x , touch.y, cam.position.z);
Now I am able to center any tile, this works well even rotating and zooming my scene. (I am not sure why Basim's method have not worked well, it makes sense).
Thank you Basim very much.
Upvotes: 0
Reputation: 936
The reason your code doesn't work is because pickedTileX
and pickedTileY
are both in world co-ordinates where each tile is 1 unit in width and height in an orthogonal grid.
To center the camera towards the tile you are looking at, you don't actually need to find out which tile was clicked. You only need the world coordinate of the point and the screen size. The following code should do the trick:
public boolean tap(float screenX, float screenY, int count, int button) {
touch.set(screenX, screenY, 0);
cam.unproject(touch);
//Centralise the camera around the touch point
cam.position.set(touch.x - cam.viewportWidth / 2,
touch.y - cam.viewportHeight / 2,
cam.position.z);
What you were doing previously using the lookAt
method simply changed the direction of the camera without translating it.
Upvotes: 1