I'm having issues with texture rendering in my game. I've done a lot of research and repeated the same steps of tutorials over the internet to render and update Sprite positions properly. Somehow, textures still lag or flickers when bodies are moving, which makes movements not smooth sometimes. So I've setup a simple project with very basic physics that shows this issue. Besides asking you for help with mistakes on the code, I would like to ask you to run my code, if possible, to see if the movement is smooth and texture is rendering properly for you. Hopefully this is somehow a local problem for me.
The sprite is bouncing because I setup the restitution to do this, so it's easy to see the flickering and lag problem with simply watching the body bounce. To move the body to the sides, just press A or D. It will also cause flickering and lagging sometimes.
The sprite used for this test is a resized version of the bad logic sprite. You can get it here, or use any sprite you have to test this unwanted texture behaviour.
This is the class called by the DesktopLauncher:
public class MyGdxGame extends ApplicationAdapter implements InputProcessor {
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private Box2DDebugRenderer debugRenderer;
private Matrix4 debugMatrix;
private OrthographicCamera camera;
private Body body;
private Body bodyEdgeScreen;
private World world;
final float PPM = 100f;
@Override
public void create() {
batch = new SpriteBatch();
world = new World(new Vector2(0, -9.8f), true);
camera = new OrthographicCamera();// camera
float w = Gdx.graphics.getWidth(); // width of the screen
float h = Gdx.graphics.getHeight();// height of the screen
camera.setToOrtho(false, w, h);
camera.update();// update camera
debugRenderer = new Box2DDebugRenderer();
texture = new Texture("badlogic.jpg");
sprite = new Sprite(texture);
createBody(new Vector2(100, 100), sprite.getWidth(), sprite.getHeight(), 100f, 1f, 1f, BodyDef.BodyType.DynamicBody, true);
createBotEdge();
Gdx.input.setInputProcessor(this);
}
public void createBody(Vector2 position, float w, float h, float d, float r, float f,
BodyDef.BodyType bType, boolean isFixedRotation) {
Fixture fixture;
BodyDef bdef = new BodyDef();
bdef.type = bType;
bdef.position.set((position.x + w / 2) / PPM,
(position.y + h / 2) / PPM);
body = world.createBody(bdef);
fixture = body.createFixture(createFixture(body, w, h, d, r, f));
body.setFixedRotation(isFixedRotation);
body.setUserData(fixture);
}
private FixtureDef createFixture(Body b, float w, float h, float d, float r, float f) {
FixtureDef fDef = new FixtureDef();
fDef.shape = createShape(w, h);
fDef.density = d;
fDef.restitution = r;
fDef.friction = f;
return fDef;
}
private PolygonShape createShape(float width, float height) {
PolygonShape shape = new PolygonShape();
shape.setAsBox(width / 2 / PPM, height / 2 / PPM);
return shape;
}
public void createBotEdge() {
//Creation of the edge on the screen
BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyDef.BodyType.StaticBody;
float w = Gdx.graphics.getWidth() / PPM;
float h = Gdx.graphics.getHeight() / PPM - 500 / PPM; // -500 to be visible
bodyDef2.position.set(0, 0);
FixtureDef fixtureDef2 = new FixtureDef();
EdgeShape edgeShape = new EdgeShape(); // Creates an edge
edgeShape.set(-w, -h, w, -h);
fixtureDef2.shape = edgeShape;
bodyEdgeScreen = world.createBody(bodyDef2);
bodyEdgeScreen.createFixture(fixtureDef2);
edgeShape.dispose();
}
@Override
public void render() {
camera.update();
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
sprite.setPosition((body.getPosition().x * PPM) - sprite.
getWidth() / 2,
(body.getPosition().y * PPM) - sprite.getHeight() / 2);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
debugMatrix = batch.getProjectionMatrix().cpy().scale(PPM,
PPM, 0);
batch.begin();
batch.draw(sprite, sprite.getX(), sprite.getY());
batch.end();
debugRenderer.render(world, debugMatrix);
}
@Override
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.A) {
body.applyLinearImpulse(new Vector2(-80f, 0f), body.getPosition(), true);
}
if (keycode == Input.Keys.D) {
body.applyLinearImpulse(new Vector2(80f, 0f), body.getPosition(), true);
}
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
Thank you for your help in advance.