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():

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:

show()

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);
camera_1.update();

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:

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;
camera_1.update();
// 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:

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.

Where do I set it? At the touchDragged() method under input processor or at the onCreate()/show()?
–
David DimalantaDec 4 '12 at 7:42

2

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.
–
MajdDec 4 '12 at 8:50

1

Wait a minute, @Majd...I think I almost get your point of your answer. I'll keep observing on the camera's documentary.
–
David DimalantaDec 5 '12 at 4:47

1

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)
–
David DimalantaDec 5 '12 at 9:45

1

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?
–
David DimalantaDec 6 '12 at 9:25