为了账号安全,请及时绑定邮箱和手机立即绑定

LibGdx Box2d 和 Scene2d 上不同设备之间的缩放

LibGdx Box2d 和 Scene2d 上不同设备之间的缩放

慕莱坞森 2023-07-28 10:09:33
我正在使用 Scene2d 和 Box2d 来处理 LibGdx。我使用 Scene2D 设计了 2 个阶段。第一个阶段用于 GUI,例如按钮,第二个阶段包含带有固定到我的 Box2d 对象坐标的图像的 Actor(在我的例子中是简单的矩形):当我在 PC 上运行游戏时,我得到以下图像:当我在 Galaxy S9+ 上运行它时,我收到以下图像:正如您所看到的,背景和 box2d 对象在 PC 和 Android 上都能正确缩放。问题是,与 Android 相比,我的演员图像在 PC 上发生了位移(牛仔图像)。我缩放 Box2D 以获得更好的物理效果,但从那时起我在跨平台缩放对象时遇到了麻烦。代码:    //My base screen, other screens extend this one:    public BaseScreen(){    Box2D.init();    mainStage = new Stage();    uiStage = new Stage();    manager = new AssetManager();    debug= new Box2DDebugRenderer();    world = new World(new Vector2(0,0),false);    loadAssetmanager();    //Box2d Sprites are initialzed in here:    initialize();    camera= new OrthographicCamera();    camera.setToOrtho(false,1024,1024);    debugMatrix= camera.combined.cpy();    debugMatrix.scale(32,32,0);    mainStage.getViewport().setCamera(camera);    camera.update();    //This BaseScreen class will initialize all assets from its subclasses here.    setMultiplexer();    }背景:我是一名住院医师,自学爪哇语。我正在尝试为我的医学生设计一个小型模拟,我希望他们能够在个人电脑、手机或平板电脑上访问它。我需要基本的 Box2D 物理(我没有时间在每周工作 40-70 小时的情况下编写自己的物理引擎),因此 LibGdx 和 Box2d 对我来说是理想的框架。任何意见将不胜感激。
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞


这是一个简单的。我初始化了 mainStage() 并且没有向它传递视口。因此 LibGdx 创建了自己的默认视口。当我查看 LibGdx 中 Stage() 类构造函数的源代码时,我发现默认的 Viewport 是一个设置为 Gdx.graphics.getWidth()、Gdx.graphics.getHeight() 的 ScalingViewport,这会读取每个设备的宽度/高度并以不同的方式缩放它:


    /** Creates a stage with a {@link ScalingViewport} set to {@link 

        Scaling#stretch}. The stage will use its own {@link Batch}

    *   which will be disposed when the stage is disposed. */

 public Stage () {

    this(new ScalingViewport(Scaling.stretch, Gdx.graphics.getWidth(), 

    Gdx.graphics.getHeight(), new OrthographicCamera()), new SpriteBatch());

    ownsBatch = true;

  }

因此,为了解决该问题,我更改了以下代码,而不是:


mainStage = new Stage();

我将代码更改为,以便每个设备都会缩放到相同的宽度/高度:


 mainStage = new Stage(new StretchViewport(1024,1024));

此更改基本上将视口设置为 1024x1024 像素,现在可以在我的 PC、Galaxy S9+/其他设备上正确缩放。


查看完整回答
反对 回复 2023-07-28
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信