1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| ;*******************************************************************************
;*
;* July-August 2014 PurePunch contest
;* PureBasic 5.22
;* 50 lines of 80 chars, 2 months delay
;*
;* Name : Fluid Dynamics Game Engine
;* Author : Graph100
;* Date : 07/07/2014
;* Notes : Made from reading Jos Stam's article
;* "Real-Time Fluid Dynamics for Games"
;*
;*******************************************************************************
Macro M:Macro:EndMacro:M E:End:End#M:M A1:Array:EndMacro;#######################
M N:Next:E#M:M F(i,j,k):For i=j To k:E#M:M St:Structure:E#M:M P:Procedure:E#M;##
M El:ElapsedMilliseconds():E#M:M Ti(a,s):a=AllocateMemory(SizeOf(s));###########
Initialize#St(a,s):EndMacro:Macro AO(o,f,a,b):Define.d x,y,s0,t0,s1,t1,t0u,t0v;#
t0u=s\t*f\m:t0v=s\t*f\m:i0=Round(o\x,0):j0=Round(o\y,0):i1=i0+1:j1=j0+1;########
s1=o\x-i0:s0=1-s1:t1=o\y-j0:t0=1-t1:o\u=t0u*(s0*(t0*a(i0,j0)+t1*a(i0,j1))+;#####
s1*(t0*a(i1,j0)+t1*a(i1,j1))):o\v=t0v*(s0*(t0*b(i0,j0)+t1*b(i0,j1))+s1*(t0*b(i1,
j0)+t1*b(i1,j1))):o\x=o\x+o\u:o\y=o\y+o\v:If o\x<1:o\x=1:E#If:If o\x>f\W:o\x=f\W
E#If:If o\y<1:o\y=1:E#If:If o\y>f\H:o\y=f\H:E#If:EndMacro:M AF(o,a,b,c,d);######
AddElement(o):o\x=a:o\y=b:o\u=c:o\v=d:o\g=Sqr(c*c+d*d):EndMacro:M AOb(o,a,b);###
AddElement(o):o\x=a:o\y=b:EndMacro:St t:A1 t.d(0,0):E#St:St Fl:a.d:W.l:H.l:e.l;#
f.l:m.d:*u.t:*v.t:*s.t:*t.t:E#St:St Ob:x.d:y.d:u.d:v.d:E#St:St Sv:x.l:y.l:u.d;##
v.d:g.d:E#St:St Sf:F.Fl:t.d:List O.Ob():List Sv.Sv():E#St:P BC(*o.Fl,Mo,;#######
A1 x.d(2)):If Mo=2:F(i,1,*o\W):x(i,0)=-x(i,1):x(i,*o\f)=-x(i,*o\H):N:Else:F(i,1,
*o\W):x(i,0)=x(i,1):x(i,*o\f)=x(i,*o\H):N:E#If:If Mo=1:F(i,1,*o\H):x(0,i)=-x(1,;
i):x(*o\e,i)=-x(*o\W,i):N:Else:F(i,1,*o\H):x(0,i)=x(1,i):x(*o\e,i)=x(*o\W,i):N;#
E#If:x(0,0)=(x(1,0)+x(0,1))/2:x(0,*o\f)=(x(1,*o\f)+x(0,*o\H))/2:x(*o\e,0)=(0+;##
x(*o\W,0)+x(*o\e,1))/2:x(*o\e,*o\f)=(x(*o\W,*o\f)+x(*o\e,*o\H))/2:E#P;##########
P LS(*o.Fl,MO,A1 x.d(2),A1 x0.d(2),a.d,c.d):F(k,0,20):F(i,1,*o\W):F(j,1,*o\H);##
x(i,j)=(x0(i,j)+a*(x(i-1,j)+x(i+1,j)+x(i,j-1)+x(i,j+1)))/c:N:N:BC(*o,MO,x()):N;#
E#P:P AD(*o.Fl,A1 x.d(2),A1 s.d(2),t.d):F(i,0,*o\e):F(j,0,*o\f):x(i,j)=x(i,j)+;#
t.d*s(i,j):N:N:E#P:P Di(*o.Fl,MO,A1 x.d(2),A1 x0.d(2),d.d,t.d):a.d=t*d**o\W**o\H
LS(*o,MO,x(),x0(),a,1+4*a):E#P:P Av(*o.Fl,MO,A1 d.d(2),A1 d0.d(2),A1 u.d(2),;###
A1 v.d(2),t.d):t0_u.d=t**o\m:t0_v.d=t**o\m:F(i,1,*o\W):F(j,1,*o\H):x.d=i+;######
-t0_u*u(i,j):y.d=j-t0_v*v(i,j):If x<0.5:x=0.5:E#If:If x>*o\W+0.5:x=*o\W+0.5:E#If
i0=Round(x,0):i1=i0+1:If y<0.5:y=0.5:E#If:If y>*o\H+0.5:y=*o\H+0.5:E#If:j0=0+;##
Round(y,0):j1=j0+1:s1.d=x-i0:s0.d=1-s1:t1.d=y-j0:t0.d=1-t1:d(i,j)=s0*(t0*d0(i0,;
j0)+t1*d0(i0,j1))+s1*(t0*d0(i1,j0)+t1*d0(i1,j1)):N:N:BC(*o,MO,d()):E#P;#########
P Pr(*o.Fl,A1 u.d(2),A1 v.d(2),A1 q.d(2),A1 dv.d(2)):F(i,1,*o\W):F(j,1,*o\H);###
dv(i,j)=-((u(i+1,j)-u(i-1,j)+v(i,j+1)-v(i,j-1))/*o\m)/2:q(i,j)=0:N:N:BC(*o,0,;##
dv()):BC(*o,0,q()):LS(*o,0,q(),dv(),1,4):F(i,1,*o\W):F(j,1,*o\H):u(i,j)=u(i,j)+;
-*o\W*(q(i+1,j)-q(i-1,j))/2:v(i,j)=v(i,j)-*o\H*(q(i,j+1)-q(i,j-1))/2:N:N:BC(*o,;
1,u()):BC(*o,2,v()):E#P:Define s.Sf:s\F\W=50:s\F\H=50:s\F\m=s\F\H:If s\F\W>s\F\H
s\F\m=s\F\W:EndIf:s\F\e=s\F\w+1:s\F\f=s\F\h+1:Ti(s\F\u,t):Ti(s\F\v,t):Ti(s\F\s,;
t):Ti(s\F\t,t):Dim s\F\u\t(s\F\e,s\F\f):Dim s\F\v\t(s\F\e,s\F\f);###############
Dim s\F\s\t(s\F\e,s\F\f):Dim s\F\t\t(s\F\e,s\F\f):AF(s\Sv(),25,25,50,10);#######
AOb(s\O(),20,20):OpenWindow(0,0,0,520,520,"",$C80001):CanvasGadget(0,0,0,520,;##
520,4):te=El:Repeat:Repeat:ev=WindowEvent():Until ev=0 Or ev=13116:s\t=(El+;####
-te)/1000:Delay(20):te=El:Dim s\F\s\t(s\F\e,s\F\f):Dim s\F\t\t(s\F\e,s\F\f);####
ForEach s\Sv():s\F\s\t(s\Sv()\x,s\Sv()\y)=s\Sv()\u:s\F\t\t(s\Sv()\x,s\Sv()\y)=0+
s\Sv()\v:N:AD(s\F,s\F\u\t(),s\F\s\t(),s\t):AD(s\F,s\F\v\t(),s\F\t\t(),s\t);#####
If s\F\a:Swap s\F\s,s\F\u:Swap s\F\t,s\F\v:Di(s\F,1,s\F\u\t(),s\F\s\t(),s\F\a,;#
s\t):Di(s\F,2,s\F\v\t(),s\F\t\t(),s\F\a,s\t):E#If:Pr(s\F,s\F\u\t(),s\F\v\t(),;##
s\F\s\t(),s\F\t\t()):Swap s\F\s,s\F\u:Swap s\F\t,s\F\v:Av(s\F,1,s\F\u\t(),;#####
s\F\s\t(),s\F\s\t(),s\F\t\t(),s\t):Av(s\F,2,s\F\v\t(),s\F\t\t(),s\F\s\t(),;#####
s\F\t\t(),s\t):Pr(s\F,s\F\u\t(),s\F\v\t(),s\F\s\t(),s\F\t\t()):ForEach s\O();###
AO(s\O(),s\F,s\F\u\t,s\F\v\t):N:StartDrawing(CanvasOutput(0)):Box(0,0,800,600,0)
F(i,0,s\F\e):For j=0 To s\F\f:LineXY(10*i,10*j,10*i+100*s\F\u\t(i,j),10*j+;#####
100*s\F\v\t(i,j),$FF):N:N:ForEach s\O():Circle(s\O()\x*10,s\O()\y*10,3,#Green):N
StopDrawing():Until ev=13116;########BY#GRAPH100#####PUREPUNCH#AOUT#2014######## |
Partager