Search code examples
scrolllibgdxtexturesspriteparallax

Parallax scrolling in Sprite LibGDX


I want to parallax scroll a Texture behind a Sprite with fixed width and height.

The problem is i need to just scroll the Texture in a given width and height and not to the end of the screen. I need something like a window view on this texture.

i could overlay the rest of the screen with black areas but there has to be a better solution i guess ;-)

currently im doing this

sprite.setX(sprite.getX() + (OVERLAY_ANIMATION_SPEED * delta));
sprite2.setX(sprite2.getX() + (OVERLAY_ANIMATION_SPEED * delta));

and reset the sprite where x is bigger than the screen width. But i have a smaller area inside the screen in which the scrolling should appear not from the beginng to the end of the screen.

Hope somebody has a hint for me how to achive this.


Solution

  • I'm using a glViewport to achieve something similar:

    public void setViewPort(float dx, float dy, float sx, float sy)
    {
        Gdx.gl.glViewport((int) (screenWidth * dx), (int) (screenHeight * dy),
                          (int) (screenWidth * sx), (int) (screenHeight * sy));
    }
    

    So:

    setViewPort(0, 0, 1, 1);
    

    would render fullscreen and:

    setViewPort(0.2f, 0.2f, 0.6f, 0.6f);
    

    would render a 60% sized 'sub'window-viewport at 20% position (thus centered), nothing is rendered outside that window (clipped by OpenGL). Hope this helps someone!