Wonderful 2D Water effects...

Game development specific discussions.
Post Reply
BasicCoder2
Posts: 3915
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Wonderful 2D Water effects...

Post by BasicCoder2 »

@dodicat,
The water waves look good. I can actually see the "lines" across the stream where the flow changes direction. In real rivers the water on the outside of a curve travels faster than the inside of the curve resulting in the outside of the curve being carved out while the inside of the curve can develop a sand bar as the slower moving water drops small particles. This over time develops a complete loop back on itself.
.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

dodicat wrote:Here is a spline along each riverbank.
Srvaldez:
I have altered a size for memcpy, maybe that was the cause for crashing.
Seems ok here, I ran the thing for about 20 minutes, no hiccups.
still getting crashes

and noticed some artifacts:
Image
magnify this and see the clear delineation in directions... doesn't need to be perfect, but having less of a line would be perfect

also notice the black triangle in the lower right f the river... strange artifact

Trying to understand your code - so far, this is pretty amazing!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

dodicat, would it help if you had a perfect path through the exact center of the river so that you could calculate slope, tangent. etc perfect(well, almost) at each point? I have an app for that! The path would simply be an array of consecutive x,y points, but taking the point before and the point next would give you the tangent...

ok, understanding your routine more now... the river banks and river path are made up of 24 points each. this is where the 'line' of direction changes artifact is happening... so actually having a point by point path (with pre-calculated arcTANs) would vastly improve speed and reduce artifacts to zero, i think...
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

wait a sec, are you cheating here?

Code: Select all

    For n As Long=1 To 24-1
        Var a=Atan2((p1(n+1).y-p1(n).y),(p1(n+1).x-p1(n).x))
        If n<16 Then rotate(image,-a,p1(n).x-50,p1(n).y-50)
        If n>=16 And n<=19 Then rotate(image,-a,p2(n).x-20,p2(n).y-50)
        If n>19  Then rotate(image,-a,p1(n).x-65,p1(n).y-50)
    Next
are you hard-coding in the river direction changes?
Last edited by leopardpm on Aug 31, 2016 4:40, edited 1 time in total.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

BasicCoder2 wrote:Ok. I have already started working on it myself with little agents in mortal combat so we can compare notes.
No Fair! You already have agents in mortal combat? I haven't even started yet!!!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

dodicat wrote:64 bit, as usual, is crap at direct pixel drawing.
32 bit -gen gcc is a bit slowed down (again as per usual for graphics)
why is 64bit crap at direct pixel manipulations? is it because a 32bit pixel does not fill the 64bits entirely? and then why is 32 bit -gen gcc 'slowed down'?

What are the 'best/fastest' compiler modes for doing graphics or direct pixel graphics? This is upsetting to me
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wonderful 2D Water effects...

Post by dodicat »

A track down the river centre would be a good idea.
I kept the image small for speed, so I had to fiddle around at the bends so not to show gaps.
(By the way, this wasn't meant to be a serious way to create a water effect)
Every swirl/eddy is done pixel by pixel both for creation/rotation, for a game I think this would be overkill.
You need some very simple way to cheat your user into thinking ... WATER is flowing through this.

I like the 32 bit -gen gas compiler.
64 bit is very jumpy and slow with graphics sometimes, on Win 10 here anyway.
Also, 64 bit handles integers faster although they are twice the size in memory as long.
Of course 64 bit is still newish with freebasic, and so few developers.
BasicCoder2
Posts: 3915
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Wonderful 2D Water effects...

Post by BasicCoder2 »

leopardpm wrote:
BasicCoder2 wrote:Ok. I have already started working on it myself with little agents in mortal combat so we can compare notes.
No Fair! You already have agents in mortal combat? I haven't even started yet!!!
Don't worry it isn't fancy. There are no obstacles or path finding involved yet.
There are two sets of agents, the red and the green. They wander around until a red meets a green. A random number determines who wins. The fellow is removed from the game.
With regards to path following I thought maybe give them sensors like those simple robot toys that follow a line. If they lose a line they could execute find a line again. Which way to travel if a line is found? Maybe the direction pointing the closest to the desired destination. These lines can be on a separate bitmap and thus not visible to the viewer. I made a start at writing a line follower and posted it in the projects section.
.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

dodicat wrote:A track down the river centre would be a good idea.
I kept the image small for speed, so I had to fiddle around at the bends so not to show gaps.
(By the way, this wasn't meant to be a serious way to create a water effect)
Every swirl/eddy is done pixel by pixel both for creation/rotation, for a game I think this would be overkill.
You need some very simple way to cheat your user into thinking ... WATER is flowing through this.
I was actually thinking that using DJ Peters tile idea which follows the center path would probably be just fine. I really do like your swirls n eddys though - slowed down a bit and with some more color tweaking and yours looks pretty darned realistic! I still haven't cracked exactly how yours it making the image though...
I like the 32 bit -gen gas compiler.
64 bit is very jumpy and slow with graphics sometimes, on Win 10 here anyway.
Also, 64 bit handles integers faster although they are twice the size in memory as long.
Of course 64 bit is still newish with freebasic, and so few developers.
hmmm, I am using the 64bit on win 10... and it does seem jumpy - I was just thinking that was the os taking time away from me programs.. sad to think that it is FB itself... I do hate the oversized integers, I really find that most all of my integer ranges fall into the 'ushort' size, so I use that alot and alot of ubytes as well for small ranges - I still have the mindset of 'every byte counts' from 30 years ago... not used to having GBs of memory to play with - but I suppose that this makes the CPU do extra work, only using 16bit or 8bit of its 64 bit potential, probably makes this stuff(ushort n ubyte vars) slower... sigh
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Wonderful 2D Water effects...

Post by Tourist Trap »

leopardpm wrote: I do hate the oversized integers
As far as I know the integers are the most efficient type by design. This is because they talk directly in the right size for FB, the OS, the hardware. Using a different size would mean an extra (hidden) management, so this is not there that you should meet any issue.
Nice water around here!
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wonderful 2D Water effects...

Post by dodicat »

There are probably hundreds of ways to get a flow.
Here is a very simple method, and completely different from the images.
Looks more like a pile of garbage flowing along, but O.K. on 64 bit for speed.

Code: Select all

 


Dim As Integer Xres,Yres
Screen 20,32,,64
Screeninfo (Xres),(Yres)
'=================================
'for shading a pixel
Function shade(c As Ulong,n As Single) As Ulong
    If n>1 Or n<0 Then Exit Function
    Return Rgba(Cptr(Ubyte Ptr,@c)[2]*n,Cptr(Ubyte Ptr,@c)[1]*n,Cptr(Ubyte Ptr,@c)[0]*n,Cptr(Ubyte Ptr,@c)[3])
End Function
'colours for ripples
Dim Shared As Ulong col1=Rgba(0,155,255,50)
Dim Shared As Ulong col2=Rgba(0,100,255,50)
Dim Shared As Ulong col3=Rgba(255,255,255,155)
'optional messing about with the base colours

'col1=shade(col1,.5)
col2=shade(col2,.5)
'col3=shade(col3,.5)
'flow macro


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 ctr,fps
Dim As Any Ptr trn=Imagecreate(xres,yres,rgb(255,255,254))
'=============================

Type pt
    As Long x,y
    As Ulong col
    As Long jmp
End Type


Function spline(p() As Pt,t As Single) As Pt
    #macro set(n)
    0.5 *(     (2 * P(2).n) +_
    (-1*P(1).n + P(3).n) * t +_
    (2*P(1).n - 5*P(2).n + 4*P(3).n - P(4).n) * t*t +_
    (-1*P(1).n + 3*P(2).n- 3*P(3).n + P(4).n) * t*t*t)
    #endmacro
    Return Type<pt>(set(x),set(y))',set(z))
End Function

Sub GetCatmull(v() As Pt,outarray() As Pt,arraysize As Long=1000)
    Dim As Pt p(1 To 4)
    Redim outarray(0)
    Dim As Single stepsize=(Ubound(v)-Lbound(v)+1)/(arraysize)
    If stepsize>1 Then stepsize=1
    For n As Long=Lbound(v)+1 To Ubound(v)-2 
        p(1)=v(n-1):p(2)=v(n):p(3)=v(n+1):p(4)=v(n+2)
        For t As Single=0 To 1 Step stepsize
            Redim Preserve outarray(1 To Ubound(outarray)+1)
            outarray(Ubound(outarray))=spline(p(),t)
        Next t
    Next n
End Sub 
'draw a spline
Sub DrawCurve(a() As Pt,i As Any Ptr=0)
    Pset i,(a(Lbound(a)).x,a(Lbound(a)).y),Rgb(255,255,255)
    For z As Long=Lbound(a)+1 To Ubound(a)
        Line i,-(a(z).x,a(z).y),Rgb(255,255,255)
    Next z
End Sub
'get a point (Length pixels) along a line between fp and p
Function shortline(fp As pt,p As pt,length As Integer) As pt
    Dim As Single diffx=p.x-fp.x,diffy=p.y-fp.y
    Dim As Single L=Sqr(diffx*diffx+diffy*diffy)
    Return Type(fp.x+length*diffx/L,fp.y+length*diffy/L)
End Function

'get the river bank points from the data
Dim As pt P1(0 To 24)
Dim As pt P2(0 To 24)
For n As Long=1 To 24
    Read P1(n).x
Next
For n As Long=1 To 24
    Read P2(n).x
Next
Restore Y_values
For n As Long=1 To 24
    Read P1(n).y
Next
For n As Long=1 To 24
    Read P2(n).y
Next
'add an extra point for the spline
P1(0).x=-20:P1(0).y=P1(1).y
P2(0).x=-20:P2(0).y=P2(1).y
Redim As pt P1C(0),P2c(0)
Dim As Long num=1000
getcatmull(P1(),P1c(),num)
getcatmull(P2(),P2c(),num)

'draw the splines to image trn
drawcurve(P1c(),trn)
drawcurve(P2c(),trn)

'now get a pile of points inside the river banks
Redim As pt a(0)
#define range(f,l) Int(Rnd*((l+1)-(f))+(f))
Dim As Ulong col
For n As Long=Lbound(p1c) To Ubound(P1c)
    Var dist=Sqr((p1c(n).x-P2c(n).x)^2 +(p1c(n).y-P2c(n).y)^2)'distance across river
    For m As Long=1 To dist
        Redim Preserve a(1 To Ubound(a)+1)
        a(Ubound(a))=shortline(p1c(n),p2c(n),m) ' points on a line joining opposite riverbank points
        Dim As Long i=range(0,2)
        Select Case As Const i
        Case 0
            col=col1
        Case 1
            col=col2
        Case 2
            col=col3
        End Select
        a(Ubound(a)).col=col
        a(Ubound(a)).jmp=dist   
    Next m
    
    ' line trn,(P1c(n).x,P1c(n).y)-(P2c(n).x,P2c(n).y)
Next

'print ubound(P1c),ubound(P2c)


Paint trn,(900,100),Rgb(0,100,0),Rgb(255,255,255)
Paint trn,(100,600),Rgb(0,100,0),Rgb(255,255,255)
'add a touch of randomness
For n As Long=Lbound(a) To Ubound(a)
    a(n).x+=Rnd-Rnd:a(n).y+=Rnd-Rnd
Next
'====================================================

Do
    ctr+=1
    
    Screenlock
    'Cls  'optional
    If ctr Mod 3=0 Then
        ctr=0
        'swap through the colours to achieve a kind of flow look.
        For n As Long=Lbound(a) To Ubound(a)-50
        Swap a(n).col,a(n+a(n).jmp).col
            Circle trn,(a(n).x,a(n).y),2,a(n).col,,,,f
            ' pset trn,(a(n).x,a(n).y),a(n).col
        Next
    End If
    Put(0,0),trn,Alpha 'background plus flow
    Locate 1,1
    Print fps
    Screenunlock
    Sleep regulate(50,fps),1
Loop Until Len(Inkey)

' Imagedestroy image
Imagedestroy trn
Screen 0
End
X_values:

Data _
0, 114, 176, 237, 247, 300, 360, 447, 512, 568, 591, 628, 688, 788, 838, 859, 853, 839, 807, 801, 888, 960, 1063, 1150, 0, 98, 143, 187, 195, 245, 326, 446, 532, 595, 622, 654, 688, 769, 798, 796, 790, 776, 736, 738, 830, 951, 1063, 1151 

Y_values:

Data _
100, 118, 147, 224, 282, 342, 370, 366, 337, 279, 219, 150, 117, 135, 222, 316, 372, 439, 535, 595, 664, 695, 712, 721, 154, 160, 181, 231, 282, 351, 398, 406, 373, 295, 226, 172, 142, 150, 224, 317, 364, 429, 518, 599, 687, 733, 760, 770 



leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

really nice! you can actually follow a bit of garbage along the flow of the entire river! So the flow is good(great!), now to combine with something that looks like water
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Wonderful 2D Water effects...

Post by Tourist Trap »

dodicat wrote:There are probably hundreds of ways to get a flow.
Here is a very simple method, and completely different from the images.
Looks more like a pile of garbage flowing along, but O.K. on 64 bit for speed.
This is more a cloud flow in the sky. Believe me it reminds me the game, the day of the comet, if you have an ambiance of mystery and such a sky above it's tremendous. Nice work.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

Tourist Trap wrote:
dodicat wrote:There are probably hundreds of ways to get a flow.
Here is a very simple method, and completely different from the images.
Looks more like a pile of garbage flowing along, but O.K. on 64 bit for speed.
This is more a cloud flow in the sky. Believe me it reminds me the game, the day of the comet, if you have an ambiance of mystery and such a sky above it's tremendous. Nice work.
clouds moving across the sky is easier - all in the same direction, just basic perlin noise, I think... I have seen this as cloud shadows on the ground... and when stormy weather comes then more clouds are used and the entire terrain darkens from the shadows... it is a nice effect
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Wonderful 2D Water effects...

Post by Tourist Trap »

leopardpm wrote:clouds moving across the sky is easier - all in the same direction, just basic perlin noise
Yes moreover Shadow of the comet has outdated graphics, so it's more about the ambiance than the effect. Anyway interesting so far. Sad that I've lost the link about making water with FFT (fast fourier transform). This maybe would be of interest to you.
Something like this: http://www.keithlantz.net/2011/11/ocean ... transform/
But seems a little tricky.

Apparently this is this article that everyone takes for reference:
http://graphics.ucsd.edu/courses/render ... endorf.pdf
Post Reply