Please, I need your help. I'm trying to make a game that will drag-n-drop a sprite/image while my finger follows precisely with the image without being offset. When I'm trying on a 900x1280 (in X [900] and Y [1280]) screen resolution of the Google Nexus 7 tablet, it follows precisely. However, if I try testing on a phone smaller than 900x1280, my finger and the image won't aligned properly and correctly except it still dragging. This is the code I used for making a sprite dragging with my finger under touchDragged():

x = ((screenX + Gdx.input.getX())/2) - (fruit.width/2);
y = ((camera_2.viewportHeight * multiplier) - ((screenY + Gdx.input.getY())/2) - (fruit.width/2));

This code above will make the finger and the image/sprite stays together in place while dragging but only works on 900x1280.

You'll be wondering there's camera_2.viewportHeight in my code. Here are for two reasons: to prevent inverted drag (e.g. when you swipe with your finger downwards, the sprite moves upward instead) and baseline for reading coordinate...I think.

Now when I'm adding another orthographic camera named camera_1 and changing its setting, I recently used it for adjusting the falling object by meter per pixel. Also, it seems effective independently for smartphones that has smaller resolution and this is what I used here:


    camera_1 = new OrthographicCamera();
    camera_1.viewportHeight = 280; // --> I set it to a smaller view port height so that the object would fall faster, decreasing the chance of drag force. 
    camera_1.viewportWidth = 196;  // --> Make it proportion to the original screen view size as possible.
    camera_1.position.set(camera_1.viewportWidth * 0.5f, camera_1.viewportHeight * 0.5f, 0f);


    x = ((screenX + (camera_1.viewportWidth/Gdx.input.getX()))/2) - (fruit.width/2);
    y = ((camera_1.viewportHeight * multiplier) - ((screenY + (camera_1.viewportHeight/Gdx.input.getY()))/2) - (fruit.width/2));

But the result instead of just following the image/sprite closely to my finger, it still has a space/gap between the sprite/image and the finger. It is possibly dependent on coordinates based on the screen resolution. I'm trying to drag the blueberry sprite with my finger. My expectation did not met since I want my finger and the sprite/image (blueberry) to stay close together while dragging until I release it. Here's what it looks like:

enter image description here

I got to figure it out how to make independent on all screen sizes by just following the image/sprite closely to my finger while dragging even on most different screen sizes instead.

UPDATED QUESTION (as of 12/5/2012):

The camera_1.unproject(touchPoint) still not working (and confused in which order should I put) and that is because of the two other cameras under show() method. Also, I changed the viewport of camera_1 by 70x100 for falling object answer based on my previous question, " How to Make Objects Fall Faster in a Physics Simulation ". Take a look below:

    Mother_Camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

    // For background.
    background_camera = new OrthographicCamera();
    background_camera.viewportHeight = 1280;
    background_camera.viewportWidth = 900;
    background_camera.setToOrtho(false, background_camera.viewportWidth, background_camera.viewportHeight);

    // For fruits. (READ: Pixels per meter!)

     *     Take note that if you set it at higher value, it might be fall slower starting from the taller peak (e.g. buildings, mountains, space). 
     *  Based on physics, it was caused by drag force. Drag force causes any object to fall slower when you try to drop it from at its very tall 
     *  peak.

    camera_1 = new OrthographicCamera();
    camera_1.viewportHeight = 100; // --> I set it to a smaller view port height so that the object would fall faster, decreasing the chance of drag force. 
    camera_1.viewportWidth = 70;  // --> Make it proportion to the original screen view size as possible.
    camera_1.position.set(camera_1.viewportWidth * 0.5f, camera_1.viewportHeight * 0.5f, 0f);
    //camera_1.zoom = 100;

    // For HUD.
    camera_2 = new OrthographicCamera();
    camera_2.viewportHeight = 1280;
    camera_2.viewportWidth = 900;
    camera_2.setToOrtho(false, camera_2.viewportWidth, camera_2.viewportHeight);

Also, there is another problem why my sprite (blueberry) didn't align with my finger while dragging and was it because of using my code under resize() method? Take a look:

    // Ingredients Needed for Screen Aspect Ratio
private static final int VIRTUAL_WIDTH = 900;
private static final int VIRTUAL_HEIGHT = 1280;
private static final float ASPECT_RATIO = ((float) VIRTUAL_WIDTH)/((float) VIRTUAL_HEIGHT);
private Camera Mother_Camera;
private Rectangle Viewport;

// Ingredients for all Independent Coordinates for Touch Screen (Drag-n-Drop)
private float Y_VIEW;
private float X_VIEW;

public void resize(int width, int height) 
    // TODO Resize

    // Calculate new viewport.
    float adjust_screen = ((float)width) / ((float)height);
    float scale = 1.0f;
    Vector2 crop = new Vector2(0.0f, 0.0f);

    // Check for changes.
    if(adjust_screen > ASPECT_RATIO)

        scale = ((float)height) / ((float)VIRTUAL_HEIGHT);
        crop.x = 0;

    } else if(adjust_screen < ASPECT_RATIO){

        scale = ((float)width) / ((float)VIRTUAL_WIDTH);
        crop.y =  0;

    } else{

        scale = (float)width / (float)VIRTUAL_WIDTH;


    // Set new changes for the screen ratio.
    float w = (float)VIRTUAL_WIDTH*scale;
    float h = (float)VIRTUAL_HEIGHT*scale;

    // Set final changes for the screen view coordinates.
    crop.y = Gdx.graphics.getHeight() - h; // --> Set always on top.
    crop.x = (Gdx.graphics.getWidth()/2) - (w/2);

    // Set new changes for the touch screen drag-n-drop for precision in any screen resolution without distorting the alignment between the finger pointer and the object.
    X_VIEW = w;
    Y_VIEW = h;

    Viewport = new Rectangle(crop.x, crop.y, w, h);

Sorry for a little delay on how to put up by thinking what cause of the problem is. I'm still using @Majd's solution but I'm still in the problem yet. Is there anymore suggestions? Hope I didn't overwhelm my question a bit due to critical thinking and analysis.


1 Answer 1


Have you looked at the documentation for the Camera class? you can use Camera.unproject(Vector3 vec) to translate a point from touch input to camera coordinates, ie:

Vector3 touchPoint=new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
  • 1
    \$\begingroup\$ Where do I set it? At the touchDragged() method under input processor or at the onCreate()/show()? \$\endgroup\$ Commented Dec 4, 2012 at 7:42
  • 2
    \$\begingroup\$ you use it whenever you want to translate a window coordinate to a world coordinate. First you set up the camera in onCreate(). Then in the touchDown or touchDragged method you have something like: touchDragged(int x, int y, int pointer){ Vector3 touchPoint=new Vector3(x,y, 0); camera_1.unproject(touchPoint); } Since you are passing a reference to the touchPoint vector the transformed values are stored in the same vector. So you can just read the transformed coordinates with touchPoint.x, touchPoint.y. \$\endgroup\$
    – Majd
    Commented Dec 4, 2012 at 8:50
  • 1
    \$\begingroup\$ Wait a minute, @Majd...I think I almost get your point of your answer. I'll keep observing on the camera's documentary. \$\endgroup\$ Commented Dec 5, 2012 at 4:47
  • 1
    \$\begingroup\$ I think I got the problem. When I'm making a game, I created three cameras: one is for background scenery, one is for dragging the fruits, and the other one is for HUD. The two other cameras contain a viewport of 900x1280 and this camera contains 70x100. (See updated question) \$\endgroup\$ Commented Dec 5, 2012 at 9:45
  • 1
    \$\begingroup\$ Why did you use Vector 3 since I'm making 2D game? Plus, Vector 3 is used for 3D graphics. Can you explain why did you used that in your solution? \$\endgroup\$ Commented Dec 6, 2012 at 9:25

You must log in to answer this question.

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