[Solved]Actor.addListener maybe wrong on detect X, Y

Anything libgdx related goes here!

[Solved]Actor.addListener maybe wrong on detect X, Y

Postby tr4788 » Thu Aug 08, 2013 7:38 am

Sorry because I ask too much :oops:

I want to create game with screen size is 720x1280. And I want to build it on various Android device. If screen of device doesnt have same aspect ratio, it will have letterboxing (black bar on up, down, left and right).

Everything is ok till now, I already did that. In resize method, I used this code
Code: Select all
Vector2 size = Scaling.fit.apply(720, 1280, width, height);
int viewportX = (int) (width - size.x) / 2;
int viewportY = (int) (height - size.y) / 2;
int viewportWidth = (int) size.x;
int viewportHeight = (int) size.y;
this.viewport = new Rectangle(viewportX, viewportY, viewportWidth, viewportHeight);

then I change glViewport at render method

Code: Select all
Gdx.gl.glViewport((int)this.viewport.x, (int)this.viewport.y, (int)this.viewport.width, (int)this.viewport.height);


After that, i create a Stage and set stage viewport

Code: Select all
this.stage.setViewport(720, 1280, false);


I create 4 actor too, in class of actor, i set touch listener after set bound for it.

Code: Select all
this.setBounds(x, y - tb.height, tb.width, tb.height);

this.addListener(new InputListener() {
   public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
      // Do something
   }

   public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
      return true;
   }
});


But I have problem on Actor Touch Detection. When screen size is not same aspect ratio, actor touch detector is wrong

Here is 3 case.

Case 1: same ratio - everything is ok, touch event was fired when I touch to white rectangle (4 white rectangle are 4 actor)

Image

Case 2: different ratio - touch area is wrong, touch event was fired when I touch to white rectangle and yellow rectangle

Image

Case 3: different ratio - touch area is wrong, touch event was fired when I touch to yellow rectangle

Image

I think it is same problem of this topic: viewtopic.php?f=11&t=10341

But how to fix this, i think it is a bug :P

So sorry for my bad English.
Last edited by tr4788 on Mon Aug 12, 2013 3:00 am, edited 2 times in total.
tr4788
 
Posts: 9
Joined: Tue Aug 06, 2013 8:47 am

Re: Actor.addListener maybe wrong on detect X, Y

Postby evilentity » Thu Aug 08, 2013 8:21 am

Implementing letterbox is such pain in the ass. Checkout letterbox tests in gdx tests if you didnt:
LetterBoxTest1
LetterBoxTest2
LetterBoxTest3
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4833
Joined: Wed Aug 24, 2011 11:37 am

Re: Actor.addListener maybe wrong on detect X, Y

Postby NateS » Thu Aug 08, 2013 8:49 am

NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: Actor.addListener maybe wrong on detect X, Y

Postby tr4788 » Thu Aug 08, 2013 8:55 am

that test is old (setviewport method is different now) :( and it doesnt have anything about touch detection =.=

btw, i cloned libgdx repo but cant build it, alot of error, so i cant run test now

"Implementing letterbox is such pain in the ass"
tr4788
 
Posts: 9
Joined: Tue Aug 06, 2013 8:47 am

Re: Actor.addListener maybe wrong on detect X, Y

Postby tr4788 » Thu Aug 08, 2013 9:07 am

NateS wrote:https://code.google.com/p/libgdx/wiki/scene2d#Viewport


I already did that by that page, and it was OK till the Touch Detection part. See my example :( Letterbox are there, but have problem when touch to actor
tr4788
 
Posts: 9
Joined: Tue Aug 06, 2013 8:47 am

Re: Actor.addListener maybe wrong on detect X, Y

Postby soy_yuma » Thu Aug 08, 2013 10:33 am

NateS wrote:https://code.google.com/p/libgdx/wiki/scene2d#Viewport


Read carefully. Especially the part where it says:
If glViewport is not used then the viewport position and size can be omitted when calling setViewport. This will cause the stage to use the entire screen as the viewport.


I'm just guessing here, but I think you only have configured the OpenGL rendering viewport. This ensures that rendering is correct, but libGDX Stage does not know the right viewport. My guess is that the detection code depends on the viewport of Stage and since it is not properly configured, the detection areas don't correspond to the rendering areas.

I think it should be fixed by NateS' reference code:
Code: Select all
stage.setViewport(780, 1280, true, viewportX, viewportY, viewportWidth, viewportHeight);


I also think you could benefit from reading the documentation in the source code of Stage. Especially the documentation of the methods setViewport(). You can find the source code here.

Good luck and good reading!
soy_yuma
 
Posts: 3
Joined: Thu Feb 09, 2012 9:03 pm
Location: Madrid, Spain

Re: Actor.addListener maybe wrong on detect X, Y

Postby tr4788 » Thu Aug 08, 2013 11:06 am

From soy_yuma post ... I can figure out now, but...

tr4788 wrote:that test is old (setviewport method is different now) :( and it doesnt have anything about touch detection =.=

btw, i cloned libgdx repo but cant build it, alot of error, so i cant run test now

"Implementing letterbox is such pain in the ass"


OMG, so sorry, I did not clear about this bold words =.= Why I cant find that setViewPort method in my Project (I saw it on document and HERE. But cant implement it into my code

Image
Last edited by tr4788 on Mon Aug 12, 2013 3:01 am, edited 1 time in total.
tr4788
 
Posts: 9
Joined: Tue Aug 06, 2013 8:47 am

Re: Actor.addListener maybe wrong on detect X, Y

Postby tr4788 » Thu Aug 08, 2013 12:40 pm

Okay, im dumdass here.

I use 0.9.8 and that lib doesnt have that method.

I changed to nightly version and its ok now :shock:
tr4788
 
Posts: 9
Joined: Tue Aug 06, 2013 8:47 am


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot] and 1 guest