## How to simulate liquid using the box2d?

Discuss issues specific to miscellaneous ports of Box2D
speedboy
Posts: 9
Joined: Sun Apr 15, 2012 8:39 pm

### How to simulate liquid using the box2d?

Hi guys,
Are there someone to simulate liquid using the box2d.
I've looked at the following web site
http://www.patrickmatte.com/index.php/2009/06/16/simulating-liquid-with-a-physics-engine-in-actionscript-3/
but I still don't understand author's code.
Someone can explain. It is best to give a sample code.

jacklance
Posts: 2
Joined: Tue Nov 06, 2012 9:21 am
Location: Clifton,New Jersey
Contact:

### Re: How to simulate liquid using the box2d?

It`s looks like stimulation of "Newton's law of universal gravitation" . If that so then consider following code:

Code: Select all

`for ( int i = 0; i < numBodies; i++ ) {    b2Body* bi = bodies[i];    b2Vec2 pi = bi->GetWorldCenter();    float mi = bi->GetMass();    for ( int k = i; k < numBodies; k++ ) {        b2Body* bk = bodies[k];        b2Vec2 pk = bk->GetWorldCenter();        float mk = bk->GetMass();        b2Vec2 delta = pk - pi;        float r = delta.Length();        float force = G * mi * mk / (r*r);        delta.Normalize();        bi->ApplyForce(  force * delta, pi );        bk->ApplyForce( -force * delta, pk );    }}`

Thanks.
Jack
Software engineer at commercial refrigeration installation

genbox
Posts: 69
Joined: Mon Jun 15, 2009 4:34 pm

### Re: How to simulate liquid using the box2d?

I really depends on what you are looking for. The link you gave is not real liquid, but a trick to make a few balls look like liquid. If you are looking for real fluids, then you need something a lot more advanced, and there are a few (4 if I remember correctly) implementations of fluids of varying degrees of complexity here on the forums.