Libgdx Box2d尺寸和相机缩放问题 [英] Libgdx box2d issue with dimensions and camera zooming

查看:158
本文介绍了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);

这是绘制圆圈的类的代码

Here is the code of the class that draw the circle

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尺寸和相机缩放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆