I wan't to make an infinite side scrolling game using libgdx and box2d, to do that I would have to spawn chunks. Unlike before when I was just dealing with sprites I used to spawn sprites infinite times without problems by pooling and adding them to and ArrayList and control them with an iterator. But now I use static bodies as my terrain and pooling cannot be done with bodies so is it ok to just create a new body all the time and delete them when not needed. Is it going to slow down my game? if so what is a better way? thanks.
Update:
this is my current code which isn't going to work cause pooling is not applicable to box2d bodies. first I created the BodyDef and Body on seperate methods:
public BodyDef createDef(){
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.StaticBody;
def.fixedRotation = true;
def.position.set(6, 6);
return(def);
}
public Body createBody(){
Body body = world.createBody(createDef());
PolygonShape shape = new PolygonShape();
shape.setAsBox(1, 1);
body.createFixture(shape, 1.0f);
shape.dispose();
return(body);
}
public void createPlatform(){
Body platform = Pools.obtain(Body.class); //then use pooling
platform = createBody(); //here I set the value equal to the return value of createBody() method
bodies.add(platform);//adding platform to the ArrayList
}
My new idea now is just to create a new body by calling this method (the creation of bodies would never end because I'm trying to make an infinite side scrolling game).
public void createBody(){
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.StaticBody;
def.postion.set(position.x, position.y);
PolygonShape shape = new PolygonShape();
shape.setAsBox(size.x, size.y);
myBody = world.createBody(def, 1.0f);
shape.dispose();
} `//I'm still working on how to remove bodies`