1
\$\begingroup\$

I have a weird camera problem that I can't solve! I add my actors to the main stage and use its camera too. Actors are constrained within the screen viewport.

I also have a second camera that I use for the parallax background. I check for touchDragged and touchDown to move the actor to that position. So I do:

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    getStage().getCamera().unproject(touchPosition.set(screenX, screenY, 0f));
    player.moveTo(touchPosition);

    return false;
}

I can pan the parallax camera, and I do:

public void panBy(float x, float y)
{
    panTo.set((float)MathHelper.round(position.x + x, 0), (float)MathHelper.round(position.y + y, 0), 0);
    panning = true;
}

@Override
public void update() {
    if (panning) {
        position.x += (panTo.x - position.x) * panLerp;
        position.y += (panTo.y - position.y) * panLerp;

        Vector3 pos = new Vector3((float)MathHelper.round(position.x, 0), (float)MathHelper.round(position.y, 0), 0);

        if (pos.epsilonEquals(panTo)) {
            position.set(pos);
            panning = false;
        }
    }

    super.update();
}

The drawing is done:

public void draw(Batch batch)
{
    //Start batch
    batch.setProjectionMatrix(parallaxCamera.combined);
    batch.begin();

    background.draw(batch, 1f);

    //End batch
    batch.end();
}

Actors are handled by the stage right after the above draw call.

The above work fine individually. However when I pan and drag the actor the parallaxCamera follows the actor too. I the parallaxCamera to be totally independent and just keep scrolling.

Why do I have this behavior?

\$\endgroup\$

Your Answer

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

Browse other questions tagged or ask your own question.