Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have created a Grid: enter image description here

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

enter image description here

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”:

enter image description here

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:

enter image description here

WHEN K = 1.5

enter image description here

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

enter image description 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 :)

Source: http://http.developer.nvidia.com/GPUGems/gpugems_ch01.html

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:

enter image description here

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:

enter image description here

Is there something wrong with my implementation?

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

Sidney de Vries

share|improve this question
1  
There is no + 1 in your function after Math.sin. –  msell Feb 4 at 6:12
    
God dammit. Im so stupid. Thanks a bunch mate :) –  TastyLemons Feb 4 at 6:18
    
Make it the answer or something so i can Tick it as the answer :) Least i can do –  TastyLemons Feb 4 at 6:18
    
Still an excellent question. –  Timothy Groote Feb 4 at 10:44
add comment

1 Answer

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.

share|improve this answer
    
I've update the question. If no one answers my new issue, ill vote this as the answer and remove part two. –  TastyLemons Feb 4 at 6:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.