# Dynamic body implementation

I am writing a 2D game where one of the characters has some very particular requirements. This character is a body with no particular shape (similar to a fluid, but not so much), it has to be able to grow and shrink (as in actually growing, not just scaling), and it has to have collision detection (even if it's basic). Because of this requirements, it obviously can't be based on a sprite, so direct rendering of the shape should be the logical thing to do.

I assume this is no easy task, but I just couldn't find a good physics engine that covers these requirements (or at least no tutorial on how to do it; I particularly searched for Box2D tutorials).

Is there a way of doing this with Box2D, SDL, or any other physics or game engine out there?

If not, what's a good place to start? I am really clueless as far as soft-body physics are concerned.

• This article from Cowboy Programming might be a good read. – user14497 Mar 21 '12 at 2:23
• @K.G. Thank you :) together with Valmond's answer, your answer helped me come up with a solution. – arturovm Mar 22 '12 at 0:42

I'd go with spheres.

When you grow, add new spheres to the existing ones (at different directions) more to the 'base spheres', less to the farther away ones, organized like this :

Sphere B is 100 units from sphere A at "vector" 0.7 ; 0.3 ; -0.4

or just use the final vector 70.0 ; 30.0 ; -40 to connect B to A.

You can even predefine the link points on the spheres so you always grows the blob the same way or go more random if you'd like that.

If the blob must "animate" you can simply interfere with the distance the spheres are connected with :

Say a "heart beat" value

float v=1.0 + sin(time)*0.2;


that affects the vectors connecting the spheres.

Using predefined tweak values for each distance can be used to break uniformity:

//each connection has this values:

Vector v; // the (70.0 ; 30.0 ; -40) values
float timediff; //animates a bit before or after if <0 or >0
float influence; //at zero, there is no animation, at 2.0 the animation is twice the base one

Vector vOut= v *  (1.0 + (sin(time+timediff)*0.2*influence));//time is the global time, 0.2 is the global animation power


Drawing can be done using simple sprites or using a particle engine (so for example, SDL can be used).

• Thanks! Based on your answer, I came up with doing it with an articulated node structure. What I still don't understand is the drawing part, are you talking about drawing each sphere separately, one sprite per sphere? How would I cover the entire set of nodes (spheres in your case) with a single texture? – arturovm Mar 21 '12 at 22:24
• I'd just draw the spheres as sprites (or particles), maybe sort them first (farthest to nearest if there is semi-transparency, the other way around otherwise). From there there are a lot of ways to optimize, say make a triangle soup with all triangles acting as billboards, using some advanced culling techniques (but that won't work well with semi transparency), draw low res spheres on a texture, size it up and do some sort of image processing on it (border detection for example + filling the inside) etc. etc. – Valmond Mar 22 '12 at 8:29

I'm not entirely sure about what physics engine or language you are using, as Box2D and SDL are used for two different languages...

Either way, I'd recommend using circles and using those to generate a jelly-like form, and just giving them a high friction, so that the circles don't fly everywhere.

I'm not sure what you mean by "actually growing, not just scaling", because growth of a character is scaling of the sprite, and raising different stats...

please, take a look at the Wikipedia article for "soft body dynamics" and http://cg.skeelogy.com/?download=SoftBodyPhysicsTutorial these have been recommended for soft body objects (which is really what your character is, it just has a container around it).