libgdx和Box2d同步相机移动时身体位置和身体不同

时间:2014-08-30 20:43:37

标签: libgdx box2d

好的家伙我知道这是一个容易回答的问题,我知道它只是一个概念或代码行远离解决我似乎无法弄清楚两个对象之间的相关性

我有一个身体和一个精灵,当相机没有移动时,它们完美排列并完美地移动但是我想以恒定的速度移动相机,当我这样做时,精灵和身体似乎相互脱离。

当我移动身体时,显然位置被更新,因此精灵的位置会更新,但是当相机移动而身体不是(它慢慢地移出屏幕)身体时#39 ; s的位置没有更新,所以即使身体向后移动,精灵仍然静止不动(我知道技术上身体根本不会移动它正在前进的相机哪个我完全明白了)

我的问题是,有什么属性/功能告诉身体,当它真正静止但只是相机正在移动时,它实际上正在移动。

如果您需要,可以使用以下代码。

public void render(float delta) {       
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    tiledMapRenderer.setView(camera);
    tiledMapRenderer.render();
    renderer.render(world,camera.combined); //DebugRenderer

    Array<Body> bodies = new Array<Body>(world.getBodyCount());
    world.getBodies(bodies);

    sb.begin(); //SpriteBatch
    for (Body body : bodies) {
        update(body);
    }
    sb.end();

    camera.position.x += .01f;
    camera.update();

    // Fixed timestep
    accumulator += delta;

    while (accumulator >= delta) {
        world.step(TIME_STEP, 6, 2);
        accumulator -= TIME_STEP;
    }       



}

  private void update(Body body) {


        CharacterUserData cud = (CharacterUserData)body.getUserData();
        cud.getSprite().setPosition((body.getPosition().x / Constants.WORLD_TO_BOX),(body.getPosition().y / Constants.WORLD_TO_BOX));
        cud.getSprite().setRotation(body.getAngle() * MathUtils.radiansToDegrees);
        cud.getSprite().draw(sb);
    }

我已经完成了一些谷歌搜索,似乎无法找到适合我的点击的正确位置。非常感谢您提供的任何帮助。

谢谢 -

0 个答案:

没有答案
相关问题