@Josef Sniatecki
Fast komplett richtig.
Ja ich verwende ein abgeändertes Gravitationsgesetzt, wodurch sich alle 6 Punkte anziehen.
Würden nun alle genau alle 60° positioniert sein, würden sie aufeinander zufliegen und wieder auseinander (kollision außeracht gelassen)
deswegen werden sie leicht verschoben.
Außerdem verwende ich keine 1/r² abhängigkeit, weil die hier einfach zu kurzsreichweitig ist, und dann zum den von teamO angesprochenen Problem führt, dass die Teilchen früher oder später hinaus katapultiert werden.
Ich verwende hier eine 1/Wurzel(r) abhänigkeit.
Ich habs mal auf eine kleine Demo reduziert:
An Anfang kommen ein paar Vektor-Macros die ich in einem Include habe,
weiter unten kommt der eigentliche Kern, dort mit ein paar Kommentaren:
Code: Alles auswählen
InitSprite()
Enumeration
#Window
EndEnumeration
Structure Vector2D
x.f
y.f
EndStructure
Macro Vector2DSub(Vector2D_1, Vector2D_2, ResultVector2D)
ResultVector2D\x = Vector2D_1\x - Vector2D_2\x : ResultVector2D\y = Vector2D_1\y - Vector2D_2\y
EndMacro
Macro Vector2DLength(Vector2D)
Sqr( Vector2D\x*Vector2D\x+Vector2D\y*Vector2D\y )
EndMacro
Macro MoveVector2D(Vector2D, MoveVector2D)
Vector2D\x + MoveVector2D\x
Vector2D\y + MoveVector2D\y
EndMacro
Procedure NormVector2D(*Vector2D.Vector2D)
Protected Length.f = Vector2DLength(*Vector2D)
If Length
*Vector2D\x / Length
*Vector2D\y / Length
Else
*Vector2D\x = 0
*Vector2D\y = 0
EndIf
EndProcedure
Macro RaiseVector2D(Vector2D, Factor)
Vector2D\x * (Factor)
Vector2D\y * (Factor)
EndMacro
Macro LengthVector2D(Vector2D, Length)
NormVector2D(Vector2D)
RaiseVector2D(Vector2D, Length)
EndMacro
Structure Photon
Position.Vector2D
Velocity.Vector2D
EndStructure
Global NewList Photon.Photon()
#Anzahl = 6
OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 800, 600, "Beispiel", #PB_Window_MinimizeGadget|#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window), 0, 0, 0)
RandomSeed(3)
For n = 1 To #Anzahl
AddElement(Photon())
With Photon()
Angle.f = Radian(n*60+Random(30))
\Position\x = WindowWidth(#Window)/2 + Cos(Angle)*200
\Position\y = WindowHeight(#Window)/2 + Sin(Angle)*200
EndWith
Next
Repeat
Delay(5)
If WindowEvent() = #PB_Event_CloseWindow : End : EndIf
Difference.Vector2D
ForEach Photon() : With Photon()
*Photon.Photon = Photon()
While NextElement(Photon())
;----------------
; Distanzvektor beider Teilchen
Vector2DSub(\Position, *Photon\Position, Difference)
; Abstand beider Teilchen
Distance.f = Vector2DLength(Difference)
; Umwandlung zum Kraftvektor (-5/Wurzel(Abstand))
LengthVector2D(Difference, -10/Pow(Distance,0.5))
; Veränderung der beiden Geschwindigkeiten
MoveVector2D(\Velocity, Difference)
MoveVector2D(*Photon\Velocity, -Difference)
;----------------
Wend
ChangeCurrentElement(Photon(), *Photon)
EndWith : Next
ClearScreen(0)
StartDrawing(ScreenOutput())
ForEach Photon() : With Photon()
MoveVector2D(\Position, 0.030*\Velocity)
Circle(\Position\x, \Position\y, 10, $FFFFFF)
EndWith : Next
StopDrawing()
FlipBuffers()
ForEver