1

我用 libgdx 制作简单的游戏。我想添加 box2d 物理引擎。我有三个精灵,其中一个是动画的(动态身体)。其余的都是静态的。我不知道如何将它与 box2d 集成。有什么解决办法。我花了很多时间进行搜索,但一无所获。我的世界渲染器类:

public WorldRenderer(GameWorld world) {
    this.world = world;
    this.camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    this.camera.position.set(CAMERA_WIDTH/2f, CAMERA_HEIGHT/2f, 0);
    this.camera.update();
    spriteBatch = new SpriteBatch();
    loadTexture();
}

public void loadTexture() {
    TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("gfx/textures.atlas"));
    int indexr = 1;
    int indexl = 1;
    monkeyLeft = atlas.findRegion("left");
    monkeyRight = atlas.findRegion("right");
    platformTex = atlas.findRegion("platform");
    bananaTex = atlas.findRegion("banana");
    TextureRegion[] walkRightFrames = new TextureRegion[2];
    TextureRegion[] walkLeftFrames = new TextureRegion[2];
    for(int i=0;i<2;i++){
        walkLeftFrames[i] = atlas.findRegion("left", indexl++);
        walkRightFrames[i] = atlas.findRegion("right", indexr++);
    }
    goLeft = new Animation(RFD, walkLeftFrames);
    goRight = new Animation(RFD, walkRightFrames);
}

public void render(){
    spriteBatch.begin();
    drawPlatform();
    drawMonkey();
    drawBanana();
    spriteBatch.end();

}

public void drawMonkey() {
    Monkey monkey = world.getMonkey();
    monkeyFrame = monkey.isFacingLeft() ? monkeyRight :  monkeyLeft;
    if(monkey.getState().equals(Stan.WALK)){
        monkeyFrame = monkey.isFacingLeft()? goLeft.getKeyFrame(monkey.getStateTime(), true) : goRight.getKeyFrame(monkey.getStateTime(), true);    
    }
    spriteBatch.draw(monkeyFrame, monkey.getPosition().x*ppuX, monkey.getPosition().y*ppuY, Monkey.SIZE*ppuX, Monkey.SIZE*ppuY);
    }

public void drawPlatform() {
    for(Platform platform : world.getPlatforms()){
        spriteBatch.draw(platformTex, platform.getPosition().x*ppuX, platform.getPosition().y*ppuY, Platform.SIZE*ppuX, Platform.SIZEH*ppuY);
    }

}
public void drawBanana() {
    Banana banana = world.getBanana();
    spriteBatch.draw(bananaTex, banana.getPosition().x*ppuX, banana.getPosition().y*ppuY, Banana.SIZE*ppuX, Banana.SIZE*ppuY);

}
4

1 回答 1

1

创造你的世界。创建你的身体,进行你的模拟。并使用宽度、高度、身体位置和旋转角度来渲染它们,如下所示:

batcher.draw(animkeyframe, body.getPosition().x-width/2F, body.getPosition().y-height/2F, width/2, height/2, width, height, 1, 1, MathUtils.radiansToDegrees*body.getAngle());

有关 Box2d 的更多信息:Wiki 条目

我可以在您的 WorldRenderer 代码中看到大部分这些内容。只有步进模拟是不存在的。

于 2014-01-09T05:55:12.780 回答