Jump to content
  • Advertisement
Sign in to follow this  
Rasmadrak

Water (Again...)

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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 1000

int 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,y
if (x < 1 ) x = 1;
else
if (x > MAX_POOL_SIZEX-1) x = MAX_POOL_SIZEX-1;

if (y < 1 ) y = 1;
else
if (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!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!