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


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

    hello, good contributions, the, but I have a problem with the download links

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

    bzkarcade :

    hello, good contributions, the, but I have a problem with the download links

    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:

    I checked the files, they are still on the server,
    It should be up and running shortly πŸ™‚

    BTW : you can check here
    The members at gamedev suggested some improvements, but I’m busy right now to update the article!

  3. satyr
    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.

    • Fuchs
      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!

    • Fuchs
      27/08/2011 at 5:55 pm

      I updated the code to run under XNA4.0
      Here it is:

      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.

        • Fuchs
          28/08/2011 at 4:40 pm

          You’re welcome!

  1. No trackbacks yet.

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: