Soft body /Gish challenge [Theory]

Discuss issues specific to the Flash/AS3 port of Box2D
hippocoder
Posts: 115
Joined: Thu Dec 25, 2008 4:54 pm

Soft body /Gish challenge [Theory]

Postby hippocoder » Wed Jan 07, 2009 9:13 am

Hi all, I've been trying to get soft body like effect as gish within Box2D. I have managed it using boris springs but the structure is wildly unstable with the springs components.

I am starting this thread for people to put around some theory on what might be a very simple way to simulate soft bodies. All I am looking for is a simple squishy ball.

Some ideas to get us started: How about the pressure model combined with a simple circle of balls connected by a revolute joint for each?

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Soft body /Gish challenge [Theory]

Postby ewjordan » Wed Jan 07, 2009 12:24 pm

We discussed this a while back, I put up some demos on the thread at viewtopic.php?f=3&t=1504&st=0&sk=t&sd=a&hilit=constant+volume&start=10 using Java. It's pretty easy to get the effect itself, the real trouble is to get it working right with everything else that Box2d does.

The demo at http://jbox2d.nfshost.com/blob/ uses a constant volume joint in JBox2d - there are several types of dynamics you can get with that method, hit right to cycle through them. I never got things quite right when integrating with warm starting and all that, but with the right tuning you might be able to make a similar method work...that is, if you ported the code to AS3 from Java, of course.

There's also one that's probably closer to the Gish dynamics at http://www.ewjordan.com/processing/VolumeBlob/, but which doesn't use Box2d - IIRC, Gish actually used a particle based Verlet simulation, which is the method I used there (and which doesn't translate to Box2d very well for various reasons).

hippocoder
Posts: 115
Joined: Thu Dec 25, 2008 4:54 pm

Re: Soft body /Gish challenge [Theory]

Postby hippocoder » Wed Jan 07, 2009 12:48 pm

The demo at http://jbox2d.nfshost.com/blob/ uses a constant volume joint in JBox2d


The java blob demo, the 2nd demo (hit right once) is perfect for my needs. May I please see the source for that one? Its got the right kind of feel for what I'd like to do.

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Soft body /Gish challenge [Theory]

Postby ewjordan » Wed Jan 07, 2009 1:25 pm

Absolutely, it's all up on Sourceforge. The following files are the relevant ones, I think:

ConstantVolumeJoint.java: http://jbox2d.svn.sourceforge.net/viewv ... iew=markup

ConstantVolumeJointDef.java: http://jbox2d.svn.sourceforge.net/viewv ... iew=markup

BlobTest2.java: http://jbox2d.svn.sourceforge.net/viewv ... iew=markup

All the blob tests (which differ mainly just by changing the parameters) are viewable from http://jbox2d.svn.sourceforge.net/viewv ... bed/tests/

hippocoder
Posts: 115
Joined: Thu Dec 25, 2008 4:54 pm

Re: Soft body /Gish challenge [Theory]

Postby hippocoder » Wed Jan 07, 2009 2:53 pm

Thank you very much, you're a star :)

hippocoder
Posts: 115
Joined: Thu Dec 25, 2008 4:54 pm

Re: Soft body /Gish challenge [Theory]

Postby hippocoder » Wed Jan 07, 2009 3:48 pm

ewjordan,

I only need to convert BlobTest2.java and ConstantVolume.Joint.as?

Will the new joints automatically be updated by the engine or do I need to call an Update() routine somewhere? I am just wondering if it'll update naturally each world.step?

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Soft body /Gish challenge [Theory]

Postby ewjordan » Wed Jan 07, 2009 11:26 pm

You'll need to make the joint def as well, and if you've got all that, then it should integrate automatically - joints are all solved the same way internally, so this basically just has to implement the right interface and it should work (it's a lot easier than creating new shapes). Follow more or less the way the other joints are done in AS3 if you have any trouble, there might be some subtle differences that I don't know about (I don't know the internals of the AS3 engine very well).

BorisTheBrave
Posts: 1911
Joined: Mon Jan 07, 2008 10:51 am
Contact:

Re: Soft body /Gish challenge [Theory]

Postby BorisTheBrave » Thu Jan 08, 2009 4:45 am

Nope, there shouldn't be any major differences. The only major difference between AS3 and C++ is the occasional pass-by-reference, but that's not going to be a problem when coming from java. If you do port this, you should then submit it to the SVN repository.

hippocoder
Posts: 115
Joined: Thu Dec 25, 2008 4:54 pm

Re: Soft body /Gish challenge [Theory]

Postby hippocoder » Thu Jan 08, 2009 8:06 am

I ran out of time for my deadline to be able to port it at present (although its far better than my current method).

I am using:

Code: Select all

       public static function AddSpringForce(bA:b2Body , bB:b2Body , k:Number , friction:Number , desiredDist:Number)
      {
         //var pA:b2Vec2 = new b2Vec2();
         //var pB:b2Vec2 = new b2Vec2();
         //var diff:b2Vec2 = new b2Vec2();

         
         var pA:b2Vec2 = bA.GetPosition();
         var pB:b2Vec2 = bB.GetPosition();
         var diff:b2Vec2 = new b2Vec2(pB.x-pA.x , pB.y-pA.y);

         var vA:b2Vec2 = bA.m_linearVelocity;
         var vB:b2Vec2 = bB.m_linearVelocity;

         var vdiff:b2Vec2 = new b2Vec2(vB.x-vA.x , vB.y-vA.y);
         
         var dx:Number = diff.Normalize(); //normalizes diff and puts length into dx
         var vrel:Number = vdiff.x*diff.x + vdiff.y*diff.y;
         var forceMag:Number = -k*(dx-desiredDist) - friction*vrel;
         
         //diff.mulLocal(forceMag); // diff *= forceMag
         diff.x *= forceMag;
         diff.y *= forceMag;
         
         bB.ApplyForce(diff , bA.GetPosition());
         
         var diff2:b2Vec2 = new b2Vec2(diff.x *=-1 , diff.y *=-1);
         
         bA.ApplyForce(diff2 , bB.GetPosition());
         //bA.applyForce(diff.mulLocal(-1.0) , bB.GetPosition());
         
         bA.WakeUp();
         bB.WakeUp();
      }


Ported from ewjordan's java snippet somewhere on the forums.

I realise that help is a 2-way street and I will definately help the community back whenever I can, so if I do port it, the SVN will have it :) plus any questions I can answer for people. At the moment I'm still "getting there" :)

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Soft body /Gish challenge [Theory]

Postby ewjordan » Thu Jan 08, 2009 9:34 am

FWIW, now that the distance joints can be "soft," you could also use those instead of that spring force code, it might have better stability for stiff springs (if that's what you need). When I originally posted that code, there was no soft distance joint yet.


Return to “Flash”



Who is online

Users browsing this forum: No registered users and 1 guest