I am programming a 2D aspect scrolling sport. I at present have a finite world, consisting of blocks saved in an array. And an array of chunks containing an inventory of entities for that chunk. As of now I simply had boundaries connected to the digital camera that block the digital camera from clipping going outdoors of the bounds of the world, so an regular finite world rendering. However I would really like it to be infinite, contemplating the blocks that works fairly effectively. I divide the digital camera bounds by the scale of 1 block and I’ve the array indices that I must render.
Out of simplicity I simply submit an instance with the block centered on the digital camera place (and likewise solely with the x Coord since y would be the identical everytime):
blockX = digital camera.place.x / BLOCK_SIZE; blockX = (world.width + blockX) % world.width;
The seconds step makes positive I at all times maintain the worth contained in the world bounds.
So if the digital camera will get into the negativ, at one level I teleport it to the opposite aspect of the world. However till then the next occurs:
Think about a world width of 100 (world with = block rely; world array goes from Zero to 99 (inclusive)) and a block with of 10:
blockX = -10 / 10; => blockX = -1; blockX = (100 + (-1)) % 100; => blockX = 99; int blockID = world.blocks[blockX]; renderBlock(blockID, blockX * BLOCK_SIZE)
I get the block data from the block -1 which is block 99, as a result of the world must be infinite, however I render it at -10 as a result of the digital camera is not at present positioned on the different aspect of the world.
For the chunks I’ve the same System, I render the chunks principally the identical because the blocks. So for chunk x = -1 I render Chunk 9 (chunk dimension = 10 blocks). But when I for instance render the entities of chunk 9 whereas the digital camera continues to be at x = -5 all of the entities get rendered at their regular world place roughly 90 to 100. However they need to get rendered at -10 to 0. My query is now how would you correctly join the chunks present place with the place of the rendered entity?