4
\$\begingroup\$

Im attempting to implement a simple height field water surface simulation outlined in Matthias Muller's GDC 2008 talk. "http://www.matthiasmueller.info/talks/gdc2008.pdf"

I can't seem to see why it my implementation doesnt work. Could anyone help? Any advice or help would be much appreciated. The main processing loop is below The obstruction array is initialized as 1. And the source just adds 0.1f to a point.

void wave()
    {
        for( int i = 0; i<size ; i++)
        {
            height[i] += source[i];
            height[i] *= obstruction[i];
        }


        for (int i = 1 ; i<iwidth-1 ; i++)
        {
            for(int j = 1 ; j < iheight-1 ; j++)
            {

                velocityField[indexFind(i,j)] += (height[indexFind(i-1,j)] +
                                                  height[indexFind(i+1,j)] + 
                                                  height[indexFind(i,j-1)] + 
                                                  height[indexFind(i,j+1)])/4 - height[indexFind(i,j)];

                velocityField[indexFind(i,j)] *= dampening;


                height[indexFind(i,j)] += velocityField[indexFind(i,j)];

            }

        }
    }

    int indexFind(int x, int y)
    {
        return (x + iwidth*y);
    }

This is what happens (height field rendered as a texture on a plane)

heightfield texture

This is the page from the link that outlines the algorithm

GDC2008 Hello World

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The error is in the slide, the correct implementation is:

for (size_t i = 0; i < N; i++)
{
    for (size_t j = 0; j < M; j++)
    {
        size_t n = i * N + j;
        size_t a = i ==   0 ? i * N + j : (i-1) * N + j;
        size_t b = i == N-1 ? i * N + j : (i+1) * N + j;
        size_t c = j ==   0 ? i * N + j : i * N + (j - 1);
        size_t d = j == M-1 ? i * N + j : i * N + (j + 1);

        v[n] = (u[a] + u[b] + u[c] + u[d]) / 4.0f - u[n];
        v[n] *= 0.99;
        u[n] += v[i * N + j];
    }
} 

Note the =instead of the first +=. To make something interesting you will need to add additional perturbation over time, since this only smooths out the waves.

\$\endgroup\$
3
  • \$\begingroup\$ Hi, thanks for the reply. Im attempting to make it interactive. I did try what you said, but the height values do the same thing. \$\endgroup\$
    – KMoore
    Jun 26, 2014 at 14:19
  • \$\begingroup\$ That is odd. I may need to implement the same to see what happens. \$\endgroup\$
    – rioki
    Jun 27, 2014 at 12:54
  • \$\begingroup\$ I found it, the slide is wrong... Complete new answer. \$\endgroup\$
    – rioki
    Jun 27, 2014 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .