Demo 2D - Banc de poisson
Publié : dim. 19/nov./2023 10:53
salut les geek,
une petite modélisation d'un banc de poisson
je la trouve tres realiste (mais pas esthetique)
la version 3D devrait suivre
une petite modélisation d'un banc de poisson
je la trouve tres realiste (mais pas esthetique)
la version 3D devrait suivre
Code : Tout sélectionner
; 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)