java - LibGdx - Scaling Sprites, Camera and Viewports -
hell & reading,
i started working on 2d android/desktop project , have become stuck trying display sprites in way want.
i have background sprite 144(w) 160(h) want able position other sprites onto screen relative points on background sprite.
i think understand if create camera/viewport 144 x 160 able position sprites on background sprite using co-ordinates based on 144 x 160 of background sprite. work across different screen resolutions found on mobile devices stretch background sprite despite experimenting different viewport types (fillviewport, fitviewport etc..).
what want achieve have background sprite maintain ratio across different screen resolutions , able place other sprites on background sprite. placing of sprite need work across different resolutions.
apologies if explanation confusing or makes no sense. add image explain reputation add post. think tltr question "what correct way display sprites on multiple screen resolutions while keeping correct ratios , scaling screen size , position of sprite in way works across multiple resolutions?"
thank, questions welcome
a fitviewport described (maintain aspect ratio), have black bars on devices. based on code posted on libgdx forum, see forgot update viewport in resize method, not behaving designed.
however, static camera game described, think best solution plan game around area visible on device, example, box (0,0) (144,160). use extendviewport width , height of 144 , 160. after update viewport in resize
, can move camera centered on rectangle this:
private static final float game_width = 144; private static final float game_height = 160; public void create(){ //... viewport = new extendviewport(game_width, game_height); //... } public void resize(int width, int height){ viewport.update(width, height, false); //centering putting true here put (0,0) @ bottom left of screen, game rectangle off center //manually center center of game box camera camera = viewport.getcamera(); camera.position.x = game_width /2; camera.position.y = game_height/2; camera.update(); }
now 144x160 box centered on screen fitviewport, not locked having black bars, because can draw extra background outside 144x160 area using whatever method like.
in case 144:160 wider portrait aspect ratio screen out there, wouldn't need worry ever filling in area sides of game rectangle. narrowest aspect ratio of phone or tablet seems 9:16, can math see how background above , below game rectangle should drawn avoid black showing through on device.
in case works out 48 units above , below rectangle want fill in:
- 144 pixels wide @ 9:16 256 tall.
- (256 - 160) / 2 = 48
edit: see post on libgdx forum want game area stuck @ top of screen , remainder of area used game controls. in case, change resize method this, since want have game area's top edge aligned top edge of screen. can calculate bottom of controls area on y axis. (the top @ y=0.)
public void resize(int width, int height){ viewport.update(width, height, false); //align game box's top edge top of screen camera camera = viewport.getcamera(); camera.position.x = game_width /2; camera.position.y = game_height - viewport.getworldheight()/2; camera.update(); controlsbottomy = game_height - viewport.getworldheight(); }
i'm not sure how plan controls, need fit in box (0, controlsbottomy)
(game_width, 0)
. keep in mind there phones aspect ratios small 3:4 (although rare now). 0.9 aspect ratio, on 3:4 phone bottom 17% of screen available controls. might fine if it's couple of buttons, problematic if have virtual joystick.
Comments
Post a Comment