I am trying to implement pan(float x, float y, float deltaX, float deltaY) from GestureDetector . I set the coordinates of the texture to the parameters of the pan , so whenever i drag the texture on screen it draws there . it worked perfectly .

Now i put a camera and a viewport in the screen and suddenly the the texture isn't following the drag coordinates properly .

here is the create() method where I initialise my camera and viewport

public void create () {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
    pos = new Vector2(0,0);

    camera = new OrthographicCamera();
    viewport = new StretchViewport(width,height,camera);
    gestureDetector = new GestureDetector(this);

and here is how i implement the pan method from the interface

public boolean pan(float x, float y, float deltaX, float deltaY) {
    pos.x = x ;
    pos.y = Gdx.graphics.getHeight() - y ;
    return true;

The code works fine when i am touching the top left corner of the screen where the Gesture detector considers the coordinate (0,0) but as I drift towards the right ,the texture drawn is somewhat ahead of the x coordinate being touched . It is working fine with respect to the Y axis .


2 Answers 2


I havent checked what you are trying to achieve but if you say it worked fine before you added a camera and viewport then it probably has something to do with screen vs world coordinates.

I believe what you are looking for is viewport.unproject() or camera.unproject(), this will transform screen coordinates into world coordinates and it should work properly.


unproject(Vector3 screenCoords)
Function to translate a point given in screen coordinates to world space.

source: Libgdx Camera Doc

  • \$\begingroup\$ I tried looking around about unprojecting but I am unable to get my head around it , It would be a huge help if you can please direct me to some resource or tell me how to use it and where . As I understand it , the screen is a part of the world we have shown as viewport using the camera ( by the projection matrix of the camera ) . -> difference between the screen and world coordinates ? any help is thoroughly appreciated :) \$\endgroup\$ Commented Jan 10, 2018 at 23:06
  • 1
    \$\begingroup\$ @satyamsinghal I added some stuff to help you out. Basically you feed it your screen coordinates in Vector format and it will spit out a translated vector which will have the correct world coordinates. \$\endgroup\$ Commented Jan 11, 2018 at 6:27

pos.add(deltax, -deltay) ;

X and y are current mouse position. Delta is how much it has been dragged.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .