1
votes

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`
2
Your question is anything but clear. Can you please rephrease your question, tell us specifically what you tried, show us the code what you have now. We cannot simply "guess" what your code looks like or what you have done so far without proper clarification.Tschallacka
There is no way for us to tell if something you do will slow down your game. We don't have your game.khelwood
Pease check my update and sorry.Kevin Bryan
I don't see why bodies are not poolable. Why are you making that assumption?Tenfour04
because I'm getting errors? and also someone told me that bodies should always be initialized like this //Body body = wold,createBody(def);Kevin Bryan

2 Answers

2
votes

Yes. It's alright to create and delete bodies indefinitely.

Depending on how many bodies you're creating at a time the allocation and garbage collection involved might end up lagging your game.

If your game does end up lagging you'll need to switch to using pooling. You can use setTransform to move box2d bodies from the left hand side of the screen to the right hand side.

0
votes

DylanVann has already answered the question but i just want to point out in that function is instead of creating new objects of BodyDef and Shape, you can even reuse that.

Keep the reference of BodyDef alive and even Shape if you are creating and destroying shapes too frequently(like 3-4 times a sec).

 public void createBody(){
        def.type = BodyDef.BodyType.StaticBody;
        def.postion.set(position.x, position.y);

        shape.setAsBox(size.x, size.y);
        myBody = world.createBody(def, 1.0f);
 }