This project has moved and is read-only. For the latest updates, please go here.

Blob physic with Farseer

Oct 30, 2008 at 6:37 PM
Edited Oct 30, 2008 at 7:19 PM
Hi, first excuse my poor English, frenchies are not very good in English :]

For my new game I require a physic engine.
And I found Farseer : Simple, robust, easy to integrate into an existing game, exactly what I search!

Nevertheless, I have some problems to use correctly Farseer in my game.

My goal is to construct a blob structure.

Each blob (BlobGroup class) is composed with points (BlobEntity class) organized in archimedes' Spiral.
Each point is linked with his neighbour.

In my game each point (BlobEntity) are construct like this:

//------------ BlobEntity code
Body bodyTemplate = BodyFactory.Instance.CreateCircleBody(Map.PhysicsSimulator,0.5f/60f, 3f);

//--- Body
Body body = BodyFactory.Instance.CreateBody(Map.PhysicsSimulator, bodyTemplate);
body.Position = position;

//--- Geom
Geom geom = GeomFactory.Instance.CreateCircleGeom(Map.PhysicsSimulator, body, 0.5f, 6, 0.0f);

geom.FrictionCoefficient = 2f;
geom.RestitutionCoefficient = 0.01f;

geom.CollisionGroup = 1;
geom.CollisionCategories = FarseerGames.FarseerPhysics.CollisionCategory.Cat1;
geom.CollidesWith = FarseerGames.FarseerPhysics.CollisionCategory.All;


There is no collision between BlobEntities in a BlobGroup but there is collision between BlobGroups, that's ok.

Then my algorithm creates links between my BlobEntities, but here is my problem, which type of link must I use?
With my actual Joint/Spring construction, my blob is unstable, my BlobGroup do not keep a "blob form" and worse, sometimes it explodes.

Here is a picture of my BlobEntities distribution and links between their :

Spirale Blob

Diagram 1 : BlobEntities (circles), left/right neighbours links (black) and up/down neighbours links (blue)
Diagram 2 : BlobEntities (circles), left/right neighbours links (black)
Diagram 3 : BlobEntities (circles), up/down neighbours links (blue)

At the moment, blue links are constructing like this:

//--- Blue link code
float SPRING_CONSTANT = 50f;

LinearSpring spring = SpringFactory.Instance.CreateLinearSpring(Map.PhysicsSimulator, blob1.Body, Vector2.Zero, blob2.Body, Vector2.Zero, SPRING_CONSTANT, DAMPNING_CONSTANT);

Black links are constructing like this:

//--- Black link code
// restlength is distance between blob1 and blob2
// vecAttach1 and vecAttach2 are calculated to be on the side of blob1 and blob2 as you see on the picture.

Joint joint = JointFactory.Instance.CreateSliderJoint(Map.PhysicsSimulator, blob1.Body, vecAttach1, blob2.Body, vecAttach2, restLength - 0.01f, restLength + 0.01f);

I tried to add an AngleLimitJoint on each points with min = -0.1, max =0.1 but the problem is the same.

So, do you have an idea to construct a good stable blob with Farseer?

Thanks a lot,

Oct 30, 2008 at 7:10 PM
@Geff - I have a concept blob design already complete and would gladly send you the demo but my laptops battery is bad and I am waiting on a new one. I did send the code to Ian (genbox) so maybe he stills has it. To me it looks like you are simply using to many joints and springs. Farseer is good but it doesn't like to many interconnected joints. My blob design was a simply circle of Bodys connected by PinJoints. Then you either connect springs across the circle or create a center Body and connect each Body on the circle with a spring to the center Body. This method created very stable blobs.

Another idea is to only connect your your blobs Bodys with springs. Each Body would have a spring to the other Bodys closest to it.

If you need a picture to explain my designs I would be glad to draw some up for you.

How are you planing on rendering the blob, if you don't mind?

Oct 30, 2008 at 7:31 PM
Edited Oct 31, 2008 at 9:50 AM

Your blob design is a real blob design, like here :

Blob structure

But in my game blobs are not just blobs they are metaballs too :]

My rendering engine use shader to render each blob as a metaball.

You can see 2 screenshots of 3 blobs here :

3 blobs

3 blobs zoom

So i must have BlobEntities into my blob, organized in spiral.

If I rely each blobentities Body to the closest, bodies that are on the side can go trough the blob structure, spring can cross themself.

Oct 30, 2008 at 8:16 PM
Uhhh, shiny screenshot :) Reminds me of the cell in biology battle ( I think it was written in XNA.

It's true that Mattbettcher send me a demo with "soft bodies" Very impressive demo by the way.
Sadly my second harddrive failed on me shortly after. Did not put it on my SVN server with the rest of all my Farseer files.

Anyways. I think Matt is able to help you. He is a busy man, but also very helpful :)
Oct 30, 2008 at 9:24 PM
@geffd: I havent had much success with joints / springs myself, either, but I thought it may be helpful to point you in the direction of the PhysicsSimulatorView that is part of the demos. If you use it, you may be able to visualise what exactly is happening. I've created a stand-alone version for easier use - you can find in one the patches page.
Oct 31, 2008 at 10:13 AM
@genbox, thanks, but it's only the beginning ;)
Biology-battle looks really great, it's a good source of inspiration!
I want to ab-use of physic in my game, with rotating blobs, attraction/repulsion, élastic blob, so Farseer seems perfect.

@DrDeth, I already use the PhysicSimulatorView, it is very helpfull, but the problem is which type of joint/spring/parameters must i use.

@Mattbetcher, perhaps can I mixed your idea (a blob structure with double layer of circles that are link to the center circle) and my spirale blob structure.
Blobentities (circles) that are inside the blob are links together with a spring.
Oct 31, 2008 at 5:40 PM
I've never made blobs like this, (or of any kind for that matter!) but I have a theory for another approach?  What if you just manually applied impulses to the bodies toward or away from other nearby blob bodies each frame, rather than using springs?  The blob parts should only collide with objects that aren't blobs. (Like walls.)

Here are my thoghts.  Let's say A and B are seperate blob groups.

A1 - - - - - A2, Two parts of the same blob, very far from each other.  No impulses.  At this distance, they should be seperate blobs of the same group?
A1 - - - - A2, Close enough to each other to be one blob, but still far away.  Add strong impulses for both parts toward each other.
A1 - - - A2, Same situation, but a bit closer.  Add weak impulses.
A1 - - A2, Just the right distance.  No impulse.
A1 - A2, Too close.  Push them away from each other.
A - - - - B, Two seperate blob groups, far from each other.  No impulse.
A - - - B, Close enough to be merged, but since they are from seperate groups, push them away from each other with a weak force.
A - - B, Now they're way to close.  Strong impulses away from each other.

Let me know if this makes any sense.  I'd kinda like to try it myself. =P It's totally just an idea, so it may be unstable as heck.

Never thought thought about having shaders generate the metaball geometry.  I wish I knew enough about them to try it.  Sounds pretty cool.
Oct 31, 2008 at 7:12 PM
@Yota - sounds like a good idea. I'm sure you'd have to have some kind of damping in there some where but a good idea. You could even make the blobs break apart at a certain threshold just like Joints do in Farseer.