0
votes

Im new on Box2d got a problem and couldnt solve it. I want to move my player left and right when the user touch my left and right buttons. I created a fixture I can move body and fixture but not the player sprite How can I attach my player sprite to my body ? and How should I control body because I cant stop it. I want to find a proper way of controlling player in box2d. I couldnt use setLinerVelocity etc.

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
Use TextureRegion instead of Sprite and use a draw method that allows you to specify rotation.Tenfour04
I dont understand. can you be more specific ?.Kaan San

1 Answers

0
votes

Do not use the Sprite class. Use the TextureRegion class instead. Sprite is confusingly subclassed from TextureRegion, so when you call batch.draw(sprite, ...) its position and rotation parameters are ignored because it is being treated as a TextureRegion.

You could use a Sprite by calling sprite.draw(batch) but a Sprite is redundant because your Body already has position and rotation parameters.

Use a TextureRegion directly with the SpriteBatch. You can orient it with rotation parameters passed into the draw method.