Water Wave Simulation Algorithm

Source: Internet
Author: User

I, Theoretical Basis

The physical model of water waves is the theoretical basis. Water Waves have the following features:

Diffusion:Water Waves always spread out from the center of the disturbance. In the process of water wave diffusion, each point vibrates at its own center after obtaining the energy, and transmits the energy to the surrounding area. The reason for the spread from the interference point is that the internal points of energy offset each other and do not see the shock. It is important that each point oscillates on its own and spreads energy around it.

Attenuation:In the process of water wave propagation, the energy will gradually decrease, because the water shock is damping.

Refraction:The surface of a water wave is skewed to varying degrees. Due to the refraction, different degrees of offsets of the Underwater scene appear to be deformed. The scene at the bottom of the observation point is not open at the bottom due to the refraction offset.

Reflection:Due to the uneven surface of the water wave, the reflection of each point on the water surface will be affected to varying degrees, thus changing the brightness and color of the water surface.

Water Waves also have diffraction characteristics. However, the core issue is energy transfer or energy diffusion. This is the root cause of the model.

II, Constraints

Achieve thisAlgorithm.

Constraints

Description

Real-Time Rendering

Because the algorithm needs to render water waves in real time, it can only be deduced in an approximate manner, and inefficient operations such as trigonometric functions and division cannot be used to ensure the speed.

 

III, Design Scheme

Use two arrays to simulate the pool. Array size: Pool Height*Pool Width. One array stores the previous state of the pool, and the other is used to store the next State currently being calculated using the previous state. After the calculation is complete, the new State is rendered. Then the new state changes to the "Last state" pool, which is used to calculate the updated pool state, the data is stored in the original first pool. The two pools alternate to the new and old status pools. As a result, energy will spread over time.

To ensure the execution efficiency, the diffusion and refraction of water waves are all replaced by a simplified model to make the algorithm linear and simple. The multiplication and division operations in it should be used as much as possible.2Can be quickly implemented through shift operations.

IV, Mathematical Reasoning and Algorithm Design

Based on the above design, we can establish a model for mathematical reasoning and algorithm design.

 

The above two rectangles represent two arrays, that is, the two pools. Set the pool width to W (W = W + 1) and Pool Height to H (H = H + 1 ).AThe coordinates are (I,J), ThenAThe subscript of the array corresponding to the vertex is :(J * w)+ I.OPoint (0,0) CorrespondingBuf [0], Diagonal lineB(W, h) RepresentativeBuf [w * h-1]That is, the last element of the array.

Derivation of the formula for calculating the amplitude:

 

, Suppose any pointX0The energy at the next time point can be12Points andX0Its own amplitude is calculated. That isX0The amplitude at the next moment is determined by the current amplitude and the surrounding12Point energy spread. And assume that12Point impactX0This model has been greatly simplified. You can get:

X0' = A * (X1 + X2 + X3 +... + X9 + Y1 + y2 + Y3 + Y4) + B * x0

A, BIs the undetermined coefficient,X0'It is the amplitude of the next moment, and the rest is the current amplitude.

If the water damping factor is0, Then the energy will be conservation. That is, the total energy of all vertices remains unchanged at the beginning and end of the time.X0' + x1' +... + Xn '= x0 + X1 +... + Xn

BecauseX0Surrounding12Point impact, that is, the surrounding12PointsX1, x2 ,.... Y4Are affectedX0,X0To appear in total12And the rest are the same (ignore the edge ).

So(12a + B) * x0 + (12a + B) * X1 + ..... + (12a + B) * xn = x0 + X1 +... + Xn

Export:12a + B = 1.

Find a solution:A = 1/4,B =-21/4It can be displaced, and the efficiency is very high.

So get the expected mathematical formula:X0' = (X1 + X2 +... + Y4)/4-2 * x0

But in fact this group of solutions is not accurate, and problems also occur in the test. Because, based on facts,X0The point itself cannot affect the energy of the next moment moreX0It can only be equal to itself. ThereforeB =-1, Absolute value is1. In this wayA = 1/6,

That isX0' = (X1 + X2 +... + Y4)/6-x0

After testing, this group of solutions has the best effect, indicating that they are close to the actual situation.

Consider damping:

The real water has damping. Otherwise, the vibration produced by the above model will continue forever. Therefore, a damping should be considered to reduce the energy at each point in the future than the ideal value.

That isX0' = (X1 + X2 +... + Y4)/6-x0Then there areX0' = x0-x0 *Damping Factor

Consider refraction:

It is unrealistic to perform accurate calculations, because the current design cannot simulate real refraction. Now we can simulate linear approximation. The more skewed the water surface is, the more powerful the refraction is. That is, the closer the underwater scenery looks to be. Therefore, the amplitude difference between the first two points and the second point is used as the refraction offset of the point.

For verticesX0For exampleXAndYThe refraction offsets in both directions are:

Xoff = X7-X5

Yoff = X3-X1

HypothesisX0The coordinate of the point is(I,J), ThenX0The bottom scene to be displayed on the point is(I + x0ff,J + yoff)The scene point at the bottom of the position.

Consider the source:

In order to simulate a realistic source, when the water surface is disturbed, it is not a point but a small range. Therefore, consider a disturbance radius.R, IfX0Point is disturbedX0Is the center of the circle and the radius isRPoints in the region will obtain different degrees of energy for vibration. This energy degrades from the center of the center, that isX0Point to obtain the maximum energy, distanceX0The farther the point gets less energy.

The following figure shows the approximate mathematical derivation to obtain a more realistic disturbance.

 

Assume that the center isA, Distance isDOfBPoint (D <r) The obtained energy is:

EB = EA-ea * (R-d)/r = EA * (1-D/R)

The above simple approximate formula has been tested to simulate the source of the source in a realistic manner.

Consider light reflection:

Considering the actual situation, if a water wave occurs on the water surface, the reflection of each point on the light may change to different degrees than that on the calm water surface due to uneven water surface. It is related to many factors and cannot be accurately calculated. As before, here we use a simplified model for Linear Approximate calculation.

For a point on the water surfaceX0(I,J), Corresponding to the arrayBuf [J * w + I], The energy of this point is assumed to beE0, ThenX0Obtain the color component after the vertex is obtained.R,G,B

Now we can get a new color weight after reflection:

R = R + E0 G = G + E0 B = B + E0

If it exceeds0-255. In this way, the brightness of some vertices on the water surface is enhanced and the brightness of some vertices is weakened according to the waveform distribution, finally, the waveform effect of the water wave is obtained.

(Note: Only brightness changes are taken into account for reflection, rather than color changes caused by ambient light)

I, Verify

When a background image is used, the refraction of water can be reflected separately, and the effect can be seen without considering reflection. This is because the background image is displayed according to the waveform offset to obtain the water wave effect.

When reflection is taken into account (whether or not refraction is taken into account), water waves may occur regardless of whether a background image is used, because the brightness of each point on the water surface is increased or decreased according to the energy. Of course, the water waves are the most vivid under both considerations.

This is equivalent to a pool of clean water. If there is no reflected light, even if there is a disturbance, there is no waveform; and if there is a background under the bottom, as long as there is a refraction, there can also be a water wave. The most important factor to form water waves is reflection.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.