2 水波算法 Water Ripple Simulation

1 波源產(chǎn)生 Water Ripple Generation

水面不會憑空振動,需要給水面施加一個振動波源。
論文里面沒有提如何產(chǎn)生波源,不同初始化波源的方式得到的波形也不一樣。常見的一種方法是圓形初始化。

Without external forces, the water surface would stay calm. In order to have water ripples, vibration sources must be added to the water. The style of the water ripple is different with different ways of initialization. The most common way is to initialize a circle area to a specific value.

引用引用運(yùn)行體的水波生成與擴(kuò)散模擬算法的話:
Simulation Algorithms of Water Wave Caused by Moving Body:

水波初始振蕩發(fā)生時 ,其原動力是負(fù)向的 ,振動向負(fù)方向偏移。
振蕩發(fā)生后 ,振動立即會向水面的各個方向傳遞 ,
其傳播到的水域內(nèi)各點(diǎn)的水紋高度應(yīng)為該點(diǎn)在上一時刻的振動狀態(tài)與其相鄰點(diǎn)在更前一時刻的原振動狀態(tài)的綜合效應(yīng)。

According to wave theory, when the oscillation of water ripples happens, 
the vibration would move along the negative z-direction and this vibration would spread to other directions in xy surface 
which means a negative impulse should be added to the mesh to cause the vibration.

對以(x,y)為中心r為半徑的區(qū)域范圍 z = A(x,y) = n 進(jìn)行初始化(施加一個負(fù)的尖脈沖)實際上正的也沒什么區(qū)別。。
r=1時就是一個點(diǎn),模擬雨點(diǎn)效果不錯。簡化版:

A(x,y) is a round area with the center(x,y) and the radius r. And use A(x,y) to initialize the mesh(Adding a negative impulse to the mesh, a positive one has no differences).
If r=1, then we just change one value of the node in the mesh. It is not bad to simulate the raindrop effect.

void init(int x, int y, float energy)
{
     nwater2[i][j] = energy;
}

2 水波演變公式推導(dǎo) Water Ripple Propagation

文章給出了水波演變公式的推導(dǎo)過程,推導(dǎo)過程不是很復(fù)雜。
這里簡單介紹一下。
波浪力學(xué)里面用小波變換來描述施加擾動后自由面的高度:

The article has the derivation process of the water ripple and it is not difficult.
In wave mechanics, small amplitude wave is a common and basic model to describe water wave. According to the small amplitude wave theory, the water height can be described as follow:

\eta = -\frac{\omega}{g}D\cdot coth(kh)\cdot sin(\omega t+k_{1}x+k_{2}z+\phi)

本質(zhì)上是一個帶一堆系數(shù)的sin函數(shù),參數(shù)意義在這里不是很重要。
Generally, it is a complex sine function where parameters are not important here.


Let
\hat t = \frac{t_i+t_{i+1}}{2}, \Delta t = t_{i+1} -t_i (i\ge0)

利用三角函數(shù)和差化積公式可以推出
This function can be deduced:

\eta(x,z,t_i) + \eta(x,z,t_{i+1}) = 2\eta(x,z,\hat t) cos\frac{\omega\Delta t}{2}

由于水波自身特點(diǎn),就是每個點(diǎn)的高度都受周圍的點(diǎn)的高度影響。理想的波是各向同性傳播的,假設(shè)這里的水波也是理想情況。
那么這個\eta(x,z,\hat t)可以用周圍的點(diǎn)的高度來估計。
假設(shè)網(wǎng)格長這樣:

Ideally, water wave propagation is isotropic. And the height of a cell is affected by the heights of its adjacent cells in the previous moment.
Then \eta(x,z,\hat t) can be estimated by the heights of its neighbor points.
Suppose the mesh is like this:

mesh03.png

\eta(x,z,\hat t)= \frac{1}{r}\sum\limits_{j=1}^{r}\eta(x_j,z_j,t_i)
這里r是4,那么令2cos\frac{\omega\Delta t}{2} = 1,這里需要注意能量守恒。如果r是8,需令cos\frac{\omega\Delta t}{2} = 1。論文中只提了r = 8的情況。

Here, r is 4,let 2cos\frac{\omega\Delta t}{2} = 1, and the law of energy conservation should be taken into consideration. If r is 8,then cos\frac{\omega\Delta t}{2} = 1. The article only mentions when r = 8.

所以演變公式可以變?yōu)?br> Then the evolving formula is
\eta(x_k,z_k,t_{i+1}) = \frac{1}{4}\sum\limits_{j=1}^{4}\eta(x_j,z_j,t_i)- \eta(x_k,z_k,t_i)

3 水波消散 Fading Process

公式理想情況下能量符合能量守恒定律的,但現(xiàn)實情況能量會減小但不能增加。如果能量增加,水面效果會爆炸。為了使水波消散,我們對得到的\eta乘上一個絕對值小于1的系數(shù)即可。
Ideally, it has to follow the law of energy conservation. But in reality, the energy can have the loss. It the energy increased, then the effect of the water surface would be a mess. We just to multiple a variable which has a value between 0 and 1 to make the ripple fade away.

void spread()
{
    for(int i = 1;i < SIZE_WATER-1; i++)
    {
        for(int j = 1; j < SIZE_WATER-1; j++)
        {
            float y = nwater1[i-1][j] + nwater1[i+1][j] + nwater1[i][j-1] + nwater1[i][j+1];
            nwater2[i][j] = y * 0.5f - nwater2[i][j];
            nwater2[i][j] -= nwater2[i][j] / 32.0f;
        }
    }
    for(int i = 1;i < SIZE_WATER-1; i++)
    {
        for(int j = 1; j < SIZE_WATER-1; j++)
        {
            ntmp[i][j] = nwater1[i][j];
            nwater1[i][j] = nwater2[i][j];
            nwater2[i][j] = ntmp[i][j];
        }
    }
}

水波動畫效果如下:
The water ripple effect:

mesh04.png
mesh05.png
mesh06.png
mesh07.png
mesh08.png
mesh09.png
mesh10.png

這個算法厲害之處在于他這個簡單規(guī)則可以有效的處理多個波互相干擾和障礙物反射。增加了能量耗散系數(shù)對水波的消退也能很好的模擬。
This algorithm is very awesome that its simple rules can easily handle the interaction of many waves and the obstacles. Adding a loss can simulate the fading process well.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容