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 goofy
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();
}
}