1
\$\begingroup\$

I have a problem. When in libGDX my player moves across the screen and the camera is moving after him the player jumps back and forth a bit. Player still moves forward but it looks goofyThe Problem

I'm moving left-up

package com.qookie.miner;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.audio.*;
import com.badlogic.gdx.Input.Keys;


public class MinerMain extends ApplicationAdapter {
SpriteBatch batch;
SpriteBatch b;
private OrthographicCamera camera;
private float timer;
private int rand;
Texture img;
Texture map;
FreeTypeFontGenerator generator;
FreeTypeFontParameter parameter;
BitmapFont font;
private int speed = 250;
GameObject player;

@Override
public void create () {
    batch = new SpriteBatch();
    b = new SpriteBatch();
    img = new Texture("images/player.png ");
    map = new Texture("map1_fixed.png");
    generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/VCR_OSD_MONO_1.001.ttf"));
    parameter = new FreeTypeFontParameter();
    parameter.size = 10;
    parameter.color = Color.WHITE;
    font =  generator.generateFont(parameter);
    generator.dispose();
    player = new GameObject(img);
    camera = new OrthographicCamera(800,480);

}

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

    //Normal rendering(map, player, objects, enemies)
    batch.begin();
        batch.draw(map,0,0);
        batch.draw(player.getTexture(),player.x,player.y);
    batch.end();


    // GUI rendering(always in the same spot on the screen)
    b.begin();
        font.draw(b,"0_01a",4,476);
    b.end();
}

public void update() {
    //batch.setProjectionMatrix(camera.combined);
    //camera.position.set(player.x+player.width/2,player.y+player.height/2,0);
    //camera.update();
    if(Gdx.input.isKeyPressed(Keys.W)){
        player.y += speed * Gdx.graphics.getDeltaTime();
    }

    if(Gdx.input.isKeyPressed(Keys.A)){
        player.x -= speed * Gdx.graphics.getDeltaTime();
    }

    if(Gdx.input.isKeyPressed(Keys.S)){
        player.y -= speed * Gdx.graphics.getDeltaTime();
    }

    if(Gdx.input.isKeyPressed(Keys.D)){
        player.x += speed * Gdx.graphics.getDeltaTime();
    }

    if(Gdx.input.isKeyPressed(Keys.ESCAPE)){
        Gdx.app.exit();
    }

    timer += Gdx.graphics.getDeltaTime();

    rand = MathUtils.random(10);
    batch.setProjectionMatrix(camera.combined);
    camera.position.set(player.x+player.width/2,player.y+player.height/2,0);
    camera.update();
}
@Override
public void dispose() {
    font.dispose();
    batch.dispose();
    img.dispose();
    b.dispose();
}
}

GameObject.java file:

package com.qookie.miner;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.*;

public class GameObject extends Rectangle {
private Texture txt;
public Texture getTexture() {
return txt;
}
public GameObject(Texture t) {
this.txt = t;
this.width = t.getWidth(); this.height = t.getWidth();
  }

}
\$\endgroup\$
4
  • \$\begingroup\$ Have you padded your player.png? My guess is that because it is not padded, OpenGL is trying to blend with the background \$\endgroup\$ Mar 2, 2016 at 20:08
  • \$\begingroup\$ So what should I do @spectacularbob? \$\endgroup\$
    – user79822
    Mar 2, 2016 at 20:19
  • \$\begingroup\$ @spectacularbob I have added 'batch.disableBlending();' after 'batch.begin()' and it did nothing. \$\endgroup\$
    – user79822
    Mar 2, 2016 at 20:26
  • \$\begingroup\$ Looks like you found your answer, but this may be helpful: The way that OpenGL renders textures, it usually blends the outer pixels of a texture region with the surrounding pixels in the image. So if your sprite is surrounded by transparent pixels in the sprite sheet, it can produce a "gutter effect" where the edge pixels seem to be correct. To fix this, you can just copy the outermost pixels of a sprite outward to add a "padding" so that when OpenGl does it's blending it will blend with the right colors. \$\endgroup\$ Mar 4, 2016 at 16:30

1 Answer 1

1
\$\begingroup\$

If I remember correctly, SpriteBatch stores it's own matrices. This means that when you do:

batch.setProjectionMatrix(camera.combined);
camera.position.set(player.x+player.width/2,player.y+player.height/2,0);
camera.update();

the camera should be updated first, then the projection matrix set after the update, instead. So instead I believe it should be:

camera.position.set(player.x+player.width/2,player.y+player.height/2,0);
camera.update();
batch.setProjectionMatrix(camera.combined);

This may be the cause of your problem, but I'm not entirely sure - good luck, and I hope this helps!

\$\endgroup\$

You must log in to answer this question.