Demo 2D - Shoal of fish

Share your advanced PureBasic knowledge/code with the community.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 285
Joined: Thu Jul 09, 2015 9:07 am

Demo 2D - Shoal of fish

Post by pf shadoko »

hi geeks,

a small modeling of a Shoal of fish
I find it very realistic (but not aesthetic)
remove the debugger
the 3D version should follow

Code: Select all

; mini demo 2D - banc de poisson - Pf Shadoko - 2023

ExamineDesktops()
Define i,j,ex=DesktopWidth(0),ey=DesktopHeight(0),ex2=ex/2,ey2=ey/2
Define ns=20,h=64,l=8,n=200

InitSprite():InitKeyboard():InitMouse()
OpenWindow(0, 0, 0, ex,ey, "", #PB_Window_BorderLess):OpenWindowedScreen(WindowID(0), 0, 0, ex,ey, 0, 0, 0)

For i=1 To ns
  CreateSprite(i,l,h,#PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(i))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Ellipse(l/2, h*0.4, l/2,h*0.4,RGBA(64+Random(127),64+Random(127),64+Random(127),255))   
  LineXY(l/2,h*0.7,l/2,h,$ffffffff)
  Ellipse(l*0.2,h*0.1,1,2,$ffffffff)
  Ellipse (l*0.8,h*0.1,1,2,$ffffffff)
  StopDrawing()
Next

Procedure.f POM(v.f)
  ProcedureReturn (Random(v*1000)-v*500)/500
EndProcedure

Structure f2
  x.f
  y.f
EndStructure

Procedure f2(*p.f2,x.f,y.f)
  *p\x=x
  *p\y=y
EndProcedure

Procedure add2d(*p.f2,*p1.f2,*p2.f2)
  *p\x=*p1\x+*p2\x
  *p\y=*p1\y+*p2\y
EndProcedure

Procedure sub2D(*p.f2,*p1.f2,*p2.f2)
  *p\x=*p1\x-*p2\x
  *p\y=*p1\y-*p2\y
EndProcedure

Procedure.f lng2D(*v.f2)
  ProcedureReturn Sqr(*V\x * *V\x + *V\y * *V\y)
EndProcedure

Procedure norme2d(*p.f2,lg.f=1)
  Protected l.f=lng2d(*p)/lg
  *p\x/l
  *p\y/l
EndProcedure

Structure spoisson
  p.f2  ;position
  v.f2  ;vitesse
  sprite.l
EndStructure

Dim p.spoisson(n)
For i=0 To n
  With p(i)
    \p\x=Random(ex)
    \p\y=Random(ey)
    \v\x=pom(1)
    \v\y=pom(1)
    norme2d(\v,4)
    \sprite=Random(ns-1)+1
  EndWith  
Next

Define.f f,  dx,dy,a,ai, lg,              align=0.1,prox=50
Define ff.f2,cc.f2,d.f2,np,  *p.spoisson,centre.f2,t0=-10000
f2(centre,ex2,ey2)
Repeat
  If ElapsedMilliseconds()-t0>5000:t0=ElapsedMilliseconds():f2(centre,ex2+pom(ex2/2),ey2+pom(ey2/2)):EndIf
  While WindowEvent():Wend
  ExamineKeyboard():ExamineMouse()
  
  ClearScreen(RGB(0,0,0))
  
  For j=0 To n
    *p=p(j):np=0
    f2(ff,pom(0.5),pom(0.5))
    For i=0 To n:If i=j:Continue:EndIf
      With p(i)
        sub2d(d,\p,*p)
        lg=lng2d(d):If lg>prox:Continue:EndIf
        f=100/(lg*lg) - 4000/(lg*lg*lg); attirance - repulsion
        If IsNAN(f):Continue:EndIf
        ff\x+d\x*f+\v\x*align
        ff\y+d\y*f+\v\y*align
        np+1
      EndWith
    Next
    sub2d(cc,*p\p,centre):norme2d(cc,-0.1):add2d(*p\v,*p\v,cc)
    If np:norme2d(ff,0.5):add2d(*p\v,*p\v,ff):EndIf
    norme2d(*p\v,4)
    add2d(*p\p,*p\p,*p\v)
  Next
  
  a+0.2
  For i=0 To n:ai=a+i
    With p(i)
      RotateSprite(\sprite,Degree(ATan2(-\v\y,\v\x))+Sin(ai)*10,0)
      DisplayTransparentSprite(\sprite,\p\x-l/2,\p\y-h/2,255)
    EndWith
  Next
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or MouseButton(3)
User avatar
CDXbow
User
User
Posts: 24
Joined: Mon Aug 12, 2019 5:32 am
Location: Oz

Re: Demo 2D - Shoal of fish

Post by CDXbow »

Nice work again. I get more of termite feel to the movement. Fish schools don't break up as much and collide with another, I suppose the right word might be they show more cohesion.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 285
Joined: Thu Jul 09, 2015 9:07 am

Re: Demo 2D - Shoal of fish

Post by pf shadoko »

I find it prettier, it creates a panic effect

replaces line 73:
align=0.1 -> align=1
fish will align more with their congeners
User avatar
idle
Always Here
Always Here
Posts: 5049
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Demo 2D - Shoal of fish

Post by idle »

looks better with them aligning.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Demo 2D - Shoal of fish

Post by Mijikai »

Love it, thanks for sharing.
Post Reply