0
votes

I got a problem on Box2d I'm new on Box2d so couldnt handle it movement of my player. I want to move my player to the left and right when the user touch left and right buttons in my game. in my game player already going through the up y axis . I wanted see cool smooth animation while controlling player. I just couldnt move my player to the left how can I do that ? thanks in advance

//EDITED

I created a fixture for testing , I can move fixture but not my player. how can I attach my player sprite to body ? and I have to find a proper way to controlling body. it wont stop once you started. this is my codes

public World world;
public Body bplayer;
public Box2DDebugRenderer b2dr;
public Matrix4 cameraBox2D;

PlayScreen

buttonimage.addListener(new ClickListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
    {

    bplayer.setLinearVelocity(-5*PPM , 0);



    return true;
    }
});
world = new World(new Vector2(player.getPosition().x , player.getPosition().y) , false);
b2dr = new Box2DDebugRenderer();
bplayer = createPlayer(player.getPosition().x , player.getPosition().y);

show method

buttonimage.setPosition(160,0);
rightbuttonimage.setPosition(320,0);
pauseimage.setPosition(220,-20);
cameraBox2D = camera.combined.cpy();

Render method

Gdx.gl.glClearColor(0, 0, 2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


sb.setProjectionMatrix(camera.combined);

player.position.y += 500 * Gdx.graphics.getDeltaTime();
sb.begin();
sb.draw(bg, 0, camera.position.y - (camera.viewportHeight/2));
sb.draw(player.sprite, player.getPosition().x , player.getPosition().y);
for (Tube tube : tubes) {
    sb.draw(tube.getlefttube(), tube.getposlefttube().x, tube.getposlefttube().y);
    sb.draw(tube.getrighttube(), tube.getposrighttube().x, tube.getposrighttube().y);
    sb.draw(tube.getLight() , tube.getPoslight().x , tube.getPoslight().y);
}





delta*=speed;
sb.end();

update(delta);
b2dr.render(world , cameraBox2D);
stage.draw();

app.batch.begin();
app.font23.draw(app.batch,"Lights collected :" + dropsGathered , 0, 720);
app.batch.end();

cameraUpdate method

Vector3 position = camera.position;
position.x = player.position.x;
position.y = player.position.y;
camera.position.set(position);

createPlayer method

Body pBody;
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.DynamicBody;
def.position.set(x * PPM, y * PPM  );
def.fixedRotation = true;
pBody = world.createBody(def);

return pBody;

update method

world.step(1 / 60f , 6 , 2);

for(int i = 0; i < tubes.size; i++) {

    Tube tube = tubes.get(i);

    if (camera.position.y - (camera.viewportWidth/2) > tube.getposlefttube().y + tube.getlefttube().getWidth()) {
        tube.reposition(tube.getposlefttube().y + ( TUBE_COUNT) );

    }

    if (tube.collides(player.getBounds())){
        app.setScreen(new GameOver(app));


    }

    if (tube.gathered(player.getBounds())){

            dropsGathered++;




    }

    if (dropsGathered >= 50){
        //app.setScreen(new Stage2(app));
    }


}
camera.update();

handleInput();
camera.position.y = player.getPosition().y + 300;
player.update(delta);
camera.update();

cameraUpdate(delta);

stage.act(delta);
1
didn't work. my body and world need to follow player like cameraKaan San

1 Answers

0
votes

In your createPlayer method you can do body.setUserData(sprite). Then in your render method, you can do this (similar to how you are rendering tubes):

for (Body body : bodies) {
   Sprite playerSprite = (Sprite) body.getUserData();
   playerSprite.setPosition(body.getPosition().x, body.getPosition().y);
   playerSprite.draw(spriteBatch);
}

This will update the sprite's position with the body's position.