1

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`
Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45
  • 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 Nov 09 '15 at 15:49
  • There is no way for us to tell if something you do will slow down your game. We don't have your game. – khelwood Nov 09 '15 at 16:01
  • Pease check my update and sorry. – Kevin Bryan Nov 09 '15 at 16:08
  • I don't see why bodies are not poolable. Why are you making that assumption? – Tenfour04 Nov 09 '15 at 22:26
  • 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 Nov 10 '15 at 06:37
  • for other people stumbling into this question - [bodydef javadoc](https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/BodyDef.html) – eis Nov 10 '15 at 10:49
  • probably the best thing to do is to test it. In theory, as long as your garbage collection can keep up, you shouldn't have an issue. – Marshall Tigerus Nov 10 '15 at 15:25
  • I'll try, do you know a proper way of creating chunks? even a tutorial link would be helpful, I'm new to this I still don't have a proper understanding of chunks. – Kevin Bryan Nov 10 '15 at 15:28
  • A "chunk" is just a created piece of your world which can align along with other "chunks" as seamlessly as possible. In minecraft, each "block" could be considered a chunk - which was generated within larger chunks - which don't blend together very well. (*Desert next to Snow*) - in other words you can just randomly generate stuff and throw it together to qualify as "chunks" – DoubleDouble Nov 10 '15 at 16:07
  • If the bodies are identical then they're definitely poolable. You'll need to use setTransform to move the bodies from off the left side of the screen to off the right side of the screen. – DylanVann Nov 10 '15 at 17:23
  • That being said creating new bodies is the easy way to go, just make sure to delete the bodies going off the left side and make sure to profile to make sure that creating the new bodies isn't lagging your game too much. – DylanVann Nov 10 '15 at 17:24

2 Answers2

2

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.

DylanVann
  • 483
  • 5
  • 9
0

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);
 }
Kush
  • 755
  • 7
  • 22