Cocos2D SPH-based water



    If it is necessary to realize water that works as a level controller, and the bodies that come to it float, then SPH ( Smoothed Particle Hydrodynamics ) will not help us. The implementation of this type of water well, such as described herein .
    The task was to realize water precisely on the basis of the hydrodynamics of smoothed particles.
    The implementation can be divided into two, in general, completely unrelated subtasks:
    • Physics (Model)
    • Graphics (View)

    Physical world


    For implementation, the Box2D physics engine was chosen .
    In fact, everything is very simple here: it is necessary to create many identical small particles that have the following parameters:
    1. The particles are smooth, i.e., Friction = 0;
    2. The particles are round;
    3. Particles interact with each other.

    The number of speed and position calculations ( velocityIterations & positionIterations ) can be set to 3 and 1, respectively. This is quite enough to ensure normal operation.
    The result can be seen in a short video demonstration.

    As you can see in the physical world, everything perfectly "lives" as it should.

    Graphics


    There are several ways in which you can draw water received from a physical engine.

    The first and how, it seemed to me the most reasonable at first - to select groups of objects, and circle the resulting non-convex ( concave ) polygons using triangulation.
    I spent several days on this wonderful method, and as a result I did not get anything good, there was no point in continuing.

    Another way: draw water particles with circles of larger diameter than physical objects. This is a very simple way, but in order for the water to look like water, you need to make it transparent, and apply a number of other special effects.
    To begin with, we tried a method of applying a mask to the layer being drawn using the capabilities of openGL ES 1.0 using the following method .

    As a result of using five CCRenderTexture, we obtained quite good water with the effect of motion blur and distortion of the image visible through the water.


    Optimization


    Much to the chagrin of all this terribly slowed down on the iPad1, working well on the iPad2 and iPhone 4S.
    Of course, it was impossible to consider such a result satisfactory.
    A lot of work has been done to optimize the code, but this has not changed much.
    After research on who runs faster than Chimpmunk or Box2D, it was found that Chimpmunk really works faster.
    Nevertheless, the result still continued to slow down. It was possible to use up to 100 particles, on iPad1 which, in general, is not enough.
    It was concluded that the graphical implementation slows down, while the physical part works quite satisfactorily.

    Cocos2d 2.0 Brunch, Shaders


    The obvious solution to this is shaders and the use of OGL ES 2.0.
    It's good that there is Cocos2d 2.0 Brunch with OGL ES 2.0. Transferring to which of Cocos2d turned out to be completely painless and simple.
    Using a masking and distorting shader made it possible to make water with 300 particles on the iPad 1 and FPS 30.
    Video of the result at the beginning of the topic.

    Also popular now: