Infinite map illusion.

Hello everyone!

I create a map storing some tiles in an array. The effect i want to achive is that when the display edge achives any border of the map, the tiles from opposite side start rendering next to the border (some sort of infinity in a big map). The problem is that i don’t know how to do this so that when the display passes the upper edge and the left edge in same time, the “corner” beween renders. Also, how should i iterate trough the array to have less CPU cost?

The zone marked with “???” is the “corner”.

Can anyone point me any idea?

You couuuuuld seperate the map in chunks, so that lets say the yellow rectangle is a chunk, the red one is a chunk, etc. and then just render the chunks that should be on the screen. Easy as pie… at least in my current thought process


xIndex = x % map.width
yIndex = y % map.height

You didn’t understand. I want my map to REPEAT, it isn’t infinite.

Determine your viewport. Iterate over the visible x and y values in that viewport. Then use the formulas I gave you.

It doesn’t matter if it’s repeat or infinite. You’ll be using the same equation either way. Infinite or not is a separate function of allowing/prohibiting an entity to move from one side to the other.

By viewport you mean the Rectangle of the screen (using xOffset, yOffset and Display.getWidth(), Display.getHeight()) ?

BTW, i’m using LWJGL. And the map is a tile-map.

Yes, but given your viewport you’ll need to determine what tile indices are visible within it and then plug them into the equations to get your map indices.

Loom_weaver’s first post was exactly what you wanted. Alternatively, consider the pseudocode:

min = ..., max = ..., range = max - min;
while(value < min) value += range;
while(value > max) value -= range;

Which might prevent rounding errors for floating point numbers or overflow issues with integral numbers. This is not the fastest method, but it’s the basic idea behind the modulus operator (remainder division.) You will need to “reset” the value within an acceptable range to prevent those types of bugs with large integers/floating points. You will have to split up the object, shift, and draw up to four separate “quadrants” (red, orange, yellow, and gray) to display the scene correctly. If you can’t split every object then you may have to draw it four times for each screen boundary (drawing most of the object off screen.)