Anim8or Community

Please login or register.

Login with username, password and session length
Advanced search  

News:

Ian Ross has just released a book on Anim8or. It's perect for a beginner and a good reference for experienced users. It contains detailed chapters on every aspect, with many examples. Get your own copy here: "Anim8or Tutorial Book"

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

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 73
    • 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

  • Jr. Member
  • **
  • Posts: 73
    • 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

  • Jr. Member
  • **
  • Posts: 73
    • 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

  • Jr. Member
  • **
  • Posts: 73
    • 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: 205
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #4 on: November 30, 2020, 06:56:58 am »

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

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #5 on: November 30, 2020, 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

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #6 on: November 30, 2020, 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

bayinghound

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #7 on: December 01, 2020, 06:26:14 am »

Ok. I thought I was totally missing something. I ended up using morph targets. The water isn't visible for long, but I did use you plug in. Nice job.
Logged

bayinghound

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #8 on: December 01, 2020, 07:21:45 am »

Is there a way to automate generating morph targets?
Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #9 on: December 01, 2020, 09:29:57 am »

That looks good. I looked up "morph targets" and found the flag tutorial. I take it you saved something like 10 meshes and used them as key frames in the sequence editor.

Possibly I could save grids from the script - but I have a feeling I've read that you cannot save from this kind of script - I need to look at the documentation.

Alternatively what is the format of the files when you save a mesh - perhaps I could just write a program in C or Python to create the mesh files?
Logged

Claude

  • Sr. Member
  • ****
  • Posts: 282
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #10 on: December 01, 2020, 12:56:06 pm »

Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #11 on: December 02, 2020, 04:54:28 am »

Hi Bayinghound,

Looking at the .an8 file for a saved mesh, it looks like it will be easy to create the mesh structures by directly writing to the file. I confess I haven't played with animation at all, so I don't know what the practical limits are. e.g. Ideally there would be a grid for every frame (so 90 meshes for your 3 second clip). Would that be too much for Anim8or/"morph targets" to handle?

If you are happy just to create the meshes then perhaps I could write a program in C that takes a .an8 file, looks for a wave object and replaces it with a number of mesh objects generated from the parameters in the wave object (the waves would look different to the script because I would be using a different random number generator). This feels like a dirty solution, and involves people trusting running my windows code, or knowing how to compile C for themselves.

Perhaps I can write a script to add an export waves option to Anim8or. Export scripts can write to files, and presumably have access to all the information I need.

I expect what you really want is to have it populate the sequence editor with X seconds of morph targets. If you could send me the Anim8or file for your animation so that I can crib from it, that would be a big help.

Thanks,
John
Logged

bayinghound

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #12 on: December 02, 2020, 12:07:07 pm »

Sure. I'll try and do it when I get home from work
Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #13 on: December 29, 2020, 07:57:13 am »

Sorry I didn't get round to creating wave meshes for each frame.  But Steve had sent me a message indicating he hoped to soon start on implementing a proper solution for animated objects.

I see that he has now done this, or at least implemented a “preliminary version”:
https://www.anim8or.com/smf/index.php/topic,6051.0.html

This is much better than a hacked workaround from me.

I didn't pass the message on, because I didn’t want to raise expectations that Steve was going to deliver functionality, when it could have turned out that there was an unexpected complication meaning he had to ditch the feature.

Regards,
John
Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: A new version of my water simulator script with more features!
« Reply #14 on: January 06, 2021, 05:22:22 am »

I added a function to place boat outlines in the water in a script I uploaded on:
https://www.anim8or.com/smf/index.php/topic,6051.0.html
I also posted a couple of YouTube links of the script in action.

/* Function: $addBoat( float $x, float $y, float $direction, float $length, float $width )
 *
 * x,y - Center of boat
 * direction - direction boat is pointing (in radians {radians=PI*degrees/180})
 * length - length of the boat
 * width - width of the boat
 */
int $addBoat( float $x, float $y, float $direction, float $length, float $width )
{
    $dx = sin($direction);
    $dy = cos($direction);
   
    for $angle = 0 to 2*PI step PI / (8 * ($length + $width)) do{
        $len_x = sin($angle) * $length;
        $len_y = cos($angle) * $width;

        $DecayingWaveOrigins.push( ($x + $len_x * $dy - $len_y * $dx,
                                    $y + $len_x * $dx + $len_y * $dy,
                                    1, 12) ); // amplitude, wave length
    }
    return 0;
}


The variable names are a bit odd because I was reusing variables I had already defined.

I should probably add amplitude and wave_length as function parameters!
Logged