【问题标题】:How do I create an orthographic camera that correctly scales my sprites/textures with libgdx?如何创建一个使用 libgdx 正确缩放我的精灵/纹理的正交相机?
【发布时间】:2011-10-15 21:11:04
【问题描述】:

我正在使用 libgdx 创建一个游戏,我想在桌面上以更高的分辨率运行它,但是当我在 android 上以较小的分辨率运行它时,我希望它能够正确地缩小所有内容。我读过最好的方法是不使用像素完美的相机,而是使用世界坐标,但我不确定如何正确地做到这一点。

这是我现在的代码:

@Override
public void create() {
    characterTexture = new Texture(Gdx.files.internal("character.png"));
    characterTextureRegion = new TextureRegion(characterTexture, 0, 0,  100, 150);
    batch = new SpriteBatch();


    Gdx.gl10.glClearColor(0.4f, 0.6f, 0.9f, 1);

    float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
    camera= new OrthographicCamera(aspectRatio, 1.0f);

}

@Override
public void render() {
    GL10 gl = Gdx.graphics.getGL10();

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    camera.update();
    camera.apply(gl);
    batch.setProjectionMatrix(camera.combined);

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    draw();
}

private void draw() {
    //batch.getProjectionMatrix().set(camera.combined);
    batch.begin();

    batch.draw(characterTextureRegion, 0, 0, // the bottom left corner of the box, unrotated
            1f, 1f, // the rotation center relative to the bottom left corner of the box
            0.390625f, 0.5859375f, // the width and height of the box
            1, 1, // the scale on the x- and y-axis
            0); // the rotation angle


    batch.end();
}

我使用的纹理是 256x256,其中的实际图像是 100x150。

这是我运行游戏时得到的结果:http://i.imgur.com/HV9Bi.png

被渲染的精灵很大,考虑到这是原始图像:http://i.imgur.com/q1cZT.png

最好的方法是让精灵以原始大小渲染,同时在以不同分辨率播放时仍保持游戏正确缩放的能力?

我只找到了两个解决方案,这两个我都不喜欢。

  1. 如果我为相机使用像素坐标,图像会显示它应该如何,但是当我将它放在具有不同分辨率的手机上时,它根本没有缩放。
  2. 我可以在绘制纹理区域时缩小它,但似乎有更好的方法,因为试图找出正确的数字来缩放它非常繁琐。

【问题讨论】:

  • 尝试使用 FitViewport 类

标签: android libgdx


【解决方案1】:

您曾经使用过 Libgdx 设置工具吗?当您使用它创建项目时,它会显示一个示例图像。无论您将屏幕更改为何种尺寸,它似乎都能保持其比例正确。

public class RotationTest implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
Stage stage;
public boolean leonAiming = true;

@Override
public void create() {      
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = new OrthographicCamera(1, h/w);
    batch = new SpriteBatch();

    texture = new Texture(Gdx.files.internal("data/libgdx.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);

    sprite = new Sprite(region);
    sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
    sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
    sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);   }....

@Override
public void render() {      
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    sprite.draw(batch);
    batch.end();

【讨论】:

  • 哈哈,刚看到这是2011年的,猜猜你现在已经找到答案了,或者停止开发了
  • 我已经尝试过这个实现,但它没有绘制我的纹理。知道为什么吗?
【解决方案2】:

首先,您需要确定世界的界限(我的意思是您的游戏)。在那个世界里,只有你的演员(游戏角色)应该玩。如果您要跨越边界,请使用摄像头进行管理,例如向上、向下、向左和向右显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2020-12-02
    • 2021-06-22
    • 2021-07-30
    相关资源
    最近更新 更多