如何创建一个正交相机来使用 libgdx 正确缩放我的精灵/纹理?
我正在使用 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
制作它的最佳方法是什么,以便精灵以原始大小渲染,同时仍然保持具有在不同分辨率下玩时游戏比例是否正确?
我只找到了两种解决方案,我都不喜欢。
- 如果我使用相机的像素坐标,该图像显示了它应该如何显示,但是当我将它以不同的分辨率放在手机上时,它根本没有缩放。
- 当我绘制纹理区域时,我可以缩小它,但似乎有更好的方法,因为尝试找出正确的数字来缩放它是非常乏味的。
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.
- 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.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用过 Libgdx 设置工具吗?当您使用它创建项目时,它会显示一个示例图像。无论您将屏幕更改为什么尺寸,它似乎都会保持正确的比例。
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.
首先,你需要确定世界的边界(我指的是你的游戏)。在那个世界里,只有你的演员(游戏角色)应该扮演。如果要跨越边界,请使用相机进行管理,例如向上、向下、向左和向右显示。
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.