In a 2D scrolling platformer, what is the most efficient way or knowing which things with parallax depth should be drawn?

The approach below describes trying to know which things are nearby so they can be udpated/drawn. The problem with working out which parallax items are nearby is that you don't know where any of them are until you calculate where they are!

Original Post

I have a 2D array Set<Thing>[][] that I use to store Things that appear on each screen in a scrolling 2D platformer. I use this to look up only Things that a near the current location being rendered, so that I don't bother updating irrelevant objects.

I translate the world coordinates, divide them by the screen size (rounding down), and can then look up all the items on that screen. For instance if screens are 320x240px, and I look up world coordinates 400,50 then 400/320 = 1 and 50/240 = 0, so I look up things[1][0] and update all the things in that entry. If any object can move, they store themselves in the right slot after moving.

Is there any way to use this same system when Things have parallax depth? IE, something with a parallax depth of 1 is infinitely far away (and so always appears at the same location in screen coordinates) and something with parallax depth of 0 is pinned to the foreground.

I'm guessing this isn't possible, as something further away than the foreground will be in more than one screen.

What other approaches are there? I've seen an experienced game developer use a 'scroll rate' instead of a parallax depth, and he moved scenery by the inverse of the distance the camera moved multiplied by this rate. In this model I could treat scenery as per any other moving item and have it store itself in adjacent entries in the 2D array when it moves, but how does it get into the 'right' array slot in the first place? IE, if it's very distant, it could be approach from either the left or the right, and I'm not to know that in advance.

I'm very confused!


1 Answer 1


This depends whether you have:

  1. Just a small number of different levels of parallax depth
  2. A continuous (i.e. near-infinite) number of different depths

For 1), it will probably be most efficient and simpler to just treat the levels independently and query the appropriate rectangle in each level.

2) is harder. You are effectively querying a subset of a 3D space. If you interpret the parallex depth as "scroll rate" then this shape is actually a highly skewed cuboid (with the back of the cuboid fixed at the same place on the infinte depth = zero scroll rate plane). Strategies for this:

  • You could use some form of spatial partitioning algorithm to query this shape efficiently (BSP trees, octrees etc.) but be aware that these algorithms may need some tuning as they aren't typically designed for skewed cuboid queries :-).
  • Query only inside the axis-aligned bounding box of the skewed cuboid. This will get inefficient if the cuboid is really skewed / on a diagonal, but is still better than querying everything. You can use your current Set<Thing>[][] for this - just query everything in a rectangle from the current screen position to the [0,0] screen (which will contain the infinite distance / zero scroll rate background).
  • Some hybrid model, where you query subregions that together cover the whole cuboid. This could be a grid-based structure (e.g. a big 3D array where you subdivide the scroll rate dimension into 5-10 different ranges from 0.0 to 1.0). This is like a 3D version of your current approach, i.e. you could have a Set<Thing>[][][] as a data structure

Either way, I think "scroll rate" is going to be the most convenient way to this about this rather than "depth". It will make the maths and geometry easier :-)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .