Skip to main content
deleted 2 characters in body
Source Link
jgallant
  • 8.5k
  • 7
  • 35
  • 46

I've done the same thing a few years ago.

The solution was to initialize a Pool of Body Objects, and then position them all off screen. The idea is to create a set amount of bodies, and then move them around when required. I managed to locate my old code, here is the initialization code from my old project:

private void InitializeBodyPool()
{
    Bodies = new Pool<LandBody>(50);
    foreach (Pool<LandBody>.Node node in Bodies.AllNodes)
    {
        node.Item.Init(World);

        node.Item.BodyType = BodyType.Static;
        node.Item.Position = new Vector2(-9999999999, -9999999999);   //init offscreen

        PolygonShape box = new PolygonShape(1);                
        box.SetAsBox(2.5f, 2.5f, Vector2.Zero, 0);
        node.Item.CreateFixture(box);

        node.Item.FixtureList[0].Friction = 1000;
        node.Item.FixtureList[0].Restitution = 0;
        node.Item.FixtureList[0].CollisionGroup = -1;
    }
}

When you need to set a body, simply grab one from the pool, and set its position.

This may not be the proper way of handling this but it worked for me.

I took a animated gif to demonstrate how the bodies whereare swapped into position: enter image description here

I've done the same thing a few years ago.

The solution was to initialize a Pool of Body Objects, and then position them all off screen. The idea is to create a set amount of bodies, and then move them around when required. I managed to locate my old code, here is the initialization code from my old project:

private void InitializeBodyPool()
{
    Bodies = new Pool<LandBody>(50);
    foreach (Pool<LandBody>.Node node in Bodies.AllNodes)
    {
        node.Item.Init(World);

        node.Item.BodyType = BodyType.Static;
        node.Item.Position = new Vector2(-9999999999, -9999999999);   //init offscreen

        PolygonShape box = new PolygonShape(1);                
        box.SetAsBox(2.5f, 2.5f, Vector2.Zero, 0);
        node.Item.CreateFixture(box);

        node.Item.FixtureList[0].Friction = 1000;
        node.Item.FixtureList[0].Restitution = 0;
        node.Item.FixtureList[0].CollisionGroup = -1;
    }
}

When you need to set a body, simply grab one from the pool, and set its position.

This may not be the proper way of handling this but it worked for me.

I took a animated gif to demonstrate how the bodies where swapped into position: enter image description here

I've done the same thing a few years ago.

The solution was to initialize a Pool of Body Objects, and then position them all off screen. The idea is to create a set amount of bodies, and then move them around when required. I managed to locate my old code, here is the initialization code from my old project:

private void InitializeBodyPool()
{
    Bodies = new Pool<LandBody>(50);
    foreach (Pool<LandBody>.Node node in Bodies.AllNodes)
    {
        node.Item.Init(World);

        node.Item.BodyType = BodyType.Static;
        node.Item.Position = new Vector2(-9999999999, -9999999999);   //init offscreen

        PolygonShape box = new PolygonShape(1);                
        box.SetAsBox(2.5f, 2.5f, Vector2.Zero, 0);
        node.Item.CreateFixture(box);

        node.Item.FixtureList[0].Friction = 1000;
        node.Item.FixtureList[0].Restitution = 0;
        node.Item.FixtureList[0].CollisionGroup = -1;
    }
}

When you need to set a body, simply grab one from the pool, and set its position.

This may not be the proper way of handling this but it worked for me.

I took a animated gif to demonstrate how the bodies are swapped into position: enter image description here

Source Link
jgallant
  • 8.5k
  • 7
  • 35
  • 46

I've done the same thing a few years ago.

The solution was to initialize a Pool of Body Objects, and then position them all off screen. The idea is to create a set amount of bodies, and then move them around when required. I managed to locate my old code, here is the initialization code from my old project:

private void InitializeBodyPool()
{
    Bodies = new Pool<LandBody>(50);
    foreach (Pool<LandBody>.Node node in Bodies.AllNodes)
    {
        node.Item.Init(World);

        node.Item.BodyType = BodyType.Static;
        node.Item.Position = new Vector2(-9999999999, -9999999999);   //init offscreen

        PolygonShape box = new PolygonShape(1);                
        box.SetAsBox(2.5f, 2.5f, Vector2.Zero, 0);
        node.Item.CreateFixture(box);

        node.Item.FixtureList[0].Friction = 1000;
        node.Item.FixtureList[0].Restitution = 0;
        node.Item.FixtureList[0].CollisionGroup = -1;
    }
}

When you need to set a body, simply grab one from the pool, and set its position.

This may not be the proper way of handling this but it worked for me.

I took a animated gif to demonstrate how the bodies where swapped into position: enter image description here