简体   繁体   English

LibGdx-缩放Sprite,摄影机和视口

[英]LibGdx - Scaling Sprites, Camera and Viewports

Hell All & thanks for reading, 地狱全部,感谢您的阅读,

I recently started working on an 2D Android/Desktop project and have become stuck trying to display my sprites in the way i want. 我最近开始从事2D Android / Desktop项目的工作,试图以我想要的方式显示我的精灵时陷入困境。

I have a background Sprite that is 144(w) by 160(h) that I want to be able to position other sprites onto the screen relative to points on the background sprite. 我有一个背景精灵,它是144(w)x 160(h),我希望能够相对于背景精灵上的点将其他精灵放置到屏幕上。

I think I understand that if I create a camera/viewport that is 144 x 160 I would be able to position my sprites on the background sprite using the co-ordinates based on the 144 x 160 of the background sprite. 我想我知道,如果我创建一个144 x 160的摄影机/视口,则可以使用基于背景精灵的144 x 160的坐标将我的精灵放置在背景精灵上。 This will work across the different screen resolutions found on mobile devices but will stretch the background sprite despite experimenting with the different viewport types (FillViewport, FitViewport etc..). 尽管可以尝试使用不同的视口类型(FillViewport,FitViewport等),这将适用于移动设备上不同的屏幕分辨率,但可以拉伸背景精灵。

What I want to achieve is to have my background sprite to maintain it ratio across different screen resolutions and to be able to place other sprites over the background sprite. 我想要实现的是让我的背景精灵在不同的屏幕分辨率下保持其比率,并能够将其他精灵放置在背景精灵上。 The placing of sprite need to work across different resolutions. Sprite的放置需要在不同的分辨率下工作。

Apologies if my explanation is confusing or makes no sense. 抱歉,如果我的解释令人困惑或毫无意义。 I would add some image to help explain but I reputation to add any to the post. 我会添加一些图片以帮助解释,但是我很乐意在帖子中添加任何图片。 However I think the TLTR question is "What is the correct way to display sprites on multiple screen resolutions while keeping a correct ratios and scaling to the screen size and position of sprite in a way that works across multiple resolutions?" 但是,我认为TLTR问题是“在多种屏幕分辨率下显示子画面,同时保持正确的比例并以跨多种分辨率工作的方式缩放至屏幕大小和子画面位置的正确方法是什么?”

Thank, All Questions Welcome 谢谢,所有问题欢迎

A FitViewport would do what you described (maintain aspect ratio), but you will have black bars on some devices. FitViewport可以完成您描述的操作(保持宽高比),但是在某些设备上会出现黑条。 Based on the code you posted on the libgdx forum, I see that you forgot to update the viewport in the resize method, so it is not behaving as designed. 根据您在libgdx论坛上发布的代码,我发现您忘记了使用resize方法更新视口,因此它的行为与设计不符。

However, for a static camera game like what you described, I think the best solution would be to plan your game around a certain area that is always visible on any device, for example, the box from (0,0) to (144,160). 但是,对于像您描述的那样的静态相机游戏,我认为最好的解决方案是将游戏计划在任何设备上始终可见的特定区域周围,例如,框从(0,0)到(144,160) 。 Then use an ExtendViewport with width and height of 144 and 160. After you update the viewport in resize , you can move the camera to be centered on the rectangle like this: 然后使用宽度和高度分别为144和160的ExtendViewport。以resize更新视口后,可以将摄像机移动到矩形的中心,如下所示:

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 by putting true here would put (0,0) at bottom left of screen, but then the game rectangle would be off center

    //manually center the center of your game box
    Camera camera = viewport.getCamera();
    camera.position.x = GAME_WIDTH /2;
    camera.position.y = GAME_HEIGHT/2;
    camera.update();
}

Now your 144x160 box is centered on the screen as it would be with FitViewport, but you are not locked into having black bars, because you can draw extra background outside the 144x160 area using whatever method you like. 现在,您的144x160框就像在FitViewport中一样位于屏幕的中心,但是您并不会陷入黑条,因为您可以使用任意方法在144x160区域之外绘制额外的背景。

In your case 144:160 is a wider portrait aspect ratio than any screen out there, so you wouldn't need to worry about ever filling in area to the sides of your game rectangle. 在您的情况下,144:160的纵向宽高比比屏幕上的任何屏幕都要宽,因此您不必担心会在游戏矩形的侧面填充区域。 The narrowest aspect ratio of any phone or tablet seems to be 9:16, so you can do the math to see how much extra background above and below the game rectangle should be drawn to avoid black showing through on any device. 任何手机或平板电脑的最窄纵横比似乎都是9:16,因此您可以进行数学计算,以了解应在游戏矩形上方和下方绘制多少额外的背景,以避免在任何设备上显示黑色。

In this case it works out to 48 units above and below the rectangle that you would want to fill in: 在这种情况下,它可以算出要填充的矩形上方和下方的48个单位:

  • 144 pixels wide at 9:16 would be 256 tall. 在9:16时144像素宽将为256高。
  • (256 - 160) / 2 = 48 (256-160)/ 2 = 48

EDIT: I see from your post on the libgdx forum that you want the game area stuck at the top of the screen and the remainder of the area to be used for game controls. 编辑:我从libgdx论坛上的帖子中看到,您希望游戏区域停留在屏幕顶部,而该区域的其余部分则用于游戏控件。 In that case, I would change the resize method like this, since you want to have the game area's top edge aligned with the top edge of the screen. 在这种情况下,我将像这样更改resize方法,因为您希望游戏区域的顶部边缘与屏幕的顶部边缘对齐。 You can also calculate where the bottom of the controls area will be on the Y axis. 您还可以计算控件区域底部在Y轴上的位置。 (The top will be at Y=0.) (顶部将在Y = 0。)

public void resize(int width, int height){
    viewport.update(width, height, false); 

    //align game box's top edge to 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 you plan to do your controls, but they would need to fit in the box (0, controlsBottomY) to (GAME_WIDTH, 0) . 我不确定您打算如何进行控制,但是需要将它们放入(0, controlsBottomY)(GAME_WIDTH, 0)框中。 Keep in mind that there are some phones with aspect ratios as small as 3:4 (although rare now). 请记住,有些手机的宽高比低至3:4(尽管现在很少见)。 So with your 0.9 aspect ratio, on a 3:4 phone only the bottom 17% of the screen would be available for controls. 因此,使用0.9的宽高比,在3:4的手机上,只有屏幕底部的17%可供控件使用。 Which might be fine if it's just a couple of buttons, but would probably be problematic if you have a virtual joystick. 如果只是几个按钮,可能会很好,但是如果您有虚拟操纵杆,则可能会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM