Realistic water simulation ! [NOT a game]

spoogob

  • Posts: 1106
It was all working fine, but in the version prior to the gradient, I had altered things so that I could place the water at a specific point on the screen - by setting a 'point' actor where I told the draw blocks to 'switch to actor space' , which then made the water draw from that point. But doing this since the gradient code going in, broke it.

So what I did was started from scratch with the latest version of the behaviour and under created and the repeat block, ive changed :

add distancebetweenpoints x current loop count in positionlist

to

add [number attribute] + distancebetweenpoints x current loop count in positionlist

and this works nicely with the number (x position) you put in, pushes the water to the right to the position you want. There maybe a better way of doing this, but it works, with the gradient intact. Taking into account a couple other little changes, I can now position the water where I want, with any x size, and it works great. :)

edit:

forgot to mention, this is only for static/single screen positioning. If player has a camera, and scenes are larger, then water wont move with it (where it would with older non-gradient version i had).. haven't figured that one out yet at moment, as I can't switch drawing to an actors space, as it doesn't work.

edit2:

Ok.. almost got it working for camera movement and keeping the water at required size and placement. shall update again later!  :)


« Last Edit: August 25, 2013, 07:24:52 am by spoogob »

spoogob

  • Posts: 1106
well, i managed to get the water , gradient included, to place where ever i wanted, and also take into account its position when the camera/scene moves. The x works fine, and the water holds its place. The y kinda works, but the gradient seems more stretched out and i havent worked out how to alter/fix that. One other side effect is that when i move up in a test level and the camera moves, the water moves up (Y) also, which is right,  but it kinda wobbles the water until you stop moving vertically, then it settles down once you dont move vertically.

Anyhow, its kinda usable as is for me in way of testing and placing into my game, but would be good for someone else to have a go at it too and make it better than my testing has done. :D

Ive attached a pic of the behaviour with my changes, but if someone has a better idea or way of doing this , that would be great.

(apart from the changes to affect position/camera etc, i included a do every .5 seconds, which creates a wave at a point in the water automatically, instead of using the mouse button).

SadiQ

  • Posts: 1781
Wouldn't doing this work?:

WaterSurface.graphics.beginGradientFill(type, colors,alphas,ratios,matrix);
for(var i:int = 0; i < _PositionList.length ; i ++)
{
    if(i == 0)
      {
        WaterSurface.graphics.moveTo(_PositionList[ i ] -getScreenX(), _Heightlist[ i ]);
      }
    else
      {
        WaterSurface.graphics.lineTo(_PositionList[ i ] -getScreenX(), _Heightlist[ i ]);
      }
    if (i == _PositionList.length-1)
      {
         WaterSurface.graphics.lineTo(_PositionList[ i ] -getScreenX(), getScreenHeight());
         WaterSurface.graphics.lineTo(_PositionList [ 0 ] -getScreenX(), getScreenHeight());
         
      } 
}
WaterSurface.graphics.endFill();
FlxG.buffer.draw(WaterSurface);

That way you don't need to change anything else anywhere (except the created event).
Of course this is only for the X movement...you'll have to do the Y part and adapt this to 3.0
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

spoogob

  • Posts: 1106
The x at the moment works fine, but I can implement that and give it a try as it might be a  bit cleaner. Y at the moment was tricky, although i got it working, but mainly because its on the Y that all the waving and such happens. As i say, when the player moves up vertically and the camera follows, i can make the water stay where it is, but when movement happens, it waves the water around roughly until you stand still. Will implement that for now anyhow. :)

Max Finch

  • *
  • Posts: 2187
Is there a way to draw the water on different layers instead of it always being in the front?

SadiQ

  • Posts: 1781
Don't know if it would work or if it's the best solution, but try creating a dummy actor and place that actor on the layer you wish to draw on. Then use the Switch to actor space block to draw the water. (hope you get the general idea of what I'm trying to say).
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

Max Finch

  • *
  • Posts: 2187
Yeah I get ya. Lemme see if that works.

Max Finch

  • *
  • Posts: 2187
All it did was change the x y positions not the layer position.

SadiQ

  • Posts: 1781
I honestly have no idea. I just created a small test game to test and it looks like I was wrong :(
Maybe post a question in the forums to see if drawing on a specific layer is actually possible? (more people will see the question that way)
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

Max Finch

  • *
  • Posts: 2187
I'll put it in suggestions for drawing as I see no way to do it in the drawing section :( however I noticed in sounds and images that there's layers so it's probably possible but just not implemented by Jon yet.

Max Finch

  • *
  • Posts: 2187
Okay I ran into a bit of a problem with this. Imagine a boat floating in this water behavior, How would I get the peak Y waters height according to the X position of the boat and then apply that to the boats Y position?

SadiQ

  • Posts: 1781
From what I remember the HeightList holds the Y values you want and PositionList holds the X values you need. You could print the values in PositionList to figure out the X coordinates (the index) and check the value you have in HeightList at the Index you got from PositionList to get the Y value.
You could even apply the Y values for the front and rear of the boat so that it would appear to be moving with the waves. 
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

Max Finch

  • *
  • Posts: 2187
I tried that and it sorta works but not very well. I figured position list held X values and heightlist held Y values. How would I go by fetching the Y position according to the list # for example, my boat is at 48 X which after some code translates to #3 on the Y list (48/16 = 3). How would I get the Y position from #3?

Max Finch

  • *
  • Posts: 2187
Here is the code that I currently had. It works to an extent, basically it's only grabbing 1 X position for the heightlist...

SadiQ

  • Posts: 1781
You cold try something like:
For each item in PositionList
if x-center of ship = item
set y of ship to get item from Heightlist

You'll have to modify it to see if your ship is in between the positions you have in PositionList (maybe checkif x-center <item ).
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.