Orthographic Camera and Scene2d.ui behaviour

Anything libgdx related goes here!

Orthographic Camera and Scene2d.ui behaviour

Postby mourules » Fri May 25, 2012 10:13 pm

Hi all,

I'm using an orthographic camera to handle a device independent game graphics and menus.

My problem is that imagebutton and image, attached to an scene works ok displaying on the screen but not tables, windows and labels.

They have a weird behaviour as label and windows looks "huge" with a pixelated aspect (is like being extremelly near to the camera), and playing with width and height only reduces the size but not image resolution (gigant pixels!!!)

I believe I'm doing the things right as I'm doing stage.setCamera(camera) and Image and Imagebutton works ok with x,y, width and heigh. looks like window and labels has a different camera that is "extremelly close" to the object but at the same time coordinates are the actual ones upon camera projection. Keep in mind that label, window and imagebutton are all of them attached to the same scene and there is nothing with groups or so.

I'm spending weeks on this and the development of my game is heavily delayed to this.

I'm missing something that I can not get from docs, forums, wikis...please help me on this.
mourules
 
Posts: 29
Joined: Sat Feb 25, 2012 5:53 pm
Location: Madrid

Re: Orthographic Camera and Scene2d.ui behaviour

Postby mourules » Fri May 25, 2012 10:24 pm

if that helps, my Orthographic camera is defined like this (for box2D stuff porpouses):

Code: Select all
camera = new OrthographicCamera(48, 80);      
  camera.position.set(0, 40, 0);
mourules
 
Posts: 29
Joined: Sat Feb 25, 2012 5:53 pm
Location: Madrid

Re: Orthographic Camera and Scene2d.ui behaviour

Postby mzechner » Sat May 26, 2012 10:02 am

I assume you specified your UI widget bounds in pixels? In general one uses a separate camera for HUDs/UIs, for which you specify a different viewport than what you have for your game world rendering.
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: Orthographic Camera and Scene2d.ui behaviour

Postby mourules » Tue May 29, 2012 8:36 pm

Thanks a lot for your answer.

I would like to qualify my question as I believe is a problem easy to solve with a clear undestanding of how scene is build (is not my case).

The question is:

1) I want to create a device-independent policy for the app. I'm following this approach:

http://blog.acamara.es/2012/02/05/keep- ... ng-libgdx/

this is not perfect but is right for me for the moment.

2) I can not find my way setting up scene's viewport and camera to work with 1)

What currently I've is a mess between virtual resolutions, actual resolutions, rearangement of scene content and weird behaviour with the touch area vs the actual drawed are on imagebuttons and other actors

I believe solving 1) and 2) would be a great foundation for device-independent games using scene2d and for sure there is a way doing this out of there but I'm spending weeks and I can't find the way.

hopefully somebody fresher and younger can help!!!

kind regards
mourules
 
Posts: 29
Joined: Sat Feb 25, 2012 5:53 pm
Location: Madrid

Re: Orthographic Camera and Scene2d.ui behaviour

Postby mourules » Wed May 30, 2012 3:44 pm

I found my way!

The problem was on Scene constructor. After reviewing the source code, I realized that making something like this:

s = new Scene (VIRTUAL_WIDTH,VIRTUAL_HEIGHT, true);

bought me exactly what I was looking for. Now I've a fully functional Scene2D working in a device-independent with this approach:

http://blog.acamara.es/2012/02/05/keep- ... ng-libgdx/

The issue was on third parameter, stretching actually only works ok when is declared "true" that by the way do not stretch the screen: if you put false there is an endless number of implications that drove me crazy with different behaviors upon screen resolutions with a mess between Scene camera and openGl viewport transformations

after testing on a variety of devices ranging from 320x480 to 768x1280 works perfect with an almost not-noticeable cropping of several pixel horizontally or vertically, depending on the screen resolution, and keeping the aspect ratio using unique layouts and single high-dpi bitmaps.

I hope this can be useful for anybody stuck on multi-screen and looking for an straighforward solution to this problem.
mourules
 
Posts: 29
Joined: Sat Feb 25, 2012 5:53 pm
Location: Madrid

Re: Orthographic Camera and Scene2d.ui behaviour

Postby sergestusx » Mon Oct 15, 2012 8:39 pm

I have a doubt with this thing. I can get to make the screen scale to whatever resolution it is I'm executing the game on, but it ALWAYS stretches. the third parameter is called "stretch", but it internally goes directly to the "keepAspectRatio" parameter in the setViewport call inside the Stage class. So, it's the opposite. If I put "true" in there, I expect the screen to be stretched, but seeing the Stage class, I should expect it to keep the aspect ratio. And viceversa. The thing is: whatever I put in there, it always stretches the screen. I'm strying this resizing the desktop window.

Is there anyway I can work with an aspect ratio respectful/resolution independent viewport using Scene2d? Or maybe it's a bug...

Thanks!
sergestusx
 
Posts: 7
Joined: Wed Aug 08, 2012 6:18 am

Re: Orthographic Camera and Scene2d.ui behaviour

Postby mzechner » Mon Oct 15, 2012 11:40 pm

You have to tell the scene that the window resized. You should do so in ApplicationListener#resize(), calling stage.setViewport(width, height).
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: Orthographic Camera and Scene2d.ui behaviour

Postby NateS » Tue Oct 16, 2012 3:34 am

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

Re: Orthographic Camera and Scene2d.ui behaviour

Postby sergestusx » Tue Oct 16, 2012 9:56 pm

Thanks for your answers! Unfortunately, this doesn't seem to totally work. I'm missing something for sure. I'll explain:

I defined the setViewport call in the resize method as suggested:

Code: Select all
@Override
public void resize(int width, int height) {
stage.setViewport(width, height, true);
//...
}


but I'm defining a background image (Image class) that doesn't resize along with the viewport. I'm defining this like this:

Code: Select all
Texture texture = new Texture(Gdx.files.internal("background.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion tRegion = new TextureRegion(texture);
Image image = new Image(new TextureRegionDrawable(tRegion));
//already have tried the following without success:
Image image = new Image(new TextureRegionDrawable(tRegion), Scaling.fit);
Image image = new Image(new TextureRegionDrawable(tRegion), Scaling.none);
Image image = new Image(new TextureRegionDrawable(tRegion), Scaling.fillX);
Image image = new Image(new TextureRegionDrawable(tRegion), Scaling.fillY);


I do have tried to try using Gdx.gl.glViewport and it seems to do things better, but I understand that I should use the Scene2d API.

Do you know what could I be doing wrong and/or not taking into account?

Thanks in advance!
sergestusx
 
Posts: 7
Joined: Wed Aug 08, 2012 6:18 am

Re: Orthographic Camera and Scene2d.ui behaviour

Postby sergestusx » Tue Oct 16, 2012 10:29 pm

I must add that if I use the Gdx.gl.glViewport function with cropping (like mourules tells us), when I add actors to the scene such as the Image I described before, touch events do not work in the right place.

For example, if I set the Image on the right border of the scene (let's say x coordinate is stage.width - image.width - 10) and extend the window so that I get two black borders on each side, I have to click on the right of the right black border, even though the image is not there (not even the viewport).

Another thing I have noticed with this method is that with cropping, the black sides flicker.

I'm using GLES20, by the way.
sergestusx
 
Posts: 7
Joined: Wed Aug 08, 2012 6:18 am

Next

Return to Libgdx

Who is online

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