# How to spawn Chunks in Libgdx?

I'm currently working on a 2D platforming game with an infinite map.

I did my research and I discovered that I can use chunks. Based on my understanding chunks is about spawning terrain at the right time and then removing them when not needed.

My idea was to pool a body then add it to an array list so that I can use a static body and remove it any time without any problems.

But then I realized that Pooling is not applicable to bodies.

How can I spawn chunks, without creating a new body every single time and then remove it when not needed?

• Have you found an actual performance problem? Are you sure creating/disposing of chunks as needed isn't a fine solution? – Richard Tingle Jan 7 '16 at 22:00

I would suggest changing position of already created chunks.

For example:

if(exampleChunk.getX()-exampleChunk.getWidth()<0){
exampleChunk.setX(Gdx.graphics.getWidth());
}else{
exampleChunk.setX(exampleChunk.getX()-(200*delta);  //delta being the render(float delta) value.
}


Simply use this code for every chunk every time you update the state of your game.

I find it pretty easy, and you won´t need to dispose or create any chunks.

Hope it helps, tell me if you have any question.

EDIT: This is just an example, you can also update Y value, or whatever changes you want to make, I think it is simple enough to understand it, even with my poor english.

• I was thinking of doing this before but I didn't want to create more than 5 chunks, haha my bad. – masterYao09 Jan 7 '16 at 17:03