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