Libgdx box2d issue with dimensions and camera zooming

99封情书 提交于 2019-12-01 08:34:37

问题


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:

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 is enabled, zoom is set to 0.125 and the squares you see are 10 units per side.

http://imgur.com/oGYvuNv

Here is the code of camera

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

回答1:


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);

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.




回答2:


you don't have to use the zoom to handle this here what i do and it works very well for me

camera = new OrthographicCamera(SCREEN_WIDTH/PPM, Config.SCREEN_HEIGHT/PPM);

// fix camera to the center of the screen
camera.position.set(Config.SCREEN_WIDTH/PPM/2f, Config.SCREEN_HEIGHT/PPM/2f, 0);

camera.update();

i put the conversion unit (PPM) to 100

hope this was helpful

Good luck



来源:https://stackoverflow.com/questions/34021355/libgdx-box2d-issue-with-dimensions-and-camera-zooming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!