gogonapel
gogonapel

Reputation: 788

Camera and sprite move with similar velocity but JITTER appears

I have a sprite that I advance in the world along with a camera. They both move at the same speed . However , I see a slight jitter of the moving sprite . Should that be possible given the fact that I add the same Y value to both the camera and the sprite?

(NOTE: The filter for the sprite is set to LINEAR)

public void AdvanceWorld(){ 
    float delta = Gdx.graphics.getDeltaTime();      
    float delta_64_srsz = delta * 64f;
    float velo = spaces.world_velocity * delta_64_srsz;
    spaces.spaceshipAdvance(velo);      
    camera.position.y += velo;
}   

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    AdvanceWorld();
    ...
    batch.begin();      
    ....
            batch.end();
            ...
    camera.update();    
    batch.setProjectionMatrix(camera.combined);     
}

public void spaceshipAdvance(float velocity){
    sprite.setPosition(sprite.getX(), sprite.getY() + velocity);
    thrusters.setPosition(thrusters.getX(), thrusters.getY() + velocity);   
}

However, after the framerate stabilises around 60fps the jitter fades and is only slightly noticeable. It's only noticeable when there is variation in FPS.

Also, if I decrease the velocity( delta_64_srsz = delta * 16f ) the jitter disappears. It seems that be big velocity I'm using causes this. Can I sync the camera with the sprite so I can use high velocities without the jitter?

Upvotes: 0

Views: 515

Answers (2)

Cristian Gutu
Cristian Gutu

Reputation: 1261

You should avoid moving the camera yourself and instead have the camera follow the sprite.

This can be achieved as follows:

First make an OrthographicCamera like this:

OrthographicCamera cam = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

Next in your render() method update the cam position with the sprite x and y positions like this:

cam.position.set(sprite.getX(), sprite.getY(), 0);

After that you should set the projection matrix to cam.combined like this:

batch.setProjectionMatrix(cam.combined);

And finally update your camera:

cam.update();

You should put these last three steps together in the render() method like this:

public void render(float delta){
    // other stuff ...
    cam.position.set(sprite.getX(), sprite.getY(), 0);
    cam.update();
    batch.setProjectionMatrix(cam.combined);
    // other stuff ...
}

Upvotes: 2

nikoliazekter
nikoliazekter

Reputation: 757

What about using meters instead of pixels? It's always nice practise not to use pixels.

Upvotes: 0

Related Questions