I have a tiled map in libgdx and i am implementing the GestureDetector pan method in my class.
What i want to happen is when the user uses the pan method the screen is dragged to that direction, kind of like the user is moving the tiled map with his finger.
When I test the code in my desktop, the dragging is perfectly fine, the map is dragged at the perfect speed and smoothness.
But when i test the same code on different android devices, the dragging of the is either too slow, or too fast. Sometimes a small finger movement moves the map across the whole screen.
How can i make the pan method consistent on all devices? My code:
public class SinglePlayerGame extends GestureDetector.GestureAdapter implements Screen{
private TiledMap map;
private OrthogonalTiledMapRenderer mapRenderer;
private OrthographicCamera camera;
private Viewport viewport;
private Main game;
public SinglePlayerGame(Main game){
this.game = game;
this.camera = new OrthographicCamera();
this.viewport = new StretchViewport(Main.VIRTUAL_WIDTH, Main.VIRTUAL_HEIGHT, camera);
this.map = new TmxMapLoader().load("map_1.tmx");
this.mapRenderer = new OrthogonalTiledMapRenderer(map);
this.camera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0);
Gdx.input.setInputProcessor(new GestureDetector(this));
resize(Main.VIRTUAL_WIDTH, Main.VIRTUAL_HEIGHT);
}
public boolean pan(float x, float y, float deltaX, float deltaY){
camera.translate(-deltaX, deltaY);
camera.update();
return false;
}
@Override
public void render(float delta){
update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.end();
mapRenderer.render();
}
public void update(){
camera.update();
mapRenderer.setView(camera);
}
@Override
public void resize(int width, int height){
viewport.update(width, height);
}
public void show(){}
public void pause(){}
public void resume(){}
public void hide(){}
@Override
public void dispose(){
}
GestureDetector units are in screen pixels, so you need to scale them to your world units, as defined by your camera and viewport:
public boolean pan(float x, float y, float deltaX, float deltaY){
float scaleX = viewport.getWorldWidth() / (float)viewport.getScreenWidth();
float scaleY = viewport.getWorldHeight() / (float)viewport.getScreenHeight();
camera.translate((int)(-deltaX * scaleX), (int)(deltaY * scaleY));
camera.update();
return false;
}