Step by step creation of a Box2D soft body blob

As you should know, Box2D only manages rigid bodies, this means it assumes its bodies are made of the hardest material of the world, which cannot be bent or deformed in any way.

This saves a lot of CPU when handling the simulation but what if we need a soft body, such as a blob?

Around the web you can find some interesting examples based on springs taken from Boris the Brave’s controller pack but I’ve found them (and the author says the are) a bit glitchy.

So here we go with a step by step creation of a soft body blob only using distance joints.

1) Creating the bodies

The first step consists in creating the collection of bodies which will represent the soft body. There is a main body, and a number of satellite bodies, as you can see from this script:

Pay particular attention to particleNumber variable which represents the number of satellite bodies which will build the blob itself, and to particleDistance which basically is the radius of the blob.

This is the result:

These are all the bodies we need to build the blob.

2) Binding satellites with the main body

Now I am going to create distance joints to bind satellites with the main body

At the end of this step, every satellite has its distance joint with the main body:

However, just using these joints is not enough, as I also need to create distance joints between satellites

3) Binding satellites one to each other

Now it’s time to add some other joints to bind satellites one to each other, this way, so we can actually get some kind of wheel

And now the blob is ready to be seen in action:

Nothing happens because all bodies are still static

4) Making bodies dynamic

It’s time to make bodies dynamic and see what happens:

Ready to see the blob? here it is:

Unfortunately the blob is really rigid, because distance joints do not allow bodies to change the distance among them (that’s why it’s called “distance” joint).

5) Playing with damping and frequency

Too good we can play with distance joint damping and frequency to turn boring distance joints into happy springs:

And finally here it is:

Now the distance joints are not that rigid, and playing with dampingRatio and frequencyHz properties (recommended range respectively 0 to 1 and 1 to 30) you can achieve a good blob effect.

I am making a game out of it, stay tuned.

Download the source code.

Last minute edit: my Box2D book is expected to be on the shelves on October 21.

  • Zerk

    nice lesson )
    But, how to tie sprite with this body?

  • This is awesome, I am also making a game using a similar blob kind effect. Originally I was trying to stick individual blobs together but this has a really good feel to it.

    Nice!

  • This is flash version. Is this soft body implementation possible in box2dweb ? I have make games in box2dweb (http://pixsansar.com/cool-box2dweb-games) but now want to make some real games using this new idea.

  • Shawn

    Thanks for the awesome tutorial! One suggestion would be to only show the new code at each step, or at least highlight it. Would make it much easier to follow each step.

  • Jason

    I suggest using squares for the outside instead of circles; it will reduce friction and make it more realistic.

  • Paul Smith

    This is brilliant, just how on earth do you texture it? You must need some opengl/directx filter that blends a bunch of mini circle blob textures into one big blob.

  • Quagh

    Simple, but effective. Try creating a second row of outer rings and joining them inversely with the inner ring to create a strong (yet flexible) blob, one that nearly cannot be squashed (doesn’t get stuck on corners and doesn’t get objects stuck inside). Out of the 7 blob types I tested, that one seems the most strong.

    A (simple) way to represent this graphically is to draw lines to each outer circle, close the connection and fill it. Just use the default Flash graphics (line + fill).

  • ricar

    but it seems that nape has soft bodies

  • ricar
  • How can we stop that “shaking”, that non-stoping movement?

  • Janitha

    Hi Emanuel,

    I’m trying to get my blob to nicely roll over a terrain as it moves along it left to right. The linear movement is alright and the center body also rotates fine but the whole body does not rotate, just the center body. How do I get to make it work as if the blob is rolling?

    Thanks

  • PRABHAKARAN

    Nice how can i add my texture to this blob,could you help me

  • Tutorial is helpful, but can u add sprite to blob and post again.Thanks

  • Pingback: How to create a Box2D blob using Corona SDK | LazyWombat Studio()

  • omer

    how to tie sprite with this body?

  • Shorefire

    Hi, after spending a week or so working on a game involving softbody’s, I came across a oddity with the distanceJoint.

    I had a problem, apply any sort of rotation to the center body and it would not rotate the outer ones very fast, thus the body would collapse in on its self. To fix this I set BOTH anchors of the distanceJoint to the outernode, which made a sort of floating joint, this allowed me to rotate the center body to rotate the entire softbody, set a angulardamping for the center body so the softbody can only roll, etc.

    Overall I’ve found this to result in a much nicer softbody, I have not had the chance to test this outside of Lua currently, but it should work.

    Compare this (old) https://www.youtube.com/watch?v=iXIbUggvpQs&index=10
    To this (new) https://www.youtube.com/watch?v=Dv_nDcIdXKk&index=2

  • peter

    how can you texture the whole group or set a sprite to the whole group so that the sprite also deforms with the objects.