# 3d Water Simulation formula questions

I have created a Grid:

At each point along the grid, is a vertex (Intersection Points):

Each of these points has an x,y,z position. Vector3f(x,y,z). In an attempt to animate the grid into a group of ocean waves, I used this formula to adjust the y position of each “Vertex”:

This is my attempt at implementing the formula into java:

private float sineY(int x, int y, float time) {
Vector2f position = new Vector2f(x, y);
float yPos = (float) ( (2 * amplitude) *Math.pow((Math.sin((wavenumber.dot(position))  + temporal_angular_freq * time +  initiatory_phase) + 1)/2, k));
return yPos;
}


This function is running from inside a for loop that is updated every ms:

Time ++;
for (int y = 0; y < sizeY; y++) {
for (int x = 0; x < sizeX; x++) {
float xPos = x;
float yPos = 0;
float zPos = y;
xPos += 0;
yPos += sineY(x,y,time);
zPos += 0;
waterVertexPos.set(xPos, yPos, zPos);
vertBufArray[index++] = waterVertexPos.getX();
vertBufArray[index++] = waterVertexPos.getY();
vertBufArray[index++] = waterVertexPos.getZ();
}
}


As you can see, for each vertex, along the grid, the yPosition of the vertex is set to whatever value the sineY method returns, which is whatever the above formula returns. Running this code reults in this:

WHEN K = 1.0:

WHEN K = 1.5

As you can see, they both do not follow in accordance with this image here:

All the formulas and graphs have been retrieved from this pdf file. The aim of this program is to simulate water using the presented equations. The pdf is really interesting have a read if you like :)

Thank you VERY MUCH for all of your time :)

EDIT:

After implementing the fix suggested by msell. It fixed this previous issue, however a new issue arose when the waves peaks still do not look as they did in the above graph:

WHEN K = 2.0:

The pdf specifies:

A common complaint about waves generated by summing sine waves directly is that they have too much "roll," that real waves have sharper peaks and wider troughs. As it turns out, there is a simple variant of the sine function that quite controllably gives this effect. We offset our sine function to be nonnegative and raise it to an exponent k. The function and its partial derivative with respect to x are.

And then refers to the formula mentioned above, and another formula:

Is there something wrong with my implementation?

And again. I cannot thank you enough for your time :)

Sidney de Vries

• There is no + 1 in your function after Math.sin. – msell Feb 4 '14 at 6:12
• Make it the answer or something so i can Tick it as the answer :) Least i can do – TastyLemons Feb 4 '14 at 6:18
• Still an excellent question. – Timothy Groote Feb 4 '14 at 10:44

You have incorrectly implemented the formula as a function. The function is missing + 1 after the call to Math.sin(), which moves the wave to the range [0, 2].