如何创建一个正交相机来使用 libgdx 正确缩放我的精灵/纹理?

发布于 2024-12-10 06:34:42 字数 1826 浏览 1 评论 0原文

我正在使用 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。

这是我运行游戏时得到的结果: https://i.sstatic.net/y9T7o.png

考虑到这是原始图像,渲染的精灵很大:https://i.sstatic.net/0E0fh.png

制作它的最佳方法是什么,以便精灵以原始大小渲染,同时仍然保持具有在不同分辨率下玩时游戏比例是否正确?

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

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

I'm creating a game with libgdx that I want to run at a higher resolution on the desktop, but I want it to scale everything down correctly when I run it on android at smaller resolutions. I've read that the best way to do this is to not use a pixel perfect camera, and instead to use world coordinates, but I'm not sure how to correctly do that.

This is the code I have right now:

@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();
}

The texture I'm use is 256x256 with the actual image in it being 100x150.

This is the result I get when I run the game: https://i.sstatic.net/y9T7o.png

The sprite that gets rendered is massive, considering this is the original image: https://i.sstatic.net/0E0fh.png

What's the best way to go about making it so that the sprites get rendered at their original size while still keeping the ability to have the game scale correctly when played in different resolutions?

I've only found two solutions, both of which I don't like.

  1. The image showed up how it was supposed to if I used pixel coordinates for the camera, but then that didn't scale at all when I put it on my phone with a different resolution.
  2. I can scale the texture region down when I draw it, but it seems like there is a better way because it is extremely tedious trying to figure out the correct number to scale it by.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-12-17 06:34:42

您使用过 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();

Have you ever used the Libgdx setup tool? When you create a project with it, it has a sample image that is displayed. It seems to keep it's ratio correct no matter what size you change the screen to.

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();
执笏见 2024-12-17 06:34:42

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

First of all you need to fix boundaries to the world (I mean to your game ). In that world only you actors(game characters) should play. If you are crossing boundaries, manage it with camera like showing up, down, left and right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文