How to simulate liquid using the box2d?

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

How to simulate liquid using the box2d?

Postby speedboy » Tue May 08, 2012 11:09 pm

Hi guys,
Are there someone to simulate liquid using the box2d.
I've looked at the following web site
but I still don't understand author's code.
Someone can explain. It is best to give a sample code.

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

Re: How to simulate liquid using the box2d?

Postby jacklance » Tue Nov 06, 2012 9:29 am

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);

        bi->ApplyForce(  force * delta, pi );
        bk->ApplyForce( -force * delta, pk );

Software engineer at commercial refrigeration installation

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

Re: How to simulate liquid using the box2d?

Postby genbox » Mon Dec 31, 2012 5:48 am

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.

Return to “Miscellaneous”

Who is online

Users browsing this forum: No registered users and 2 guests