LIbGDX: prefered way to code for map (and player) scrolling

Hi,

I’m trying to rewrite my code of my Knightmareg based game (see youtube vid at bottom) to incorporate LIbGDX.
The way how i formarly (when i was still using applet) set up my map scroll is this:

  • I’ve got a map that 4800 pixels in height
  • In the draw method of my main class I had it start displaying my map at 800 pixels from the bottom left (my original GameHeight = 800px)
  • then in my backgrounds class at every update i have that Y coordinate incremented by 1 pixel on every update untill the top left Y coordinate of my map drawn on screen (at 0,0) reaches the top left of the map.
    -(one problem I then have to work around is that the draw method uses (int) and i can’t slow my scroll down by using a value less then 1)

Now I could do the same in my LibGDX project but it just doesnt seem like the best way to do this.

One way, I’ve been thinking of, to create my new scroll is instead of updating the position where my map is drawn is to move libgdx’s orthographic camera viewport around and create my scroll like this.
This also would mean that i have to change the players position to always be relative to the viewports position.

[b]My question (before I start coding doing this all):

Would you also do it like this?

Or do you know if a preferred(/better) way to handle my (vertical) map scroll?[/b]

bmb_7p_kI1M

Literally stop over thinking it.

Setup a camera, translate it vertically. Only render things in the frustum. There really should not be any doubt about this method, you can easily test if the player falls behind or if you reach the top of your map.

You can easily create the vertical translation with a simple matrix that you continuously (each frame) translate with a vector that points down (a Vector2f(0f; -100f*delta) for example).
In your shaders you can multiply your vertices’ position with the matrix to translate them downwards. It’s as easy as that. :slight_smile:

Edit: Sorry, didn’t see that you’re using LibGDX. In that case there’s really no need for custom shaders and such, as Gibbo3771 said you should simply move the orthographic camera downwards. Also, if you’re using orthographic projection (which you should for 2D games) then you aren’t using a frustum but a cube for rendering. Seeing that for 2D we don’t use the Z axis (other than for something like Z-Ordering) you can simply just check against a quad instead of a cube, which is extremely simple. However, I’m not sure if this would be necessary for such a small game, I don’t see much of a performance gain from it. :slight_smile: