android - Screens, resolutions, cameras, viewports chaos -
i have read few articles resolutions, screens, viewports , cameras on mobile phone, more confused now, before. please me keep issue , handle it, working on mobile game, without success. using libgdx.
regarding answer below changed program (thanks explanation xoppa :)
new piece of code:
@override public void create () { orthographiccamera = new orthographiccamera(); fillviewport = new fillviewport(960, 600, orthographiccamera); orthographiccamera.position.set(orthographiccamera.viewportwidth * 0.5f, orthographiccamera.viewportheight * 0.5f, 0); fillviewport.apply(); }
.
@override public void render () { gdx.gl.glclearcolor(0.22f, 0.22f, 0.22f, 1); gdx.gl.glclear(gl20.gl_color_buffer_bit); ... }
.
@override public void resize (int width, int height) { fillviewport.update(960, 600); orthographiccamera.position.set(960 * 0.5f, 600 * 0.5f, 0); }
but result same.
output: 2 small dots players. :(
even if change viewport resolution, size of players not change. thing change visible resolution of viewport, behind not see mz players. sketeched better imagination (values imagination).
physics body of players:
public character(vector2 startposition) { bodydef bodydef = new bodydef(); bodydef.type = bodytype.dynamicbody; bodydef.position.set(startposition); // create our body in world using our body definition body = physic.gameworld.createbody(bodydef); // create circle shape , set radius 6 circleshape circle = new circleshape(); circle.setposition(new vector2()); circle.setradius(0.39f); // create fixture definition apply our shape fixturedef fixturedef = new fixturedef(); fixturedef.shape = circle; fixturedef.density = 1f; // create our fixture , attach body body.createfixture(fixturedef); circle.dispose(); }
even if change radius of circle, body need more energy manipulate , body not big enough. of course can not set radius value more 10f box2d doc not recommended it.
but not see anything, when run it, or created physical world objects small or flattened (physical world configuration , initialization think; radius of circle physical objects 0.39). or missing in code, statements or else?
but think have problem correct understanding of mentioned issues.
could please me or explain it?
first make sure understand camera , viewport , does. perhaps example might help:
imagine you're in park , take photo camera/smartphone of tree , bench. bench e.g. 3 meters wide , half meter in height , depth. tree might e.g. half meter wide , 10 meters in height. if @ photo on screen of camera/smartphone, you'll see bench no longer 3 meters wide, instead few millimeters wide. actual size depends on how you've setup camera (e.g. zoom) resolution of photo (in pixels) , density of screen you're watching on.
so practically in example above bench , tree have 2 different sizes: actual size in park in physical world , size on screen you're watching photo on. of course tree , bench don't shrink depending on photo, stay same size. size on photo size of projection of tree , bench.
projection practically transforming world objects onto screen, based on various camera settings (like zoom, position, etc.).
the park bigger portion you've taken photo of. when took photo decided portion of park (the physical world) want project onto photo. let's call park's viewport.
likewise don't have infinite storage project photo, you'll have define portion (the resolution of photo) want project onto. let's call photo's viewport.
the park's viewport expressed in real world units, meters or inches e.g. photo's viewport expressed in pixels (the resolution you've set to).
when making game typically aren't making photo rendering screen. therefor photo's viewport called screen viewport. , when making game park might not exist, game world virtual. therefor park's viewport called virtual viewport.
on small side-note: pixels integers, therefor screenviewport expressed in int
. world units meters or inches can fractional, therefor virtualviewport expressed in float
.
the camera
class of libgdx described above, performs the projection of virtual world onto screen. however, in practice comes problems. e.g. not every screen has same aspect ratio. therefor need define how want cope differences in aspect ratio. e.g. add black bars, expand virtual world or stretch it, etc.
the viewport
class of libgdx solves problem implementation various strategies can choose on how define virtualviewport , screenviewport. encapsulates (and manages) camera you.
in code you've given orthographiccamera
in constructor manage, typically used 2d games. virtualviewport you've set orthographiccamera
in constructor overwritten though. because you've chosen use screenviewport
. implementation makes virtualviewport (the portion of park) same screenviewport (the portion of screen).
it unlikely want use viewport implementation. instead want use e.g. fillviewport
:
public void create () { viewport = new fillviewport(50f, 50f); }
this creates virtualviewport grows if needed maintain aspect ratio. 50 50 in world units, e.g. meters or inches.
now need tell viewport screenviewport use. depends on size of screen of device , therefor best set in resize
method.
public void resize (int width, int height) { viewport.update(width, height); }
all other code of these methods of can removed, dont need them. dont need update camera in render method. can use viewport would. , if need access camera directly (but not modify it, read e.g. projection matrix), can use:
viewport.getcamera()
Comments
Post a Comment