Wonderful 2D Water effects...

Game development specific discussions.
Post Reply
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

BTW, you are one of the more 'expert' programmers in these forums, so if you have any ideas or interest in posting your code samples, I am sure it will always be most appreciated by us lower lifeforms! That way we get a chance to learn, and if there is something that is interesting but we don't understand in your code, then we can ask for an explanation and learn even more... please don't just 'tag along'!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

here is a GIF of same thing but being able to flow in octile directions... not too much difference... gotta tackle the wall hugging... ug

I think I will have to use an influence map of the shoreline, that will get me some 'push' towards the center of the river, then combine that somehow with the previous flow map...

Image
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: Wonderful 2D Water effects...

Post by dafhi »

I like UDTs because they allow for rapid prototyping.

Code: Select all

buf.screen_init 800,600
image.create 400,300
image.blit @buf, x,y
As far as my water calculation, I use 5 layers

2 standard sng arrays outlined in the article
internal image
flow field (basically a precalc for potential water + terrain interaction)
main buffer

I updated my code for readability (hopefully)
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

dafhi wrote:I like UDTs because they allow for rapid prototyping.
ah, ok... I definitely will have to learn more
dafhi wrote:I updated my code for readability (hopefully)
uh huh... LOL! but I noticed more comments which are always a help to me
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: Wonderful 2D Water effects...

Post by dafhi »

i've got sub-pixel effect working

hats off to the original developer
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

that is looking really good! and somehow the speed got boosted about 3 times (did you do some optimizing?). my pokey laptop gets 35 - 44 fps here

trying to understand code... I figured out the constrain routine so far - LOL! and I bet it is faster than his min/max method

am trying to modify your routine to be colored (blueish) so can see final effect better...

Do you have any thoughts on how to apply it to a map of tiles, where it will go from one water tile to the next and stop when it hits land within a tile?
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

question: I see you use the VAR command alot for defining variables... why? don't you want to know what type all your variables end up being? how does it help?
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

ok I can't figure out your grayscale plotting routine enough to change it to blue scale (say from rgb(117,163,221) to rgb(117,198,190), just off the cuff, wanted to get some greenish ocean color in there too)

Here is your plotting routine (i think! lol) that figures out the right grayscale color to plot, but I totally am not understanding how it is calculating the grayscale...

Code: Select all

  dim as ulong ptr  plocal = imv_local.pixels
  const             GrayScale=1+256+65536
  for y as integer = 0 to hm
    var x=y*w
    for x=x to x+wm
      plocal[x] = int(psngdest[x]*255.999) * GrayScale
    next
  Next
  imv_local.blit pimvdest, posx, posy ' (call aquarect.render_target() to set imv_dest)
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: Wonderful 2D Water effects...

Post by dafhi »

GrayScale .. let's visualize this in base ten.
Value range 0 to 99: GrayScale = 010101 (1 + 100 + 10000)

What happens when you multiply 010101 by an integer, say 50
you get 505050 .. :D

Next question: how to get blue water. I haven't examined the color algorithm but if you're using a palette, it's a snap:
pixel= pal(int(src*255.999))

Btw I've updated the sample yet again. Simpler and slightly faster.

I do think this overall technique could be easily adapted to terrains.

Var is something I picked up from DJ Peters. The compiler can take advantage of optimizations in a best-fit scenario.
Sure, debugging can be trickier but with experience comes speed
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

dafhi wrote:GrayScale .. let's visualize this in base ten.
Value range 0 to 99: GrayScale = 010101 (1 + 100 + 10000)

What happens when you multiply 010101 by an integer, say 50
you get 505050 .. :D
amazing trick! I think these kind of things are why I like programming so much, this is simply beautiful!
Var is something I picked up from DJ Peters. The compiler can take advantage of optimizations in a best-fit scenario.
Sure, debugging can be trickier but with experience comes speed
that makes sense, didn't think about compiler optimizations... interesting, but, I really like to know what my variable types are for debugging purposes (and most of my programming time is spent debugging...)
Btw I've updated the sample yet again. Simpler and slightly faster.
checking it out now!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

oh yeah, forgot to ask...

Sometimes I think I have seen you do this:

Code: Select all

do
  screenlock
    cls
...
  screenunlock
  sleep 1
  if inkey = chr(27) then exit do
loop
and sometimes this:

Code: Select all

do
  screenlock
    cls
...
  screenunlock
  sleep 1
loop until inkey = chr(27)
I prefer the latter, but is there any real differences between the two? I prefer not to exit from within a loop, as a preference... especially for...next, etc...
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

ALSO!

I usually use this regulate routine for both calculating the FPS and have it easy to limit the fps, but I think it is a bit clunky... using the Sleep value to regulate

Code: Select all

'================================================================ To calc & regulate fps
Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
  Static As Double timervalue,_lastsleeptime,t3,frames
  var t=Timer
  frames+=1
  If (t-t3)>=1 Then t3=t:fps=frames:frames=0
  Var sleeptime=_lastsleeptime+((1/myfps)-T+timervalue)*1000
  If sleeptime<1 Then sleeptime=1
  _lastsleeptime=sleeptime
  timervalue=T
  Return sleeptime
End Function
'====================================================
dim as long fps

'main loop
do
	screenlock
	cls
	locate 2,2 : print "fps(";fps"  )";

' yummy stuff here

    sleep Regulate(<max fps>, fps),1
loop until <exit condition>
do you have a better/different way?

I don't remember who I grabbed this routine from... could even by you! lol
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

I like your updated routine - much simpler and a bit faster

here is my first thought on creating a palette between two color values, do you have a better way?

oops, very bad, doesnt work...real one coming

ok, this one works: looks pretty good with your routine!

Code: Select all

' for palette of greenish blues...
dim shared as ulong pal(255)
' to make a palette between two color values: 007B91 to 68B8FF
' r = 0 to &h68
' g = &h7B to &hB8
' b = &h91 to &hFF
' interval = ((hi - lo)/255)
for p as integer = 0 to 255
    dim as ubyte r =    0 + (&h68 -    0)/255 * p
    dim as ubyte g = &h7B + (&hB8 - &h7B)/255 * p
    dim as ubyte b = &h91 + (&hFF - &h91)/255 * p
    pal(p) = rgb(r,g,b)
next p
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

I am thinking that all this pixel processing s ALOT of work just to achieve an animated effect.. maybe DJ Peters idea of have various layers might be the best way and most likely much faster all around.... will see if I can cobble together something that gives decent effects....
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Wonderful 2D Water effects...

Post by BasicCoder2 »

leopardpm wrote:I am thinking that all this pixel processing s A LOT of work just to achieve an animated effect..
And from what I have seen I don't think it is all that great or easy to use.
It is important for visual appeal not to mix graphic styles.
If you are using a tile based world I think the current animated tiles would match better and look just fine.
Some of the water tile examples I liked:
http://gas13.ru/v3/tutorials/waterflow_imitation.php
https://forums.wesnoth.org/viewtopic.php?f=9&t=28584
http://forums.rpgmakerweb.com/index.php ... ter-tiles/
When a character moves through the water I would just include water waves as part of the character animation.
.
Post Reply