我用 libgdx 制作简单的游戏。我想添加 box2d 物理引擎。我有三个 Sprite ,其中之一是动画的(动态主体)。其余的都是静态的。我不知道如何将其与 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);
}
最佳答案
创建您的world 。创建你的 body ,逐步进行模拟。并使用宽度、高度、主体位置和旋转角度渲染它们,如下所示:
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 Entry
我可以在您的 WorldRenderer 代码中看到大部分这些内容。仅步进模拟是不存在的。
关于java - 使用box2d制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18922489/