Water (Again...)

Started by
4 comments, last by Rasmadrak 18 years, 10 months ago
Hi there! I just tried out a water tutorial... but alas; mine doesn't work: even thou it's almost identical as the tutorial... :S


int Damping = 4;

 for (int y=1; y < WaterSizeY-1;y++)
     for (int x=1; x < WaterSizeX-1;x++)
        {
        Water[x][y] = ((OldWater[x-1][y]+
                        OldWater[x+1][y]+
                        OldWater[x][y+1]+
                        OldWater[x][y-1]) /2 ) -
                        Water[x][y];

        Water[x][y] -= Water[x][y] >> Damping;

        }


 for (int y=1; y < WaterSizeY-1;y++)
   for (int x=1; x < WaterSizeX-1;x++)
        OldWater[x][y] = Water[x][y];


Does anyone know why this doesn't work? the waves NEVER stop.. :S I'm kinda stuck, since I've followed the tutorials (articles->specialfx->water) precisly... :( Thanks in advance!
"Game Maker For Life, probably never professional thou." =)
Advertisement
Most probably this:
> Water[x][y] >> Damping
always is 0 in your case.

This is because you're working with ints.
I suggest you switch to floats.
Maciej Sawitus
my blog | my games
I've tried this too...

But the original used int, so I switched back... :/

I got the dampening going now, by manually decreasing every Water[x][y], but now I noticed another strange bug....

the water ring is not spreading uniformely, if I click on the center of the pool, the lower right corner gets ripples first.... :(
weird indeed... dunno why really.


Sorry for being stupid. :)
"Game Maker For Life, probably never professional thou." =)
Update...

void UpdateWater(){  for (int y=1; y < WaterSizeY-1;y++)    for (int x=1; x < WaterSizeX-1;x++)        {        Water[x][y] = ((Water[x+1][y]+                        Water[x-1][y]+                        Water[x][y+1]+                        Water[x][y-1]) /2 ) -                        OldWater[x][y];        Water[x][y] -= Water[x][y] >> Damping;        //"pageflip"        OldWater[x][y] = Water[x][y];        }}


works like a charm EXCEPT that instead of water rings, they're more like "blazing comets" ... :S

Does anyone know why? I'm stuck... this area blending crap has really left me clueless...

Thanks. :)
"Game Maker For Life, probably never professional thou." =)
I wonder about that 'flip'; according to the article, you should make a "page-flip" which probably means you must store both the old and the new water somewhere. Have two big buffers allocated for them and simply swap the pointers. Be careful about the edges too and you should consider switching to floats, things will get a lot smoother.
Other than that, hacks like this is about experimenting :)
Spot on!

I separated the flipping from the main loop, and now it works as intended! woooho!! :)

source...

#define MAX_POOL_SIZEX 1000#define MAX_POOL_SIZEY 1000int WaterSizeX=300;int WaterSizeY=300;int Damping=5;int TempWater=0;int NewWater=0,OldWater=1;int Water[2][MAX_POOL_SIZEX][MAX_POOL_SIZEY];void UpdateWater(){  for (int y=1; y < WaterSizeY-1;y++)    for (int x=1; x < WaterSizeX-1;x++)        {        TempWater = ((Water[OldWater][x+1][y]+                        Water[OldWater][x-1][y]+                        Water[OldWater][x][y+1]+                        Water[OldWater][x][y-1]) /2 ) -                        Water[NewWater][x][y];        TempWater -= TempWater >> Damping;        Water[NewWater][x][y] = TempWater;        }        //pageflip        int temp=NewWater;        NewWater=OldWater;        OldWater=temp;}void DrawWater(vertex3f Position){ for (int Y=1; Y < WaterSizeY-1;Y++)     for (int X=1; X < WaterSizeX-1;X++)        {        //This is for testing only, to be replaced with GL commands...        //Add "refraction" value to color, mix with background        }}void CreateRipple(int x,int y,int amplitude){//amplitude is the speed and weight of the object...//below is not really nescesary if you know that you never supply a faulty x,yif (x < 1 ) x = 1;elseif (x > MAX_POOL_SIZEX-1) x = MAX_POOL_SIZEX-1;if (y < 1 ) y = 1;elseif (y > MAX_POOL_SIZEY-1) y = MAX_POOL_SIZEY-1;//"Add" amplitude!!Water[0][x][y] = -amplitude;}void CreatePool(int SizeX,int SizeY,int damping){SizeX < MAX_POOL_SIZEX ? WaterSizeX = SizeX : WaterSizeX = MAX_POOL_SIZEX;SizeY < MAX_POOL_SIZEY ? WaterSizeY = SizeY : WaterSizeY = MAX_POOL_SIZEY;Damping = damping;ZeroMemory(Water, sizeof(int)*(2*MAX_POOL_SIZEY*MAX_POOL_SIZEX));}




First call CreatePool(), then create ripple with: CreateRipple()...

Cheers! :)



Use for whatever you like!!
"Game Maker For Life, probably never professional thou." =)

This topic is closed to new replies.

Advertisement