1

My problem is the following, when i don't move my camera my sprite follow my box2d body well. But when i implement the scrolling aka i use cam.translate the sprite go each frame to high until it disapear. The weird thing is that if i check the coordinate are exactly the same between my b2body and my sprite.

UPDATE : I tried to use cam.position.set(cam.position.x, cam.position.y * 1.001f, 0) to remplace the cam.translate but i had the same problem.

The variables :

private GalaxyFighter jeu;

//render variables
private OrthographicCamera cam;
private Viewport gamePort;
private Hud hud;
private float maxScrolling;

//tilded map variables
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;

//box2d variables
private World world;
private Box2DDebugRenderer b2dr;
private SpaceShip player;
private Sprite spriteSpaceShip;

The constructor :

public EcranJeu(GalaxyFighter jeu, float maxScrolling){
    this.jeu = jeu;

    cam = new OrthographicCamera();

    gamePort = new FitViewport(GalaxyFighter.V_WIDTH / GalaxyFighter.PPM, GalaxyFighter.V_HEIGHT / GalaxyFighter.PPM, cam);

    hud = new Hud(jeu.batch);

    this.maxScrolling = maxScrolling;

    maploader = new TmxMapLoader();
    map = maploader.load("level1.tmx");
    renderer = new OrthogonalTiledMapRenderer(map, 1 / GalaxyFighter.PPM);

    cam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);

    world = new World(new Vector2(0,0), true);

    b2dr = new Box2DDebugRenderer();

    player = new SpaceShip(world);

    spriteSpaceShip = new Sprite(new Texture("redship1.png"));

    player.b2body.setUserData(spriteSpaceShip);

    new B2WorldCreator(world, map);

    world.setContactListener(new WorldContactListener());
}

Here is my update method called in render :

 public void update(float dt){
    handleInput(dt);

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

   if(cam.position.y < (maxScrolling / GalaxyFighter.PPM))
        cam.translate(0, 0.5f / GalaxyFighter.PPM);

    limitPlayer();

    spriteSpaceShip.setPosition(player.b2body.getPosition().x * GalaxyFighter.PPM - 8, player.b2body.getPosition().y * GalaxyFighter.PPM - 8);
    System.out.println("Pos player x = " + (player.b2body.getPosition().x * GalaxyFighter.PPM - 8) + " y = " + (player.b2body.getPosition().y * GalaxyFighter.PPM - 8));
    System.out.println("Pos sprite x = " + spriteSpaceShip.getX() + " y = " + spriteSpaceShip.getY());

    cam.update();
    renderer.setView(cam);
}

Here my render method :

 public void render(float delta) {
    update(delta);

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

    renderer.render();

    b2dr.render(world, cam.combined);

    jeu.batch.setProjectionMatrix(hud.stage.getCamera().combined);

    hud.stage.draw();

    jeu.batch.begin();
    spriteSpaceShip.draw(jeu.batch);
    jeu.batch.end();
}

I don't see where is my mistake so I hope someone will find it ! Thank you for your time.

EDIT : Here is the other class I am using, and i complete the gameScreen class

HUD

public class Hud implements Disposable {
public Stage stage;
private Viewport viewport;

private Integer life;
private static Integer score;
private Integer niveau;

Label lifeLabel;
private static Label scoreLabel;
Label niveauLabel;
Label lifeStrLabel;
Label scoreStrLabel;
Label niveauStrLabel;

public Hud(SpriteBatch sb){
    life = 3;
    score = 0;
    niveau = 1;

    viewport = new FitViewport(GalaxyFighter.V_WIDTH, GalaxyFighter.V_HEIGHT, new OrthographicCamera());
    stage = new Stage(viewport, sb);

    Table table = new Table();
    table.top();
    table.setFillParent(true);

    lifeLabel = new Label(String.format("%02d", life), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    scoreLabel = new Label(String.format("%04d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    niveauStrLabel = new Label("NIVEAU", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    lifeStrLabel = new Label("VIE", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    scoreStrLabel = new Label("SCORE", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    niveauLabel = new Label(String.format("%02d", niveau), new Label.LabelStyle(new BitmapFont(), Color.WHITE));

    table.add(lifeStrLabel).expandX().padTop(10);
    table.add(niveauStrLabel).expandX().padTop(10);
    table.add(scoreStrLabel).expandX().padTop(10);
    table.row();
    table.add(lifeLabel).expandX();
    table.add(niveauLabel).expandX();
    table.add(scoreLabel).expandX();

    stage.addActor(table);
}

And the SpaceShip which is the player :

public class SpaceShip extends Sprite {
public World world;
public Body b2body;

public SpaceShip(World world){
    this.world = world;
    defineSpaceShip();
}

public void defineSpaceShip(){
    BodyDef bdef = new BodyDef();
    bdef.position.set(GalaxyFighter.V_WIDTH / 2 / GalaxyFighter.PPM, 3 / GalaxyFighter.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(5 / GalaxyFighter.PPM);

    fdef.filter.categoryBits = GalaxyFighter.SPACESHIP_BIT;
    fdef.filter.maskBits = GalaxyFighter.DEFAULT_BIT | GalaxyFighter.ASTEROIDE_BIT |GalaxyFighter.BONUS1_BIT|GalaxyFighter.BONUS2_BIT|GalaxyFighter.BONUS3_BIT|GalaxyFighter.PISTE_BIT|GalaxyFighter.BORDSMAP_BIT;

    fdef.shape = shape;
    b2body.createFixture(fdef);

    EdgeShape head_spaceship = new EdgeShape();
    head_spaceship.set(new Vector2(-4 / GalaxyFighter.PPM, 0), new Vector2(4 / GalaxyFighter.PPM, 0));
    fdef.shape = head_spaceship;
    fdef.isSensor = true;
    b2body.createFixture(fdef).setUserData("head_spaceship");

    EdgeShape head_spaceship2 = new EdgeShape();
    head_spaceship2.set(new Vector2(0, -4 / GalaxyFighter.PPM), new Vector2(0, 4 / GalaxyFighter.PPM));
    fdef.shape = head_spaceship2;
    fdef.isSensor = true;
    b2body.createFixture(fdef).setUserData("head_spaceship2");
}
elenox
  • 13
  • 2
  • Is the `Camera` you get from `hud.stage.getCamera()` the same `Camera` as `cam`? It seems you are rendering the **Box2D** debug view using one camera and the `spriteSpaceShip` using another. – bornander Mar 17 '19 at 16:44
  • Yes i use another cam as you can see in the HUD class is this a problem ? – elenox Mar 17 '19 at 17:17
  • Try using the same camera for the box2d debug renderer and the Sprite. – bornander Mar 17 '19 at 17:41
  • Do you mean replacing this : "jeu.batch.setProjectionMatrix(hud.stage.getCamera().combined);" to this : "jeu.batch.setProjectionMatrix(cam.combined);" because i did it and it changed nothing – elenox Mar 17 '19 at 18:12
  • What does PPM mean in `cam.translate(0, 0.5f / GalaxyFighter.PPM);`? – Luis Fernando Frontanilla Mar 17 '19 at 23:39
  • It scale the coordinate to the world, PPM means Pixel per Meter. If i don't the camera moves too fast. – elenox Mar 18 '19 at 13:49
  • Your mistace lies here: `spriteSpaceShip.setPosition(player.b2body.getPosition().x * GalaxyFighter.PPM - 8, player.b2body.getPosition().y * GalaxyFighter.PPM - 8);`. I think PPM is not 1 so if body position is 5 your sprite will be somewhere else than 5 minus 8. That make no sence do you agree with me? So why you don't set your sprite simple to your body like: `spriteSpaceShip.setPosition(player.b2body.getPosition().x, player.b2body.getPosition().y);` Now the only thing you must remember is that the 0,0 in box2d is in the center and 0,0 in libgdx is on the bottom left corner. – Morchul Mar 22 '19 at 15:40

1 Answers1

0

Have you tried setting the camera's position to the player's position instead of just translating it linearly? Like this:

cam.position.set(0, spriteSpaceShip.getY(), 0);
TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37
  • the .setPosition() doesn't exist in the camera class and the orthographic camera – elenox Mar 18 '19 at 20:12
  • I don't want to follow the player with the camera I want to do a scrolling game where the camera goes up and the player is restricted in the camera aera which i did. The problem is when i don't translate the camera everything is ok, the sprite follow my box2d object but when i activate the scrolling little by little the sprite go far away up till it disapear. – elenox Mar 18 '19 at 20:23
  • Try that, I just edited it again. Now it only follows the player in the y axis – TheChubbyPanda Mar 18 '19 at 20:25
  • Ohhh I get it, hold on. – TheChubbyPanda Mar 18 '19 at 20:40
  • Sorry, I can't seem to figure it out. – TheChubbyPanda Mar 18 '19 at 20:43
  • Too bad :( But it gave me more info. The Sprite doesn't start in the player body but at the bottom of the screen and go up faster than the player body – elenox Mar 18 '19 at 20:53
  • I feel like you are messing up the PPM scaling somewhere but I just don't have the time to go through all of your code. If you want, you can send it to me and I'll take a look at it when I get a chance. – TheChubbyPanda Mar 18 '19 at 20:55