Anim8or Community

Please login or register.

Login with username, password and session length
Advanced search  

News:

An update to Anim8or, v1.00b, is available with a few bug fixes. Get your copy HERE. See the "ReadMe" file for details.

Author Topic: A new version of my water simulator script with more features!  (Read 183 times)

2020 Hindsight

  • Newbie
  • *
  • Posts: 17
    • View Profile
A new version of my water simulator script with more features!
« on: November 17, 2020, 02:43:16 pm »

OK,  I didn't anticipate a new update this quick, but looking at Ian Ross's yacht image made me think that it would look better if objects in the water looked like they were interacting with the water. So here is a new script to do that, called "Combo waves". It has a new icon to avoid confusion with the "waves" script.

I considered tagging this onto the end of the "waves" topic. But I think the script is different enough to warrant starting fresh. The interface for generating random waves is identical, and you should follow the instructions posted on the "waves" post https://www.anim8or.com/smf/index.php/topic,6025.0.html.

If you don't edit the script itself then this script is indistinguishable from my "waves" script. However I have modified the code to allow wave sources to be easily added.

There are two types of wave that can be added:
1) Decaying waves: These are best for boat hulls, drips, etc.
2) Immortal waves: These never reduce in size, and are good for wind produced waves - best if they are placed outside the visible area of the mesh.

As with the original "waves" script the user can play with the run time parameters to create random weather. The values plugged into the "Parametric Plug-in Editor" only act on the randomly generated waves, not the hard coded waves (actually you can write code to read $XTiles, $YTiles, and potentially other passed variables, if you want to do clever things with your hard coded waves).

To add hard coded waves look for the following comment block in the script:
"
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Start of additional hard coded waves block
///////////////////////////////////////////////////////////////////////////////
"

Under this there are two sub sections:
"
///////////////////////////////////////////////////////////////////////////////
//  Add additional hard coded non-decaying "immortal" wave origins here if required:
"

You can add as many additional wave origins as you want.

Each "immortal" wave origin is added with a line like:
$WaveOrigins.push( (0,0, 0.25, 5) );

In this example push() takes the parameter (0, 0, 0.25, 5). Which means the origin is at x=0,y=0, the wave height = 0.25, and the wave length = 5.


The second sub section is where decaying waves origins are added with a line like:
$DecayingWaveOrigins.push( (10, 20, 40, 7) );

In this example push takes the parameter  (10, 20, 40, 7). Which means the origin is at X=10, and Y=20, with a height of 40, and a wavelength of 7. (Decaying waves need larger height values than immortal waves do.)

You could place it at the center of the grid with:
$DecayingWaveOrigins.push( ($XTiles/2, $YTiles/2, 40, 7) );

In this example the width and height the user sets in the "Parametric Plug-in Editor" are read from $XTiles, and $YTiles, and used to find the centre.

Another variable that may be useful is "Max wave height" stored in $MaxWaveHeight


I mark the end of the area where hard coded waves should be added with the comment block:
"
///////////////////////////////////////////////////////////////////////////////
// End of additional hard coded waves block
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
"

Have fun!
John
Logged

2020 Hindsight

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #1 on: November 17, 2020, 02:47:16 pm »

As an example of creating waves around a boat I came up with this rough and ready demonstration of a boat hull at 45 degrees. (note +0.5 on x,y to avoid big splashes):

$DecayingWaveOrigins.push( ($XTiles/2 -10.5, $YTiles/2 +10.5, 3, 3) );
$DecayingWaveOrigins.push( ($XTiles/2 -8.5, $YTiles/2 +8.5, 4, 4) );
$DecayingWaveOrigins.push( ($XTiles/2 -6.5, $YTiles/2 +6.5, 5, 5) );
$DecayingWaveOrigins.push( ($XTiles/2 -4.5, $YTiles/2 +4.5, 6, 6) );
$DecayingWaveOrigins.push( ($XTiles/2 -2.5, $YTiles/2 +2.5, 7, 7) );
$DecayingWaveOrigins.push( ($XTiles/2 -0.5, $YTiles/2 +0.5, 8, 8) );
$DecayingWaveOrigins.push( ($XTiles/2 +2.5, $YTiles/2 -2.5, 7, 7) );
$DecayingWaveOrigins.push( ($XTiles/2 +4.5, $YTiles/2 -4.5, 6, 6) );
$DecayingWaveOrigins.push( ($XTiles/2 +6.5, $YTiles/2 -6.5, 5, 5) );
$DecayingWaveOrigins.push( ($XTiles/2 +8.5, $YTiles/2 -8.5, 4, 4) );
$DecayingWaveOrigins.push( ($XTiles/2 +10.5, $YTiles/2 -10.5, 3, 3) );
Logged

2020 Hindsight

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #2 on: November 17, 2020, 03:03:45 pm »

In the code there are a couple of commented out example wave origins:

$WaveOrigins.push( (0,0, 0.25, 5) );    // Example of how to add a hard coded immortal wave, should you have a reason to do so.

This is an immortal wave starting at the bottom left with a wavelength of 5.

and

$DecayingWaveOrigins.push( ($XTiles/2, $YTiles/2, 40, 7) );    // x, y = horizontal offset, z = amplitude, w = wavelength;

This is a decaying wave starting in the centre, with a wavelength of 7.

I captured some images:

First one with the randomly generated waves + immortal waves + Decaying waves. I selected a Time to demonstrate what I mean by a "spiky spikes" in the code comments.

The second image is the same setup, but with the "Max wave height" turned down to 0, so only the hard coded waves are visible.
« Last Edit: November 17, 2020, 07:26:50 pm by 2020 Hindsight »
Logged

2020 Hindsight

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #3 on: November 17, 2020, 04:34:30 pm »

If you are having problems with decaying waves creating spikes that are too big (maybe poking through another object). You can change the script to reduce the spike size. (Originally this code was just to protect against dividing by zero.)

Lines 206,207 are an if statement:
if( $distance <= 0.1 ) // If you want big spikes change this to: if( $distance == 0 )  
    $distance = 0.1; // Avoid division by zero for points exactly on tile boundary



If you want smaller spikes you could modify these lines to: 
if( $distance <= 0.5 )
    $distance = 0.5;



If on the other hand you want to make a feature of the spikes you can increase them with:
if( $distance <= 0.001 )
    $distance = 0.001;


Remember each tile is 1.0 long. The maximum spike is achieved when the wave origin is positioned on the corner of a tile (i.e. X and Y are both whole numbers). The "if" statement above bluntens spikes.
Logged

bayinghound

  • Full Member
  • ***
  • Posts: 111
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #4 on: Yesterday at 06:56:58 am »

Maybe a stupid question. Does this animate the waves as well?
Logged

2020 Hindsight

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #5 on: Yesterday at 07:43:14 am »

Hi bayinghound,

I would expect the animated waves to look realistic - although I don't know anim8or well enough to do animations.

On the other hand decaying waves (which you can only add by changing the code) may look artificial, as the energy remains localised at the origin of the waves - so probably will look OK for boat hulls, reeds, and rocks sticking out of the water, but will probably look artificial for animated water drips. I suppose the code could be changed to take time from impact as well as distance into account when calculating wave height.

You can get a low fidelity feel for what the animation will look like, by changing the "Time" values in the "Parametric Plug-in  Editor".

You can do this by advancing the time by 5, and shift-Tab (to move back to the "Wave direction" field (so causing the waves to be redrawn)), then Tab (to move back to the "Time" field), to modify the field again. The waves will evolve rather than just shift position - but this will be more obvious the closer the "Min wave distance" is, and the larger the values of "X Tile", and "Y Tile". So position the cursor on "time", and with dexterity type quickly:

5<shift-Tab><Tab> 10<shift-Tab><Tab> 15<shift-Tab><Tab> 20<shift-Tab><Tab>...

It is easier to count up in 10s! But perhaps that works better with larger waves.
Logged

2020 Hindsight

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #6 on: Yesterday at 02:04:56 pm »

Hi bayinghound,

If my answer didn't seem to match your question, it is because I missed the word "as" when reading it.

I made a post https://www.anim8or.com/smf/index.php/topic,6026.0.html asking advice about animation, but I didn't get any responses. I feel it should be easy to automate, the script just needs to be called for each frame with a time or frame number - but I don't believe this is currently supported.

If you have patience, then I suspect you could manually change the time field, then save the waves as a grid with a name for that frame. And repeat for each frame, and load different grids for each frame. Automation would be so much nicer though!

John
Logged