Paweł Mogiła's game development blog

water February 11, 2012

Filed under: gamedev,videos — szaman @ 19:57


It’s been long time since last post, but it wasn’t wasted (i hope so). I fixed a lot of bugs and made few changes that improves performance of engine. Most important upgrade applies to lighting – previously I was clearing screen alpha component for each visible light with glClear(…) then i noticed that this is the bottleneck of lighting system, now I’m just drawing quad with certain dimensions  which set alpha to zero – this approach is faster. Additionally i restrict shadow drawing area to AABB of current light via glScissior(…)  – this helps because shadows were projected to “infinity” (far away of light) what consumes a lot of fillrate.

OK that was not interesting part of work I have done. Let’s look closer on new features (yey) I added.The easiest to  see and most complex is water. To really to talk about it, we must to split it into two planes:

graphics part: which is responsible for general outlook of water so looking wavy and… splashes when something is getting into water or out of water. Splashes are made of particles of which I have mentioned some time ago. Simply – physics part of water reports when, where and how big splash (determined by velocity) should be shown. Splash also is modifying water surface to generate waves on water which are simulated by modified algorithm described in presentation Real Time Fluids in Games (link) by Matthias Müller which general rule goes like this:

u[N] - water surface peaks
v[N] - velocity of peaks

for(unsigned int i=1;i<N-1;++i)
    v[i] +=(u[i-1] + u[i+1])/2 – u[i];
    v[i] *= 0.99;
    u[i] += v[i] *delta_time;

when splash occurs i simply add velocity to certain group of vertices, negative velocity when something falls into water, positive otherwise.
– physics part – responsible for applying “fake” buoyancy force. Linear buoyancy itself isn’t hard – just add central force to body. The problem  is with correct rotating body in water which is not so obvious. I was searching  how to do that, i found that is implemented in bullet but it was not enough flexible and complicated (complicated = much lines of code = probably slow 🙂 ) I was looking for something lightweight. I also found that someone implemented buoyancy controller in box2d (see and flash demo) but this also was to heavy (computing centroid including rotations). So I came up with my own solution, very approximate btw. It’ based only on AABB of objects so it’s very quick. Most of physics engines uses AABB so i have more than half at the beginning. Next step is to try to minimize object AABB if it is floating on water by applying torque in specific direction. That’s all. As I said,  it has nothing with real buoyancy  but for my purposes it is enough (we all know game programing is art of cheating). Moreover I added new parameter for physics objects which decide if object would sink or float (x<1.f sink ; x>1.f float ; x=1.f stay).

I got some concepts for levels with water… and maybe You have ideas for some puzzles with such mechanics? (yes fluids can also kill like toxic water or immortal lava <pshhhh>).

On video you can see actual state of game / engine. I added some interesting items to this map like trampoline (on the left in beginning) but during video recording I forgot about jumping on it several times to show how it behaves. I collected also new nice textures and climatic ambient sounds that creates climatic scary atmosphere. Of course you should enable full screen for better look.