Libgdx box2d 尺寸和相机缩放问题

Libgdx box2d issue with dimensions and camera zooming(Libgdx box2d 尺寸和相机缩放问题)

本文介绍了Libgdx box2d 尺寸和相机缩放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Box2D 有问题.我正在制作一个简单的 2D 游戏,玩家向敌人射击.我读到 Box2D 中的 1 个单位相当于 1 米,所以我应该使用小尺寸.我将世界尺寸设置为 160x90,但问题如下:

I've got a problem with Box2D. I'm making a simple 2D game, with a player shooting to enemies. I read that 1 unity in Box2D is equivalent to 1 meter so i should use little dimensions. I set world dimensions to 160x90 but here are the problems:

使用小尺寸时,我不得不缩放设置为 1280x720 的相机,但如您所见,形状质量大幅下降,而且身体的运动也不再流畅.而且即使世界步速为1/300,最高速度还是太慢了!

Using little dimensions i had to zoom my camera that is set to 1280x720 but as you can see there is a big drop of quality in shapes, and also the movements of bodies are no longer flowing. And the max speed is still too slow even if the world steps at 1/300!

Vsinc 已启用,缩放设置为 0.125,您看到的正方形是每边 10 个单位.

Vsinc is enabled, zoom is set to 0.125 and the squares you see are 10 units per side.

http://imgur.com/oGYvuNv

这是相机的代码

OrthographicCamera camera = new OrthographicCamera(64,32);

这是绘制圆的类的代码

public class JoyCircle implements GraphicalComponent {
public int radius;
public Vector2 position;
ShapeRenderer renderer;
Color color;
OrthographicCamera c;

public JoyCircle(int radius,Vector2 position,OrthographicCamera c,Color color) {
    this.c = c;
    this.color = color;
    this.radius = radius;
    this.position = position;
    renderer = new ShapeRenderer();
    renderer.setProjectionMatrix(c.combined);
}

@Override
public void update() {

}

@Override
public void draw() {
    renderer.begin(ShapeType.Filled);
    renderer.setColor(color);
    renderer.circle(position.x, position.y, radius);
    renderer.end();
}

}

Here is the code of the world draw
public void draw() {
    motionJoystick.draw(); //draws 2 instances of JoyCircle
    renderer.render(world, camera.combined);
}

推荐答案

不要使用 PPM,我知道你可能已经看过很多使用 PPM 的教程,而是使用虚拟像素或土豆像素用于相机和 box2d.在这里阅读这篇精彩的 文章 和这篇 答案.例如:

Don't use PPM, i know you might have seen a lot of tutorials using PPM but instead use virtual pixels or potato pixel for camera and box2d. Here read this wonderful article and this answer. For example:

OrthographicCamera camera = new OrthographicCamera(20, 11);

这里 20 是 WIDTH 的任意数字,HEIGHT 实际上应该是 WIDTH*Gdx.graphics.getHeight()/Gdx.graphics.getWidth().您不需要设置任何缩放级别,保持默认即可.

Here 20 is any arbitrary number for WIDTH and HEIGHT should be actually WIDTH*Gdx.graphics.getHeight()/Gdx.graphics.getWidth(). You don't need to set any zoom level, leave it to default.

这篇关于Libgdx box2d 尺寸和相机缩放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Libgdx box2d 尺寸和相机缩放问题

基础教程推荐