Home > Games Programming, Graphics, XNA > 3D Water Bodies

## 3D Water Bodies

This is a simple XNA project that simulates an ocean…
The idea first came to me when I was practicing on creating terrain using heightmaps! I was playing with the vertices of the terrain, giving them some motion on the Y Axis then I said to my self: “Wow! This looks like the sea wavy water! :D”

The principle is simple! examine this very simple example:

I have 9 vertices forming 4 quads
I also have an array of 9 integers to determine the direction of each vertex : up/down
when first initializing the WaterBody, a random value between -n,n is assigned to the Y component of each vertex. so each vertex should start with a random Y component… The direction of each vertex is also generated randomly!

Now all I have to do – every frame – is to move each vertex in its direction, if Position.Y<-n or Position.Y>n then fix Position.Y and change the direction of the vertex ( if up make it down, if down make it up! pretty simple huh? π )

If you apply the above to a larger number of vertices ( I used 70×70 in my project ) you’ll have a nice wavy water!
Adding some lighting and texture will make things better ;).
Adding sound effects will make things even better! π π

Here’s the final result!

You can control several things in the application like enabling/disabling lighting and texture, changing camera position…
You can change the max wave height,wind speed and the application will automatically randomize theΒ  waves!

WaterBodies Executable ( 144 KB ) – XNA Redistributable 3.1 Required ONLY if you don’t have XNA Game Studio 3.1 installed
WaterBodies Source Code ( 183 KB ) – XNA Game Studio 3.1 Required

Revisions:

rev. 1 : Position,Rotation,Scale properties are now available! the WaterBody class is now more flexible than before!

rev. 2 : Updated camera class, now it’s a free look camera. Code now is more tidy..

1. 07/09/2010 at 8:12 pm

2. 08/09/2010 at 12:07 am

That’s odd! π¦ Nothing is working on mediafire.com π¦
it says it’s under maintenance

Scheduled time: 12:30pm to 11:59pm CST
Maintenance Progress:
30%

I checked the files, they are still on the server,
It should be up and running shortly π

BTW : you can check here
http://www.gamedev.net/community/forums/topic.asp?topic_id=581248
The members at gamedev suggested some improvements, but I’m busy right now to update the article!

3. 03/07/2011 at 7:43 pm

I defenetly need code for xna to make effect like in game Left4dead boomer explotion. It even would be better without green color.

• 04/07/2011 at 1:23 pm

I haven’t played the game, any screenshots?

4. 27/08/2011 at 2:29 am

I am wondering do you have source code to run on XNA4.0? Thanks!

• 27/08/2011 at 5:55 pm

I updated the code to run under XNA4.0
Here it is:
http://www.mediafire.com/?gtnct01lfy7lvgq

The code runs for XNA Reach Profile, so index buffers use short for indexing.

Just a note, since normals calculation is done on the CPU, creating large water bodies will not run smoothly.

• 28/08/2011 at 6:44 am

Thank you so much! It runs pretty well and is very helpful.

• 28/08/2011 at 4:40 pm

You’re welcome!