0

I'm loading a .tmx map using Libgdx and the map is not filling the whole screen. I cannot figure out the problem so as a last resort I'm asking a question here.I'm following a tutorial on YouTube and he did not cover this problem and as a result I cannot continue. I have tried multiple things with no sucess. The map is width: 240 tiles, height: 13 tiles, and tiles are 16 by 16

What it looks like on Desktop, What it looks like on Android.

This is the code. I think the problem has to do with

renderer = new OrthogonalTiledMapRenderer(map, 1/DBZ.PPM),
gameCam.position.set(gamePort.getWorldWidth()/2, 
gamePort.getWorldHeight()/2, 0);,
gameCam = new OrthographicCamera(); 
gamePort = new FitViewport(DBZ.V_WIDTH/DBZ.PPM, DBZ.V_HEIGHT/DBZ.PPM, gameCam);

    public class PlayScreen implements Screen {
    private  DBZ game;
    private OrthographicCamera gameCam;
    private Viewport gamePort;
    private Hud hud;
    private TmxMapLoader maploader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;

    private World world;
    private Box2DDebugRenderer b2dr;

    private Goku player;
    private TextureAtlas atlas;


    public PlayScreen(DBZ game){
        atlas = new TextureAtlas("goku.pack");
        this.game=  game;
        gameCam = new OrthographicCamera();
        gamePort = new FitViewport(DBZ.V_WIDTH/DBZ.PPM, DBZ.V_HEIGHT/DBZ.PPM, gameCam);
        hud = new Hud(game.batch);
        maploader= new TmxMapLoader();
        map = maploader.load("level1.tmx");
        renderer = new OrthogonalTiledMapRenderer(map, 1/DBZ.PPM);
        gameCam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);
        world = new World(new Vector2(0,-10),true);
        b2dr = new Box2DDebugRenderer();
        new B2WorldCreator(world,map);
        player = new Goku(world, this);
    }

    public TextureAtlas getAtlas(){
        return atlas;
    }

    @Override
    public void show() {

    }

    public void handleInput(float dt){
        if (Gdx.input.isKeyJustPressed(Input.Keys.UP))
            player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2)
            player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
            player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);

    }

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

        world.step(1/60f, 6, 2);
        player.update(dt);
        gameCam.position.x = player.b2body.getPosition().x;





        gameCam.update();
        renderer.setView(gameCam);

    }

    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        renderer.render();

        b2dr.render(world, gameCam.combined);

        game.batch.setProjectionMatrix(gameCam.combined);
        game.batch.begin();
        player.draw(game.batch);
        game.batch.end();

        game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
        hud.stage.draw();

    }

    @Override
    public void resize(int width, int height) {
        gamePort.update(width, height);


    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        map.dispose();
        renderer.dispose();
        world.dispose();
        b2dr.dispose();
        hud.dispose();

    }
}

public class DBZ extends Game{
    public SpriteBatch batch;
    public  static final int V_WIDTH = 400;
    public  static final int V_HEIGHT = 208;
    public static final float PPM = 100;


    @Override
    public void create () {
        batch = new SpriteBatch();
        setScreen(new PlayScreen(this));
    }

    @Override
    public void render () {
        super.render();
    }

    @Override
    public void dispose () {
        batch.dispose();
    }
}

public class Goku extends Sprite {
    public World world;
    public Body b2body;
    private TextureRegion gokuStand;

    public Goku(World world, PlayScreen screen){
        super(screen.getAtlas().findRegion("goku_sprite"));
        this.world = world;
        defineGoku();
        gokuStand = new TextureRegion(getTexture(), 5,12,59,85);
        setBounds(0,0,59/DBZ.PPM,85/DBZ.PPM);
        setRegion(gokuStand);

    }

    public void defineGoku(){
        BodyDef bdef = new BodyDef();
        bdef.position.set(32/DBZ.PPM,32/DBZ.PPM);
        bdef.type = BodyDef.BodyType.DynamicBody;
        b2body = world.createBody(bdef);

        FixtureDef fdef = new FixtureDef();
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(59/2/DBZ.PPM, 85/2/DBZ.PPM);

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

    }

    public void update(float dt){
        setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2 );

    }

}
m2j
  • 1,152
  • 5
  • 18
  • 43
  • Possible duplicate of [Cannot render .tmx map using OrthogonalTiledMapRenderer (Android)](http://stackoverflow.com/questions/42418269/cannot-render-tmx-map-using-orthogonaltiledmaprenderer-android) – Abhishek Aryan Apr 09 '17 at 06:31

2 Answers2

0

I believe what you are looking for is the ability to clamp the camera to the map. This can be achieved using MathUtils.clamp.

gameCam.position.x = MathUtils.clamp(gameCam.position.x, viewportWidth/2 , 38 - viewportWidth/2);
gameCam.position.y = MathUtils.clamp(gameCam.position.y, viewportHeight/2, 208 - viewportHeight/2);

EDIT: Your updated PlayScreen:

package com.edwin.game.Screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.edwin.game.DBZ;
import com.edwin.game.Scenes.Hud;
import com.edwin.game.Sprites.Goku;
import com.edwin.game.Tools.B2WorldCreator;

/**
 * Created by Edwin on 3/20/2017.
 */
public class PlayScreen implements Screen {
private  DBZ game;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Hud hud;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;

private World world;
private Box2DDebugRenderer b2dr;

private Goku player;
private TextureAtlas atlas;
private float viewportWidth;
private float viewportHeight;


public PlayScreen(DBZ game){
    atlas = new TextureAtlas("goku.pack");
    this.game=  game;
    gameCam = new OrthographicCamera();
    gamePort = new FitViewport(DBZ.V_WIDTH/DBZ.PPM, DBZ.V_HEIGHT/DBZ.PPM, gameCam);


    hud = new Hud(game.batch);
    maploader= new TmxMapLoader();
    map = maploader.load("level1.tmx");


    renderer = new OrthogonalTiledMapRenderer(map, 1/DBZ.PPM);

    viewportWidth = gamePort.getWorldWidth();
    viewportHeight= gamePort.getWorldHeight();

    world = new World(new Vector2(0,-10),true);
    b2dr = new Box2DDebugRenderer();

    new B2WorldCreator(world,map);

    player = new Goku(world, this);


}

public TextureAtlas getAtlas(){
    return atlas;
}

@Override
public void show() {

}

public void handleInput(float dt){
    if (Gdx.input.isKeyJustPressed(Input.Keys.UP))
        player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2)
        player.b2body.applyLinearImpulse(new Vector2(0.5f, 0), player.b2body.getWorldCenter(), true);
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
        player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);

}

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

    world.step(1/60f, 6, 2);
    player.update(dt);
    gameCam.position.x = player.b2body.getPosition().x;

    // cam pos                          / var to clamp       / min val          / max val
    gameCam.position.x = MathUtils.clamp(gameCam.position.x, viewportWidth/2 , 38 - viewportWidth/2);
    gameCam.position.y = MathUtils.clamp(gameCam.position.y, viewportHeight/2, 208 - viewportHeight/2);

    System.out.println(gameCam.position.x);


    gameCam.update();
    renderer.setView(gameCam);

}

@Override
public void render(float delta) {
    update(delta);
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    renderer.render();
    //

    //

    b2dr.render(world, gameCam.combined);

    game.batch.setProjectionMatrix(gameCam.combined);
    game.batch.begin();
    player.draw(game.batch);
    game.batch.end();

    game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
    hud.stage.draw();

}

@Override
public void resize(int width, int height) {
    gamePort.update(width, height);


}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    map.dispose();
    renderer.dispose();
    world.dispose();
    b2dr.dispose();
    hud.dispose();

}
}
dfour
  • 1,376
  • 1
  • 12
  • 16
  • I replaced `gameCam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);` with `float viewportWidth = gamePort.getWorldWidth(); float viewportHeight= gamePort.getWorldHeight(); gameCam.position.x = MathUtils.clamp(gameCam.position.x, viewportWidth/2 , 400 - viewportWidth/2); gameCam.position.y = MathUtils.clamp(gameCam.position.y, viewportHeight/2, 208 - viewportHeight/2);` and it looks the same. It didn't work. – JuniorDeveloper Apr 09 '17 at 22:44
  • You need to add this code to your render method so its applied every frame. Applying it only to the PlayScreen constructor will only apply it once when the PlayScreen is made. – dfour Apr 10 '17 at 09:23
  • I tried it and it did not work. I added that code right below renderer.render(); in the render method. I would appreciate it if you could take a look at my Github repository https://github.com/eochoa5/Dragon-Ball-Z-Bros- – JuniorDeveloper Apr 10 '17 at 10:28
  • It works now. It fills the entire width of the screen but not the height or y axis. When it's on desktop it fills the whole screen but when I test it on Android or resize the window, the entire y axis isn't filled. It's centered vertically with some room at the top and bottom. – JuniorDeveloper Apr 11 '17 at 08:49
  • I was only able to test on desktop :/ in order to limit the y axis as well you need to change the 208 on the gameCam.position.y = MathUtils.clamp(gameCam.position.y, viewportHeight/2, 208 - viewportHeight/2); line to a more suitable number like 15 or however high your level is. – dfour Apr 11 '17 at 09:28
  • Changing fitviewport to fillviewport fixed it. Both answers are the accepted answers after making that change. I was just going to give up but now I can move on – JuniorDeveloper Apr 12 '17 at 21:37
0

Problem is in placement of camera, attach camera with player so that viewport of camera cover whole screen. I am trying to fix this.

public float gokuStartingPositionX;

public PlayScreen(DBZ game){
    .....
    new B2WorldCreator(world,map);

    gokuStartingPositionX=64/DBZ.PPM;  //added in your method
    player = new Goku(world, this);
} 

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

    world.step(1/60f, 6, 2);
    player.update(dt);
    //camera position is decided by player position and keep camera in this way so it cover whole viewport width with screen
    gameCam.position.x = player.b2body.getPosition().x + gamePort.getWorldWidth()/2 - gokuStartingPositionX;

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

Small modification in Goku

public Goku(World world, PlayScreen screen){
    super(screen.getAtlas().findRegion("goku_sprite"));
    this.world = world;
    defineGoku(screen.gokuStartingPositionX);

    gokuStand = new TextureRegion(getTexture(), 5,12,59,85);
    setBounds(0,0,59/DBZ.PPM,85/DBZ.PPM);
    setRegion(gokuStand);

}

public void defineGoku(float startX){
    BodyDef bdef = new BodyDef();
    bdef.position.set(startX,32/DBZ.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);
    ... 
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • I get the same problem as with the other answer and an answer provided by a person on Youtube. It fills the entire width of the screen but not the height or y axis. When it's on desktop it fills the whole screen but when I test it on Android or resize the window, the entire y axis isn't filled. It's centered vertically with some room at the top and bottom – JuniorDeveloper Apr 11 '17 at 09:11
  • this is what [FitViewport](https://github.com/libgdx/libgdx/wiki/Viewports#fitviewport) dear, otherwise use some other viewport. – Abhishek Aryan Apr 11 '17 at 09:16
  • Both answers work by just changing fitviewport to fillviewport. It takes the same parameters which is nice. Thank you. – JuniorDeveloper Apr 12 '17 at 21:34