If you need to implement water that works as a level controller, and bodies hitting it float, then SPH ( Smoothed Particle Hydrodynamics ) will not help us. The implementation of this type of water is good, for example, described here . 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, absolutely unrelated subtasks:
Physics (Model)
Graphics (View)
Physical world
For the implementation, the physics engine Box2D was chosen. In fact, everything is very simple: it is necessary to create many identical small particles that have the following parameters:
The particles are smooth, i.e. Friction = 0;
Round particles;
Particles interact with each other.
The number of velocity and position counts ( velocityIterations & positionIterations ) can be set to 3 and 1, respectively. This is quite enough to ensure normal operation. The result can be seen on a brief video demonstration.
As seen in the physical world, everything “lives” beautifully as it should.
Graphics
There are several ways in which you can draw water from the physics engine.
The first and how, at first, it seemed to me the most reasonable - to select groups of objects, and circle the resulting non-convex ( concave ) polygons using triangulation. I spent a few days on this wonderful way, and as a result I didn’t get anything good, there was no point going on. ')
Another way: to draw particles of water in 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, a method of masking a mask onto a drawing layer was tested using the openGL ES 1.0 features using the following method .
As a result of using the five CCRenderTexture, quite good water was obtained with the effect of motion blur and distortion of the image translucent through the water.
Optimization
To the great chagrin, it all terribly slowed down on iPad1, working well on iPad2 and iPhone 4S. Of course, such a result could not be considered satisfactory. A lot of work has been done on optimizing the code, but this has changed almost nothing. After researching who is faster than Chimpmunk or Box2D , it turned out that Chimpmunk really works faster. Nevertheless, the result still continued to slow down. Up to 100 particles could be used, on iPad1 which is generally 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 solutions are shaders and the use of OGL ES 2.0. It is good that there is Cocos2d 2.0 Brunch c OGL ES 2.0. The transfer to which of Cocos2d was absolutely 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.