Stage not reporting correct viewport width

Anything libgdx related goes here!

Stage not reporting correct viewport width

Postby tamas305 » Sun Jan 02, 2011 10:28 pm

Code: Select all
this.stage = new Stage(480, 320, false);

I initialize stage as above but when I call

Code: Select all
stage.width();

It reports the width as being 533.33. This is putting my touch joystick completely out of whack.

My solution is a completely ghetto one

Code: Select all
stage.toStageCoordinates((int)(x/stage.width()*480), y, toConvert);

Is this a bug or am I doing something wrong?

Thanks
-Tamas
tamas305
 
Posts: 108
Joined: Sat Nov 06, 2010 1:51 pm

Re: Stage not reporting correct viewport width

Postby mzechner » Mon Jan 03, 2011 3:11 am

You specify false as the last parameter which will automatically extend the stage's size on devices that have a different aspect ratio than the size you specify. E.g. 480x320 has an aspect ratio of 1.5. On a 800x480 display (with an aspect ratio of 1.66) you have two options: either stretch horizontally, or adjust your viewport size to use more pixels horizontally, which is exactly the value you see (533). From the docs of the stage constructor

Code: Select all
        /**
         * <p>
         * Constructs a new Stage object with the given dimensions. If the device resolution does not equal the Stage objects
         * dimensions the stage object will setup a projection matrix to guarantee a fixed coordinate system. If stretch is disabled
         * then the bigger dimension of the Stage will be increased to accomodate the actual device resolution.
         * </p>
         *
         * @param width the width of the viewport
         * @param height the height of the viewport
         * @param stretch whether to stretch the viewport to the real device resolution
         */


So to solve your problem just specify true instead of false for the stretch parameter.
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: Stage not reporting correct viewport width

Postby tamas305 » Mon Jan 03, 2011 4:44 am

Ah beautiful...next time I should read before posting. :D

A somewhat related question...if I have something that was circular (like a virtual joystick) how would I prevent that from being deformed by SpriteBatch on phones that don't have the same aspect ratio as my viewport?

Thanks
-Tamas
tamas305
 
Posts: 108
Joined: Sat Nov 06, 2010 1:51 pm


Return to Libgdx

Who is online

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