简体   繁体   English

libgdx 和 box2d 中的相机

[英]cameras in libgdx and box2d

I develop 2D game using libGDX and box2D.我使用 libGDX 和 box2D 开发 2D 游戏。 Camera is following with body.I want to scroll background (png 3200X48) too.相机跟随身体。我也想滚动背景(png 3200X48)。 Now the camera works fine in box2D world but background is not moving.现在相机在 box2D 世界中工作正常,但背景没有移动。 I searched and tried many solutions but didn't help.我搜索并尝试了许多解决方案,但没有帮助。 What is the best solution for it?什么是最好的解决方案? Maybe makes two cameras one for physics world and second for libgdx scene?也许制作两个相机,一个用于物理世界,第二个用于 libgdx 场景? Has someone got the similar problem?有人遇到过类似的问题吗? Thanks for help.感谢您的帮助。 Here are a parts of my code这是我的代码的一部分

    float w = 320;
    float h = 480;

        @Override
    public void show() {
            camera = new OrthographicCamera();
        camera.setToOrtho(false, w/2, h/2);
        world = new World(new Vector2(0, -20), true);
        box2DDebugRenderer = new Box2DDebugRenderer();
    background = new Texture(Gdx.files.internal("gfx/forklift/background.png"));
    bg = new Sprite(background);
    ......}
    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    moveCamera(dynamicBody.getPosition().x);
     moveBody();
        batch.begin();
        batch.draw(background, 0, 0, 3200, 480);
        batch.end();
    Gdx.app.log("", ""+dynamicBody.getPosition());
        world.step(1/60f, 6, 2);
        box2DDebugRenderer.render(world, camera.combined);}

    public void moveCamera(float x){
        camera.position.set(x, dynamicBody.getPosition().y, 0);
        camera.update();
        }
    @Override
    public void resize(int w, int h) {
    camera = new OrthographicCamera(w, h);
        camera.translate(h/2, w/2, 0);
        super.resize(w, h);
    }

在 batch.begin() 之前使用 batch.setProjectionmatrix(camera.combined)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM