Reputation: 599
I make simple game with libgdx. I want to add box2d physics engine. I have three sprites and one of them is animated (dynamic bodie). The rest are static. I don't know how to integrate this with box2d. Is there any solution. I spent a lot of hours for searched but found nothing. My world renderer class:
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);
}
Upvotes: 1
Views: 1858
Reputation: 10320
Create your world. Create your bodies, step your simulation. And render them using the width, height, body position and rotate angle like this:
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());
For more info about Box2d: Wiki Entry
I can see most these things in your WorldRenderer code. Only stepping the simulation is not there.
Upvotes: 1