Water simulation

by Philipp Crocoll

Keys to use:
  • w,a,s,d:   forward, left, back, right
  • y,x:   look down, look up
  • r,f:   move up, move down
  • c,v:   strafe left, strafe right
  • 1:   turn on/off rain simulation
  • 2:   turn on/off solid mode
  • 3:   turn on/off lighting


  • General about the simulation
    The water surface consists of a certain number of "oscillators". These oscillators can move up and down. Each oscillator is represented by a vertex, which is part of a vertex array (see my Vertex-Array-Tutorial).
    Before we render a new frame, we have to update each oscillator. This is done by having a look on the four direct neighbors: If they are higher than the current oscillator, it is accelerated upwards. Then they are moved by their current velocity.


    The exciters
    Exciters are oscillators which are controlled by an "outside force". They cause the waves, by bringing movement into the surface. The "outside force" forces them to move up and down as a sine function. This could be changed, but it creates good looking waves.
    The exciters are stopped after a certain time (g_fExcitingTime), because they would bring more and more energy into the system, which would cause higher and higher waves.
    Because of the simulation technique, each oscillator may be an exciter with it's own frequency and amplitude. See end of CreatePool() how to create exciters.


    Reflexions
    The reflexions at the edge of the "pool" are simulated as reflexions at fix ends. You could add the lines for free ends: The calculation of AvgDifference (the average y-distance of the neighbor oscillators) must be change, the rest remains.


    Normal vector calculation
    After the oscillators' y-values have been updated, also the normal vectors must be recalculated. This is only required for lighting (but I do not switch it off if lighting is off).


    Number and distance of oscillators
    These values can be easily changed at the head of main.cpp. Note that many oscillators require much cpu power!


    Oscillator weight
    In order to simulate the waves, oscillators have an inertia which only exists, if the oscillators have a weight (I'm sorry if my expressions are not 100% correct - I am not a native english writer!). This weight can be set (but do not set it two low!). It is a constant and valid for all oscillators (you might get interesting waves, if you have different values within the "pool").


    The rain simulation
    It is not a very good simulation but looks funny: If you randomly set an oscillator's y-value down (smaller than 0), it also creates little wave.


    **************************************************************************

    Any comments? Conact me!

    philipp.crocoll@web.de

    www.codecolony.de