Saved me some time! thanks
You get nice clean elipses, I don't, if you go into the options menu and turn trails you can see I get kind of three petaled flower shapes (more or less depending on distance)
The constants at the top can be fiddled with but there's something wrong with my algorithm or the way I'm calculating vectors...
Any tips on where I'm going wrong?
Code: Select all
Structure Star
LocX.f
LocY.f
VectX.f
VectY.f
Mass.l
Colour.l
Size.l
EndStructure
; play with these
#StarCount = 7
#ScrWidth = 1200
#ScrHeight = 900
#StarSize = 4
#DistScale = 600
#StartingVelocity = 500
#RedGiantMass = 500000000
#PlanetMassDiv = 0.5;0
Declare UpdateStarVectors(StarIdx.l) ;Idx is to skip
Declare.f GravMag(*Star1.star, *Star2.star)
Declare Main()
Declare ReStart()
Global Dim Stars.Star(#StarCount)
Global MaxDist.f = Sqr(Pow(#ScrWidth,2) + Pow(#ScrHeight,2))
Global ShowTrails.l
Global UseRedGiant.l = #True ;#False
Main()
Procedure ReStart()
StartDrawing(ImageOutput(0))
Box(0, 0, #ScrWidth, #ScrHeight,0)
StopDrawing()
ReDim Stars.Star(#StarCount)
For I = 1 To #StarCount
; Stars(i)\Mass = Random(999) + 1
Stars(i)\LocX = (Random(#ScrWidth - 2) + 1) * #DistScale
Stars(i)\LocY = (Random(#ScrHeight - 2) + 1) * #DistScale
stars(i)\vectx = #StartingVelocity / 2 - Random(#StartingVelocity)
stars(i)\vecty = #StartingVelocity / 2 - Random(#StartingVelocity)
Stars(i)\Size = #StarSize
col.l = Random(255) ;the red'er the planet the heavier
Stars(i)\Colour = RGB(col,Random(255),Random(255));RGB(Random(255),Random(255),Random(255))
Stars(i)\mass = Pow(col,2) / #PlanetMassDiv
Next
If UseRedGiant = #True
Stars(1)\Size = 10
Stars(1)\mass = #RedGiantMass
Stars(1)\Colour = RGB(255,10,10)
Stars(1)\LocX = #ScrWidth / 2 * #DistScale
Stars(1)\Locy = #ScrHeight / 2 * #DistScale
stars(1)\vectx = 0
stars(1)\vecty = 0
EndIf
EndProcedure
;=======================================================================
Procedure Main()
If OpenWindow(0, 0, 0, #ScrWidth, #ScrHeight, "Rnd Star Field", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget); And CreateGadgetList(WindowID(0))
If CreateImage(0, #ScrWidth, #ScrHeight, 32)
ImageGadget(0, 0, 0, #ScrWidth, #ScrHeight, ImageID(0)) ; imagegadget standard
EndIf
If CreateMenu(0, WindowID(0))
MenuTitle("File")
MenuItem( 1, "Start / Restart")
MenuTitle("Options")
MenuItem(2, "Show Trails")
MenuItem(3, "Red Giant")
EndIf
SetMenuItemState(0,3,1)
frame.q = 0
Repeat
frame = frame + 1
StartDrawing(ImageOutput(0))
;clear screen
If ShowTrails = #False
Box(0, 0, #ScrWidth, #ScrHeight,0)
EndIf
For i = 1 To #StarCount
UpdateStarVectors(i)
If UseRedGiant = #True
Stars(1)\LocX = #ScrWidth / 2 * #DistScale
Stars(1)\Locy = #ScrHeight / 2 * #DistScale
stars(1)\vectx = 0
stars(1)\vecty = 0
EndIf
Next
For I = 1 To #StarCount
If Stars(i)\LocX < 1 Or Stars(i)\LocY < 1 Or Stars(i)\LocX / #DistScale > #ScrWidth Or Stars(i)\LocY / #DistScale > #ScrHeight
Else
If Stars(i)\Size = 1
Box(Stars(i)\LocX/#DistScale, Stars(i)\LocY/#DistScale, Stars(i)\Size ,Stars(i)\Size ,Stars(i)\Colour)
Else
Circle(Stars(i)\LocX/#DistScale, Stars(i)\LocY/#DistScale, Stars(i)\Size / 2 , Stars(i)\Colour)
EndIf
EndIf
Stars(i)\LocX = Stars(i)\LocX + Stars(i)\VectX
Stars(i)\LocY = Stars(i)\LocY + Stars(i)\VectY
Next
StopDrawing()
SetGadgetState(0, ImageID(0))
Repeat
event.l = WindowEvent()
Select event
Case #PB_Event_Menu
Select EventMenu() ; To see which menu has been selected
Case 1 ; Restart
ReStart()
Case 2 ; Trails
If ShowTrails = #False
ShowTrails = #True
SetMenuItemState(0,2,1)
Else
ShowTrails = #False
SetMenuItemState(0,2,0)
EndIf
Case 3 ; Red giant
If UseRedGiant = #False
UseRedGiant = #True
SetMenuItemState(0,3,1)
Else
UseRedGiant = #False
SetMenuItemState(0,3,0)
EndIf
EndSelect
EndSelect
Until event = 0 Or event = #PB_Event_CloseWindow
If frame % 10 = 0
Delay(10)
EndIf
Until event = #PB_Event_CloseWindow
EndIf
EndProcedure
;=======================================================================
Procedure.f GravMag(*Star1.star, *Star2.star)
Distx.l = (*Star2\LocX - *Star1\LocX)
Disty.l = (*Star2\LocY - *Star1\LocY)
If Distx < 0: Distx = Distx * -1: EndIf
If Disty < 0: Disty = Disty * -1: EndIf
Dist.d = Sqr(Pow(Distx,2) + Pow(Disty,2))
;ProcedureReturn (1 / Pow((Dist),2)) * *Star2\mass
ProcedureReturn *Star2\mass * 0.006673 / Pow((Dist),2)
EndProcedure
;=======================================================================
Procedure UpdateStarVectors(StarIdx.l) ;Idx is to skip
For i = 1 To #StarCount
If i <> StarIDx
;stop stars shooting off too fast
If (Stars(i)\locX - Stars(StarIdx)\locX > 1 Or Stars(StarIdx)\locX - Stars(i)\locX > 1) And (Stars(i)\locy - Stars(StarIdx)\locy > 1 Or Stars(StarIdx)\locy - Stars(i)\locy > 1)
;Get Distance for grav strength
Magnitude.d = GravMag(@Stars(StarIdx), @Stars(i))
;get vector describing diffrence between star 1 and 2 and add
VectX.f = VectX + ((Stars(i)\locX - Stars(StarIdx)\locX) * Magnitude)
VectY.f = VectY + ((Stars(i)\locY - Stars(StarIdx)\locY) * Magnitude)
EndIf
EndIf
Next
Stars(StarIdx)\VectX = Stars(StarIdx)\VectX + (VectX * 0.01)
Stars(StarIdx)\VectY = Stars(StarIdx)\VectY + (VectY * 0.01)
EndProcedure
;=======================================================================