# 3d Water Simulation formula questions

| | August 6, 2015

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

## One Response to “3d Water Simulation formula questions”

1. 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].

Regarding your second problem, I don’t see anything wrong. In fact the screenshot looks exactly like it should and seems to match the plotted curve. Try with k=10 and you should see the difference clearly.

• ### How To Submit a iPhone App to the Appstore

by on July 5, 2012 - 0 Comments

The process to develop an iPhone app is not as hard as one might think. The entry into the app store has been opened up to many new types of develope...

• ### Develop iphone Apps on Windows

by on March 7, 2011 - 13 Comments

Top 10 Ways to Develop an iphone/ipad app without a Mac #1. Code in Java For Java developers, there is a workaround: XMLVM. XMLVM cross-compiles byte...

• ### Iphone Android App Ideas: Getting Started

by on March 19, 2011 - 3 Comments

Smartphone Application Ideas: Getting Started Smartphone application developers are constantly on the move. And since overzealous developers stick ...