REM ***********************************************
REM Title: Wavey flag
REM Author: Unknown
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
sync on : randomize timer()
 
 
rem MAKE THE NOISE
 
rem noise vars
roughness#=0.60
dim noise#(32,4)
dim noise2#(32,4)
 
rem make noise
gosub makenoise
 
rem transfer array
for x=0 to 32
for y=0 to 4
noise2#(x,y)=noise#(x,y)
next y
next x
 
rem make noise
gosub makenoise
 
rem precalculate
frames#=300
interpolation#=30
dim height#(8,4,frames#+interpolation#)
gosub precalc
 
 
rem load texture
load image "pyramid.bmp",1
 
rem make the matrix
make matrix 1,80,40,8,4
prepare matrix texture 1,1,8,4
for x=0 to 7
for z=0 to 3
set matrix tile 1,x,3-z,(z*8)+x+1
next z
next x
update matrix 1
 
rem make cylinder
make object cylinder 1,4
scale object 1,100,2000,100
xrotate object 1,90
color object 1,rgb(80,60,0)
 
 
rem set the camera
position camera 0,100,0
point camera 0,0,0
color backdrop rgb(200,200,200)
set text transparent
 
rem vars
t#=interpolation#
amplify#=4.0
 
 
 
 
 
 
 
rem DO LOOP
 
do
 
text 0,0,str$(screen fps())
 
rem movement
yrotate camera wrapvalue(camera angle y()+mousemovex())
xrotate camera wrapvalue(camera angle x()+mousemovey())
if mouseclick()=1 then move camera 2
if mouseclick()=2 then move camera -2
 
 
rem animate matrix
for x=0 to 8
for y=0 to 4
h#=((height#(x,y,int(t#))*(t#-int(t#)))+(height#(x,y,int(t#)+1)*((int(t#)+1)-t#)))
set matrix height 1,x,y,h#*amplify#
next y
next x
normalize(1,8,4,1,1,7,3)
update matrix 1
 
rem update var
t#=t#+2.0
if t#>=frames#+interpolation# then t#=interpolation#
 
sync
loop
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
precalc:
 
set text opaque
 
rem flag vars
scroll#=0.0
pf#=20.0
wf#=2.00
ff#=8.00
friction#=0.25
dim flagspeed#(16,4)
 
 
 
 
 
rem THE LOOP
 
for t=0 to frames#
 
dt#=0.0010
 
for q=1 to 100
 
for x=0 to 8
for y=0 to 4
 
rem Pressure force
 
xarray#=scroll#+x
if xarray#>31 then xarray#=xarray#-31
 
rem calculate pressure difference
pdif#=(noise2#(int(xarray#),y)*(xarray#-int(xarray#))+noise2#(int(xarray#)+1,y)*((int(xarray#)+1)-xarray#))-(noise#(int(xarray#),y)*(xarray#-int(xarray#))+noise#(int(xarray#)+1,y)*((int(xarray#)+1)-xarray#))
a#=pdif#*pf#
 
 
rem WIND PUSHING
a#=a#+(height#(x,y,t)*wf#*-1.00)
 
rem FLAG PULLING
count#=0
hdif#=0.0
for p=x-1 to x+1
for r=y-1 to y+1
if p<>x or r<>y
if p>=0 and p<=8
if r>=0 and r<=4
dif#=height#(p,r,t)-height#(x,y,t)
hdif#=hdif#+dif#
count#=count#+1
endif
endif
endif
next r
next p
 
rem average
hdif#=hdif#/count#
a#=a#+(hdif#*ff#)
 
rem friction
a#=a#-(friction#*flagspeed#(x,y))
 
rem calculate new speed and position
flagspeed#(x,y)=flagspeed#(x,y)+(a#*dt#)
height#(x,y,t)=height#(x,y,t)+(flagspeed#(x,y)*dt#)
if x=0 then height#(x,y,t)=0
 
 
next y
next x
 
rem update scroll var
scroll#=scroll#+(dt#*2.5)
if scroll#>=32 then scroll#=scroll#-32.0
 
next q
 
center text 320,200,"LOADING... "+str$(int((t/(frames#+interpolation#))*100.00))+"%"
sync
 
next t
 
 
 
 
rem make cubic interpolation
for t=frames# to frames#+interpolation#
for x=0 to 8
for y=0 to 4
yb#=height#(x,y,frames#)
yc#=height#(x,y,interpolation#)
ya#=height#(x,y,frames#)+((height#(x,y,frames#-1)-height#(x,y,frames#))*interpolation#)
yd#=height#(x,y,interpolation#)+((height#(x,y,interpolation#+1)-height#(x,y,interpolation#))*interpolation#)
part#=(t-frames#)/interpolation#
part2#=part#*part#
a0#=(yd#-yc#-ya#)+yb#
a1#=ya#-yb#-a0#
a2#=yc#-ya#
a3#=yb#
height#(x,y,t)=(a0#*part#*part2#)+(a1#*part2#)+(a2#*part#)+a3#
next y
next x
 
center text 320,200,"LOADING... "+str$(int((t/(frames#+interpolation#))*100.00))+"%"
sync
 
next t
 
 
return
 
 
 
 
 
 
 
 
 
 
 
makenoise:
 
s=4
 
rem calculate noise
repeat
 
for x=0 to 32 step s
for y=0 to 4 step s
 
x#=x : y#=y : s#=s
 
if s=4
noise#(x,y)=(rnd(2000)-1000)/1000.0
endif
 
if s<>4
 
rem centre
if x#/(2.0*s#)<>int(x#/(2.0*s#)) and y#/(2.0*s#)<>int(y#/(2.0*s#))
noise#(x,y)=((noise#(x-s,y-s)+noise#(x-s,y+s)+noise#(x+s,y-s)+noise#(x+s,y+s))/4.00)+(roughness#*((rnd(2000)-1000)/1000.0))
endif
 
rem side
if x#/(2.0*s#)=int(x#/(2.0*s#)) and y#/(2.0*s#)<>int(y#/(2.0*s#))
noise#(x,y)=((noise#(x,y-s)+noise#(x,y+s))/2.00)+(roughness#*((rnd(2000)-1000)/1000.0))
endif
 
rem side
if x#/(2.0*s#)<>int(x#/(2.0*s#)) and y#/(2.0*s#)=int(y#/(2.0*s#))
noise#(x,y)=((noise#(x+s,y)+noise#(x-s,y))/2.00)+(roughness#*((rnd(2000)-1000)/1000.0))
endif
 
 
 
endif
 
 
 
 
rem make seamless
if x=32 then noise#(x,y)=noise#(0,y)
 
next y
next x
 
s=s/2
 
until s=0
 
 
 
 
low#=100.00
 
for x=0 to 32
for y=0 to 4
if noise#(x,y)<low# then low#=noise#(x,y)
next y
next x
 
for x=0 to 32
for y=0 to 4
noise#(x,y)=noise#(x,y)-low#
next y
next x
 
rem make it max 1
 
high#=-100.0
 
for x=0 to 32
for y=0 to 4
if noise#(x,y)>high# then high#=noise#(x,y)
next y
next x
 
for x=0 to 32
for y=0 to 4
noise#(x,y)=noise#(x,y)/high#
next y
next x
 
return
 
 
 
 
 
 
 
 
 
rem NORMALIZE
 
 
function normalize(mat,mattilex,mattilez,bx,bz,ex,ez)
 
if bx<=0 then bx=1
if bz<=0 then bz=1
if ex>=mattilex then ex=mattilex-1
if ez>=mattilez then ez=mattilez-1
 
rem Use matrix normals to make it smooth
for z=bz to ez
   for x=bx to ex
 
      rem Get matrix heights
      h8#=get matrix height(mat,x,z-1)
      h4#=get matrix height(mat,x-1,z)
      h#=get matrix height(mat,x,z)
      h2#=get matrix height(mat,x,z)
 
      rem Calculate projected angle X using heights
      x1#=(x-1)*25.0 : y1#=h#
      x2#=(x+0)*25.0 : y2#=h4#
      dx#=x2#-x1#
      dy#=y2#-y1#
      ax#=atanfull(dx#,dy#)
      ax#=wrapvalue(90-ax#)
 
      rem Calculate projected angle Z using heights
      z1#=(z-1)*25.0 : y1#=h2#
      z2#=(z+0)*25.0 : y2#=h8#
      dz#=z2#-z1#
      dy#=y2#-y1#
      az#=atanfull(dz#,dy#)
      az#=wrapvalue(90-az#)
 
      rem Make normal from projected angle
      nx#=sin(ax#)
      ny#=cos(ax#)
      nz#=sin(az#)
 
      rem Setting matrix normal for smoothness
      set matrix normal mat,x,z,nx#,ny#/6.0,nz#
 
   next x
next z
update matrix mat
 
endfunction