0
\$\begingroup\$

I'm starting with Libgdx and I would like to make my ParallaxBackground move following my player. I am using a TiledMap along with Scene2d, and have the camera follow the sprite of my player. I'm basing my ParallaxBackground on the example of the following link:

Link here

Best regards

\$\endgroup\$
0
\$\begingroup\$

Answering because you've not had any response yet. I don't have experience of doing parallax background but in the example you linked the speed of the background is hard coded, would you not just set the speed based on user input?

In your GameScreen you'd do something like

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    parallaxBackground.setSpeed(0); // reset to 0 to stop the background when no keys are pressed. 
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
          // Set player speed x -1
          parallaxBackground.setSpeed(-1);
    }
    if( Gdx.input.isKeyPressed(Input.Keys.RIGHT) ) {
          // Set player speed x 1
          parallaxBackground.setSpeed(1);
    }
    stage.act();
    stage.draw();
}

Then in the constructor around this bit:

    parallaxBackground.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    parallaxBackground.setSpeed(1);
    stage.addActor(parallaxBackground);

Comment out that middle line and it should work.

You'll also need to move the

ParallaxBackground parallaxBackground

definition from the constructor and make it a field like so:

ParallaxBackground parallaxBackground;

public GameScreen(Game aGame) {
    game = aGame;
    // Other code removed for ease of reading
    parallaxBackground = new ParallaxBackground(textures);
}

I've tested this without a sprite and it seems to work. The only thing you might find if you're also moving the sprite is the player might feel too fast, in which case you might just reduce the sprite and parallax movement speed.

Hope that helps.

\$\endgroup\$
0
\$\begingroup\$

LibGDX provides a parallax test which shows you all you need to create a parallax background.

The core items are a Parallax camera:

class ParallaxCamera extends OrthographicCamera {
    Matrix4 parallaxView = new Matrix4();
    Matrix4 parallaxCombined = new Matrix4();
    Vector3 tmp = new Vector3();
    Vector3 tmp2 = new Vector3();

    public ParallaxCamera (float viewportWidth, float viewportHeight) {
        super(viewportWidth, viewportHeight);
    }

    public Matrix4 calculateParallaxMatrix (float parallaxX, float parallaxY) {
        update();
        tmp.set(position);
        tmp.x *= parallaxX;
        tmp.y *= parallaxY;

        parallaxView.setToLookAt(tmp, tmp2.set(tmp).add(direction), up);
        parallaxCombined.set(projection);
        Matrix4.mul(parallaxCombined.val, parallaxView.val);
        return parallaxCombined;
    }
}

and the drawing process:

    // background layer, no parallax, centered around origin
    batch.setProjectionMatrix(camera.calculateParallaxMatrix(0, 0));
    batch.disableBlending();
    batch.begin();
    batch.draw(layers[0], -(int)(layers[0].getRegionWidth() / 2), -(int)(layers[0].getRegionHeight() / 2));
    batch.end();
    batch.enableBlending();

    // midground layer, 0.5 parallax (move at half speed on x, full speed on y)
    // layer is 1024x320
    batch.setProjectionMatrix(camera.calculateParallaxMatrix(0.5f, 1));
    batch.begin();
    batch.draw(layers[1], -512, -160);
    batch.end();

    // foreground layer, 1.0 parallax (move at full speed)
    // layer is 2048x320
    batch.setProjectionMatrix(camera.calculateParallaxMatrix(1f, 1));
    batch.begin();
    for (int i = 0; i < 9; i++) {
        batch.draw(layers[2], i * layers[2].getRegionWidth() - 1024, -160);
    }
    batch.end();

This should be all you need to implement a parallax background

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.