Super simple libgdx game stuttering when moving player in y axis, why?

I have a simple desktop Libgdx game that allows you to move around a simple map. I am getting an issue whenever the player moves up or down where there is a rapid stutter. It doesn't happen when it moves left or right. The code is identical for moving along the x axis as it is along the y, and I have no idea what is causing it. I noticed if I don't use a camera (which I need for working with Tiled maps (is that even true?)), the stuttering goes away. Am I implementing the camera wrong? Thanks!

Player:

public class Player{
    private float x, y;
    private Texture ship, ship_up, ship_middle, ship_down;
    private float speed = 60;
    public static boolean UP_TOUCHED, DOWN_TOUCHED, LEFT_TOUCHED, RIGHT_TOUCHED;
    private float width, height;
    private TiledMapTileLayer collisionLayer;

    public Player(TiledMapTileLayer collisionLayer) {
        this.collisionLayer = collisionLayer;

        x = VIEWPORT_WIDTH / 2;
        y = VIEWPORT_HEIGHT / 2;

        loadPlayerTextures();
    }

    public void update (float delta){

        UP_TOUCHED = false;
        DOWN_TOUCHED = false;
        LEFT_TOUCHED = false;
        RIGHT_TOUCHED = false;

        // update player movement
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT) && x > 0){
            x -= speed * delta;
            LEFT_TOUCHED = true;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && x < VIEWPORT_WIDTH - width) {
            x += speed * delta;
            RIGHT_TOUCHED = true;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.UP) && y < VIEWPORT_HEIGHT - height){
            y += speed * delta;
            UP_TOUCHED = true;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && y > 0){
            y -= speed * delta;
            DOWN_TOUCHED = true;
        }

        // set ship texture:
        if (UP_TOUCHED == true && DOWN_TOUCHED == false) {
            ship = ship_up;
        } else if (DOWN_TOUCHED == true && UP_TOUCHED == false) {
            ship = ship_down;
        } else {
            ship = ship_middle;
        }

        checkCollisionMap();
    }

    public void render (SpriteBatch sb){
        sb.draw(ship,x,y);
    }

    public void loadPlayerTextures(){
        ship_middle = new Texture("ship_middle.png");
        ship_up = new Texture("ship_up.png");
        ship_down = new Texture("ship_down.png");

        ship = ship_middle;

        width = ship_middle.getWidth();
        height = ship_middle.getHeight();
    }

    public float getSpeed() {
        return speed;
    }

    public void checkCollisionMap(){
        float xWorld = x;
        float yWorld = y;

        ////////////////// Check For Collision
        boolean collisionWithMap = false;
        // check right side middle
        collisionWithMap = isCellBLocked(xWorld, yWorld);

        // //////////////// React to Collision
        if (collisionWithMap) {
            System.out.println("player-map collision!!!");
            if(UP_TOUCHED){
                y -= 1;
            }
            if(DOWN_TOUCHED){
                y += 1;
            }
            if(LEFT_TOUCHED){
                x += 1;
            }
            if(RIGHT_TOUCHED){
                x -= 1;
            }
        }
    }

    public boolean isCellBLocked(float x, float y) {
        TiledMapTileLayer.Cell cell = collisionLayer.getCell(
                (int) (x / collisionLayer.getTileWidth()),
                (int) (y / collisionLayer.getTileHeight()));

        return cell != null && cell.getTile() != null
                && cell.getTile().getProperties().containsKey("collision");
    }

}

Gridlock (the Main class):

public class Gridlock extends ApplicationAdapter {
    // map is 32 by 32 tiles with 32 pixel tiles

    public static int VIEWPORT_WIDTH = 1024;
    public static int VIEWPORT_HEIGHT = 1024;

    private SpriteBatch sb;
    public static OrthographicCamera cam;
    private Player player;
    private float delta;

    // TiledMap
    private TiledMap tileMap;
    private OrthogonalTiledMapRenderer tileMapRenderer;

    // Collision things
    private TiledMapTileLayer collisionLayer;
    public static float SCROLLTRACKER_X;
    public static float SCROLLTRACKER_Y;

    @Override
    public void create () {
        cam = new OrthographicCamera();
        cam.setToOrtho(false, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
        sb = new SpriteBatch();
        sb.setProjectionMatrix(cam.combined);

        tileMap = new TmxMapLoader().load("tiledmap1.tmx");
        tileMapRenderer = new OrthogonalTiledMapRenderer(tileMap);

        collisionLayer = (TiledMapTileLayer) tileMap.getLayers().get("Tile Layer 2");
        player = new Player(collisionLayer);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        delta = Gdx.graphics.getRawDeltaTime();

        // updates
        player.update(delta);

        // rendering
        tileMapRenderer.setView(cam);
        tileMapRenderer.render();

        sb.begin();
        player.render(sb);
        sb.end();
    }

    @Override
    public void dispose () {

    }

}

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image