5

I have a button that I want to use on my screen. However when I resize my desktop window, the touch coordinates messes up itself. I have no idea how to solve this.

So first of all I'm using

cam = new PerspectiveCamera();
viewport = new FitViewport(screenWidth, screenHeight, cam);

and called

viewport.update(width, height, true);

in my resize method so that my screen would always look scaled proportional to its original size.

For my input

Gdx.input.setInputProcessor(new InputHandler((float) scaleX, (float) scaleY, world));

Now after resizing my window, the images are at correct places but the touch coordinates are not. It's kind of hard to explain but I will try my best. The scale is working properly. However, since I am using Fitviewport, when I call Gdx.graphics.getHeights() or .getWidth(), it's getting the height and width of the entire screen (including the black areas when I increase one side only). Thus, this makes my scale for my input proportional to the entire screen. While the images are being illustrated via Fitviewport and being resized proportional to it's original size. This makes my input position where the button responses to be too far left or right or up or down compared to the images that are drawn. I have also tried cam.unproject. How do I solve this?

I have some log provided in hopes that it will help you understand my situation better.

---not resized(original) ---

resize called
Resizing x : 480 y : 800
Scales x : 1.0 y : 1.0

(coordinates where the button originally is and where the button response to input)
Before Scaled X : 398 Y : 75
After Scaled X : 398 Y : 75
Gdx input X : 398 Y : 75
cam unproject X : 240.26144 Y : 400.53613

---after resizing---

resize called
Resizing x : 807 y : 743
Scales x : 1.68125 y : 0.92875

-where button appears on screen-

Before Scaled X : 542 Y : 38
After Scaled X : 322 Y : 40
Gdx input X : 542 Y : 38
cam unproject X : 240.13632 Y : 400.5924

-touch coordinate at which button response to-

Before Scaled X : 677 Y : 36
After Scaled X : 402 Y : 38
Gdx input X : 677 Y : 36
cam unproject X : 240.2692 Y : 400.59595

Thank you!

Hyunwoo Lim
  • 312
  • 3
  • 15
  • 1
    Remove the `true` from the update. – noone Aug 09 '14 at 19:58
  • @noone nope.. doesn't work – Hyunwoo Lim Aug 10 '14 at 00:49
  • Wait... you are talking about buttons, but you have a `PerspectiveCamera`, huh?? – noone Aug 10 '14 at 05:51
  • @noone yeah i have a perspective camera because i wanted my screen to look scaled to its original size even if i resize my window.. if you have a better idea please help me =( The reason that i showed that i have a perspective camera is because i think thats the issue thats causing my scales to go all wacky – Hyunwoo Lim Aug 10 '14 at 08:58
  • Don't use PerspectiveCamera, use OrthographicCamera with UI. Even with that you could "zoom". But for resizing for different screen sizes you should use `Viewport`. – noone Aug 10 '14 at 09:47
  • @noone I've tried OrthographicCamera but still doesn't work. I don't quite understand when you've said use it with UI. How do i initiate a camera within my UI class? and I've used perspectiveCamera because of this link https://github.com/libgdx/libgdx/wiki/Viewports. Also I've just tried ExtendViewport and it's working wonderfully... although I really wish I could use fitviewport and get the same effect =[ – Hyunwoo Lim Aug 10 '14 at 10:27
  • Have you found a solution to your problem? – Robin Ellerkmann Aug 25 '14 at 17:35
  • @RobinEllerkmann LOL i' still stuck but I've found the reason for it though I have no idea how to fix it. So, I've nested a private class implementing inputhandler to override some stuff. Apparently, Gdx.graphic.getWidht() in the private class returns correct value. However if I try something like... float x = getWidth() / 480, it always returns 1... PS. my config's width is 480. – Hyunwoo Lim Sep 12 '14 at 19:43

2 Answers2

0

I finally found my solutions to my question. The problem was that I did not know about float division.

link. I need to divide float/int. This float division explains well

thx for all the help tho

Community
  • 1
  • 1
Hyunwoo Lim
  • 312
  • 3
  • 15
  • 1
    please post your modified code..,I am also having same issue – salih kallai Oct 12 '16 at 16:11
  • @salihkallai there isn't much to post. It's just that when you do stuff with float and int, the float gets casted to int so the return type will be an int. This make the value lose its precision. Ex) Float + int = int :) – Hyunwoo Lim Oct 13 '16 at 23:05
0

I had a quite similar problem. In my touchDown method I translated the worldcoordinates with:

Vector3 testPoint3 = new Vector3();
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    testPoint3.set(screenX, screenY, 0);
    camera.unproject(testPoint3);
    testPoint.set(testPoint3.x * GameWorld.BOX_TO_WORLD, testPoint3.y * GameWorld.BOX_TO_WORLD);
    //And then check the Fixture
    if(myFixture.testPoint(testPoint.x, testPoint.y)) {
        System.out,println("hit");
    }
}

But the cameras width and height is, as you said, the whole screen width and height. So you need to unproject with the Viewport. viewport.unproject(testPoint3)

GameWorld.BOX_TO_WORLD ist 0.01f in my case, becaue I choose 1 meter = 100px.

KarlTheGreat
  • 142
  • 10