Water
A tutorial for creating simple wave effects.
Water is a really nice effect, one of the better tricks around. It's also pretty simple to code, should take no more than an afternoons work to get a good water routine.
[size="5"]Basics
First thing you'll need is 2 buffers, for the water. This needs to be an array of ints, same size as destination buffer. Arrange these in a 2-element array, to ease the flipping. Clear these to zero, and you're ready to start.
[size="5"]Calculate The New Water
Calculating the new water is pretty simple. You'll need a loop that execute from 1 to height-1, then 1 to width - 1. At each element for the water, you'll need to sum the North, South, East, West points from the current water, and divide by 2 Not 4, 2. Then subtract the new water for [y][x] from this. This is your basic smooth function. Now you need to add the 'harmonic motion' to it. Take the new-water[y][x], shift it right x places (x could be 4) and subtract it from the new-water[y][x]. Pseudo-code for this might look like:
for y := 1 to height - 1
for x := 1 to width - 1
new-water[y][x] = ((old-water[y-1][x] +
old-water[y+1][x] +
old-water[y][x-1] +
old-water[y][x+1]) / 2) -
new-water[y][x])
new-water[y][x] -= new-water[y][x] shr x
end
end[size="5"]Paint The Water
Water is also pretty straightforward to paint. In fact some of the techniques here, you will see later on in the bump-mapping. Back to the task in hand. We will again need a loop for every pixel on the screen. What we need to do is calculate a kind-of normal at each pixel. This is simply:
offsetx = water[y][x] - water[y+1][x]
offsety = water[y][x] - water[y][x+1]offsetx /= 3;
offsety /= 8;
indexu = offsetx + x;
indexv = offsety + y;
MulTable[backdrop[indexv*256+indexu]*256+colour];For y := 1 to height - 1
For x := 1 to width - 1
offsetx = water[y][x] - water[y+1][x]
offsety = water[y][x] - water[y][x+1]
colour = 128 - offsetx
trim colour to 0..255
divide offsetx and offsety by 8
add offsetx to x giving indexu
add offsety to y giving indexv
Plot (backdrop*colour) >> 8, lookup in table
End
End[size="5"]The Water Loop
Note it makes a difference what order you do calculations in. It's pretty simple though. You need to:
- Draw to the water
- Paint it
- Calculate new water
- Page flip the water
Tom Hammersley, [email="tomh@globalnet.co.uk"]tomh@globalnet.co.uk[/email]
Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Tom Hammersley
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion