Demo 2D - Repulsion

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

Demo 2D - Repulsion

Post by pf shadoko »

a small demo without pretention
Remove the debugger
(you can use the mouse)

Code: Select all

; mini demo 2D - répulsion - Pf Shadoko - 2023

ExamineDesktops()
Define i,j,n,rnd,a.f,l=256,ex=DesktopWidth(0),ey=DesktopHeight(0),ry.f=ey/1080,ex2=ex/2,ey2=ey/2,ex3=ex*1.5,ey3=ey*1.5

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 6
  CreateSprite(i,l,l,#PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(i))
  DrawingMode(#PB_2DDrawing_Gradient| #PB_2DDrawing_AllChannels)      
  GradientColor(0,$ffffFFFF)
  GradientColor(1,RGBA(Random(128),Random(128),Random(128),255))
  CircularGradient(l*0.3, l*0.3, l*0.5)     
  Circle(l*0.5, l*0.5, l*0.5)     
  StopDrawing()
Next

Structure spoint
  x.f
  y.f
  r.f
  m.f
  sprite.l
EndStructure

n=200
Dim p.spoint(n)
RandomSeed(4)
For i=0 To n
  With p(i)
    rndf.f=Random(999)/1000
    rnd=Int(rndf*rndf*5)+1
    If i=0:rnd=6:EndIf
    \x=Random(ex)
    \y=Random(ey)
    \r=rnd*15*ry
    \m=rnd*rnd*6000*ry*ry
    \sprite=rnd
  EndWith  
Next

Define.f f,fx,fy,   x,y,m,dx,dy,l2

MouseLocate(ex2,ey2)
Repeat
  While WindowEvent():Wend
  ExamineKeyboard():ExamineMouse()
  
  ClearScreen(RGB(0,0,0))
  
  For j=0 To n:x=p(j)\x:y=p(j)\y:m=p(j)\m:fx=0:fy=0
    For i=0 To n:If i=j:Continue:EndIf
      With p(i)
        dx=\x-x:dx=Mod(dx+ex3,ex)-ex2
        dy=\y-y:dy=Mod(dy+ey3,ey)-ey2
        l2=Sqr(dx*dx+dy*dy):If l2<10:l2=10:EndIf:f=\m/(l2*l2*l2):fx+dx*f:fy+dy*f
      EndWith
    Next
    p(j)\x=Mod(p(j)\x-fx+ex,ex)
    p(j)\y=Mod(p(j)\y-fy+ey,ey)
  Next
  
  If MouseDeltaX():mouseon=1:EndIf
  If mouseon:p(0)\x=MouseX():p(0)\y=MouseY():Else:a+0.01:p(0)\x=Cos(a)*ex/3+ex/2:p(0)\y=Sin(a)*ey/3+ey/2:EndIf
  
  For i=0 To n
    With p(i)
      ZoomSprite(\sprite,\r*2,\r*2):DisplayTransparentSprite(\sprite,\x-\r,\y-\r,255)
    EndWith
  Next
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or MouseButton(3)
ebs
Enthusiast
Enthusiast
Posts: 564
Joined: Fri Apr 25, 2003 11:08 pm

Re: Demo 2D - Repulsion

Post by ebs »

That is amazing - and less than 100 lines of code too!

VERY NICE WORK!
User avatar
mk-soft
Always Here
Always Here
Posts: 6412
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Demo 2D - Repulsion

Post by mk-soft »

Works perfect on macOS ;)

Try sorting everything with the mouse. The big ones in the middle ... You could make a game out of it.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 6095
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Demo 2D - Repulsion

Post by idle »

nice
User avatar
Caronte3D
Addict
Addict
Posts: 1379
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Demo 2D - Repulsion

Post by Caronte3D »

I like it :wink:
BarryG
Addict
Addict
Posts: 4269
Joined: Thu Apr 18, 2019 8:17 am

Re: Demo 2D - Repulsion

Post by BarryG »

I played longer than I should have. Hehe.
AZJIO
Addict
Addict
Posts: 2241
Joined: Sun May 14, 2017 1:48 am

Re: Demo 2D - Repulsion

Post by AZJIO »

I tried it on SpiderBasic

Code: Select all

; mini demo 2D - répulsion - Pf Shadoko - 2023

ExamineDesktops()
Global i,j,n,rnd,a.f,l=256,ex=DesktopWidth(0) - 100,ey=DesktopHeight(0) - 100,ry.f=ey/1080,ex2=ex/2,ey2=ey/2,ex3=ex*1.5,ey3=ey*1.5

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 6
	CreateSprite(i,l,l,#PB_Sprite_AlphaBlending)
	StartDrawing(SpriteOutput(i))
	DrawingMode(#PB_2DDrawing_Gradient| #PB_2DDrawing_AllChannels)      
	;   GradientColor(0,$ffffFFFF)
	;   GradientColor(1,RGBA(Random(128),Random(128),Random(128),255))
	;   CircularGradient(l*0.3, l*0.3, l*0.5)     
	Circle(l*0.5, l*0.5, l*0.5, RGB(Random(255), Random(255), Random(255))) 
	StopDrawing()
Next

Structure spoint
	x.f
	y.f
	r.f
	m.f
	sprite.l
EndStructure

n=200
Global Dim p.spoint(n)
RandomSeed(4)
For i=0 To n
	With p(i)
		rndf.f=Random(999)/1000
		rnd=Int(rndf*rndf*5)+1
		If i=0:rnd=6:EndIf
		\x=Random(ex)
		\y=Random(ey)
		\r=rnd*15*ry
		\m=rnd*rnd*6000*ry*ry
		\sprite=rnd
	EndWith  
Next

Global.f f,fx,fy,   x,y,m,dx,dy,l2

MouseLocate(ex2,ey2)
FlipBuffers()

Procedure RenderFrame()
	; Repeat
	ExamineKeyboard():ExamineMouse()
	
	ClearScreen(RGB(0,0,0))
	
	For j=0 To n:x=p(j)\x:y=p(j)\y:m=p(j)\m:fx=0:fy=0
		For i=0 To n:If i=j:Continue:EndIf
			With p(i)
				dx=\x-x:dx=Mod(dx+ex3,ex)-ex2
				dy=\y-y:dy=Mod(dy+ey3,ey)-ey2
				l2=Sqr(dx*dx+dy*dy):If l2<10:l2=10:EndIf:f=\m/(l2*l2*l2):fx+dx*f:fy+dy*f
			EndWith
		Next
		p(j)\x=Mod(p(j)\x-fx+ex,ex)
		p(j)\y=Mod(p(j)\y-fy+ey,ey)
	Next
	
	If MouseDeltaX():mouseon=1:EndIf
	If mouseon:p(0)\x=MouseX():p(0)\y=MouseY():Else:a+0.01:p(0)\x=Cos(a)*ex/3+ex/2:p(0)\y=Sin(a)*ey/3+ey/2:EndIf
	
	For i=0 To n
		With p(i)
			ZoomSprite(\sprite,\r*2,\r*2):DisplayTransparentSprite(\sprite,\x-\r,\y-\r,255)
		EndWith
	Next
	FlipBuffers()
	; Until KeyboardPushed(#PB_Key_Escape) Or MouseButton(3)
	If MouseButton(#PB_MouseButton_Right):End:EndIf
EndProcedure

BindEvent(#PB_Event_RenderFrame, @RenderFrame())
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Demo 2D - Repulsion

Post by firace »

Nice demo!

Just one thing, I have a transparency issue any time circles are overlapping.

I was able to get rid of the issue by removing the #PB_Sprite_AlphaBlending flag.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 426
Joined: Thu Jul 09, 2015 9:07 am

Re: Demo 2D - Repulsion

Post by pf shadoko »

@firace :

which OS do you use ?

(I have this problem under directx (probably a new bug), but by default the code runs under opengl ...)
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Demo 2D - Repulsion

Post by firace »

pf shadoko wrote: Thu Apr 13, 2023 4:51 pm @firace :

which OS do you use ?

(I have this problem under directx (probably a new bug), but by default the code runs under opengl ...)
windows 10 x64

but i'm still running an older version of PB (v5.62)

edit: you're right, it seems related to directx:
the bug doesn't occur if I manually specify opengl in Library subsystem
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5526
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Demo 2D - Repulsion

Post by Kwai chang caine »

Very splendid effect like 3D :shock:
I don't know if it's normal, but here the black corners of each ball is visible :|
But thanks for sharing this jewell 8)
ImageThe happiness is a road...
Not a destination
Post Reply