Ooops
November 27, 2020, 05:02:44 PM

Author Topic: [bb] Orbital gravity by stu [ 1+ years ago ]  (Read 1236 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Orbital gravity by stu [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Orbital gravity
Author : stu
Posted : 1+ years ago

Description : Some very simple orbital mechanics with a single planet orbiting a star - then a large number of 'asteroids' added to the mix. The asteroids do not effect each other but are effected by both the planet and the star, and there is no collision detection or 'clumping' of matter- I need to find a way to do this without slowing things down to much

Code :
Code: BlitzBasic
  1. ;
  2. ; Gravity demo
  3. ;
  4.  
  5. ;
  6. ; Set up
  7. ;
  8.  
  9. Graphics3D 1024,768
  10.  
  11. ;
  12. ; Global Variables - needs some tidying
  13. ;
  14. Global angle#, radius#
  15. Global earthx#,earthy#,earthvelx#,earthvely#
  16. Global earthangle#,earthradius#,earthrot#
  17. Global earthforce#,eaccelx#,eaccely#
  18. Global eradius#, eangle#
  19.  
  20. SetBuffer BackBuffer()
  21. ;
  22. ; camera
  23. ;
  24. camera = CreateCamera()
  25. CameraViewport camera,0,0,1024,768
  26. CameraRange camera,2,2000
  27. PositionEntity camera,0,-350,-100
  28. RotateEntity camera,-40,0,0
  29.  
  30. AmbientLight(200,100,100)
  31.  
  32. light1=CreateLight(2)
  33. light2=CreateLight(2)
  34. light3=CreateLight(2)
  35. light4=CreateLight(2)
  36.  
  37. LightColor light1,200,100,100
  38. LightColor light3,200,100,100
  39. LightColor light2,200,20,20
  40. LightColor light4,200,20,20
  41.  
  42. PositionEntity light1,20,0,200
  43. PositionEntity light2,0,-20,200
  44. PositionEntity light3,-20,0,200
  45. PositionEntity light4,0,20,200
  46.  
  47. SeedRnd MilliSecs()
  48. ;
  49. ; Particle type
  50. ;
  51. Type Par
  52.         Field xpos#,ypos#
  53.         Field xvel#,yvel#
  54.         Field xp
  55.         Field mass#
  56. End Type
  57.  
  58. ;
  59. ; set up scenario initial conditions
  60. ;
  61. Global sun = CreateSphere(32)
  62. Global Earth = CreateSphere(32)
  63.  
  64. ScaleEntity sun,20,20,20
  65. ScaleEntity Earth,5,5,5
  66. EntityColor sun,255,255,0
  67. EntityColor Earth,50,200,255
  68. ;suntex = LoadTexture("sun.jpg",1)
  69. ;earthtex = LoadTexture("earth.jpg",1)
  70. sunangle# = 90.0
  71. Const SUNMASS = 10000
  72. Const EARTHMASS = 1000
  73. Const Fieldx = 200
  74. Const Fieldy = 200
  75. ; no of particles
  76. Const Parcnt = 1000
  77. Const G# = 0.000667
  78. ; Start limit distance from Sun
  79. Const START = 80
  80. earthx = 100
  81. earthy = 100
  82. earthvelx = 0.2
  83. earthvely = -0.1
  84.  
  85. PositionEntity sun,0,0,200
  86.  
  87.  
  88. PositionEntity Earth,earthx,earthy,200
  89.  
  90. ;Create the Texture
  91. width=1024
  92. sptex = CreateTexture(width,width,1+8)
  93. SetBuffer TextureBuffer(sptex)
  94. For a = 1 To 200
  95.         Plot Rand(0,width-1),Rand(0,width-1)
  96. Next
  97. SetBuffer BackBuffer()
  98. TextureBlend sptex,5
  99.  
  100. ;Create the Sphere
  101. spbox = CreateSphere(5)
  102. ScaleEntity spbox,1000,1000,1000
  103. EntityTexture spbox,sptex
  104. ScaleTexture sptex,.25,.5
  105. EntityFX spbox,1
  106. FlipMesh spbox
  107. EntityOrder spbox,99999
  108.  
  109. ;
  110. ; function to create particle
  111. ;
  112.  
  113.  
  114. Function CreatePar()
  115.         p.Par = New par
  116.         pxpos# = Rand(-Fieldx,Fieldx)
  117.         pypos# = Rand(-Fieldy,Fieldy)
  118.         If pxpos > 0.0 And pypos > 0.0 And pxpos < START And pypos < START
  119.                 pxpos = START
  120.                 pypos = START
  121.         ElseIf pxpos < 0.0 And pypos < 0.0 And pxpos > -START And pypos > -START
  122.                 pxpos = -START
  123.                 pypos = -START
  124.         ElseIf pxpos > 0.0 And pypos < 0.0 And pxpos < START And pypos > -START
  125.                 pxpos = START
  126.                 pypos = -START
  127.         ElseIf pxpos < 0.0 And pypos > 0.0 And pxpos > -START And pypos < START
  128.                 pxpos = -START
  129.                 pypos = START
  130.         EndIf
  131.  
  132.         If pxpos > 0.0 And pypos > 0.0
  133.                 pxvel = Rand(1,1.2)/10.0
  134.                 pyvel = Rand(-1.2,-1)/10.0
  135.         ElseIf pxpos > 0.0 And pypos < 0.0
  136.                 pxvel = Rand(-1.2,-1)/10.0
  137.                 pyvel = Rand(-1.2,-1)/10.0
  138.         ElseIf pxpos < 0.0 And pypos < 0.0
  139.                 pxvel = Rand(-1.2,-1)/10.0
  140.                 pyvel = Rand(1,1.2)/10.0
  141.         Else
  142.                 pxvel = Rand(1,1.2)/10.0
  143.                 pyvel = Rand(1,1.2)/10.0
  144.         EndIf  
  145.         pmass = 5.0
  146.         pxp = CreateSphere()
  147.         EntityColor pxp,10,10,10
  148.         EntityShininess pxp,1.0
  149. End Function
  150.  
  151. ;
  152. ; Update particle position
  153. ;
  154. Function UpdatePar(p.Par)
  155.         Local sunforce#,accelx#,accely#
  156.         Local eforce#,accelex#,acceley#
  157.         AngleSun p
  158.         If radius < 20.0 Or radius > 500.0
  159.                 pxvel = 0.0
  160.                 pyvel = 0.0
  161.                 PositionEntity pxp,0,0,200     
  162.                 Delete p
  163.         Else
  164.                 Sunforce = (G * SUNMASS * pmass) / (radius*radius)
  165.                 AngleEarth p
  166.                 If eradius < 5.0
  167.                         pxvel = 0.0
  168.                         pyvel = 0.0
  169.                         PositionEntity pxp,0,0,200
  170.                         Delete p
  171.                 Else    
  172.                         eforce = (G * EARTHMASS * pmass) / (eradius*eradius)
  173.                         accelx=-(sunforce/pmass) * Cos(angle)
  174.                         accely=-(sunforce/pmass) * Sin(angle)
  175.                         accelex =  -(eforce/pmass) * Cos(eangle)
  176.                         acceley =  -(eforce/pmass) * Sin(eangle)
  177.                         accelx = accelx + accelex
  178.                         accely = accely + acceley
  179.                         pxvel = pxvel+accelx
  180.                         pyvel = pyvel+accely
  181.                         pxpos=pxpos+pxvel
  182.                         pypos=pypos+pyvel
  183.                         PositionEntity pxp,pxpos,pypos,200
  184.                 EndIf
  185.         EndIf
  186. End Function
  187.  
  188. ;
  189. ; Get angle + radius
  190. ;
  191. Function AngleSun(p.Par)
  192.         angle = ATan2(pypos,pxpos)
  193.         radius = EntityDistance(sun,pxp)
  194. End Function
  195.  
  196. Function AngleEarth(p.Par)
  197.         eangle = ATan2(pypos-earthy,pxpos-earthx)
  198.         eradius = EntityDistance(Earth,pxp)
  199. End Function
  200.  
  201.  
  202.        
  203.  
  204.        
  205. ;
  206. ; Create some particles
  207. ;
  208. For i = 1 To Parcnt
  209.         CreatePar
  210. Next
  211.  
  212. ;
  213. ; Main loop
  214. ;
  215.  
  216. While Not KeyHit(1)
  217.  
  218. ;EntityTexture sun,suntex
  219. ;EntityTexture Earth,earthtex
  220. ;sunangle = sunangle + 0.1
  221. ;earthrot = earthrot+ 0.001
  222. ;RotateTexture suntex,sunangle
  223. ;TurnEntity Earth,0,earthrot,0
  224.  
  225. ;
  226. ; Move Earth
  227. ;
  228.         earthangle = ATan2(earthy,earthx)
  229.         earthradius = Sqr(earthx*earthx+earthy*earthy)
  230.         earthforce = (G * SUNMASS * EARTHMASS) / (earthradius*earthradius)
  231.         eaccelx=-(earthforce/EARTHMASS) * Cos(earthangle)
  232.         eaccely=-(earthforce/EARTHMASS) * Sin(earthangle)
  233.         earthvelx = earthvelx+eaccelx
  234.         earthvely = earthvely+eaccely
  235.         earthx=earthx+earthvelx
  236.         earthy=earthy+earthvely
  237.         PositionEntity Earth,earthx,earthy,200
  238.  
  239. ;
  240. ; Move particles
  241. ;
  242.  
  243. For p.Par = Each Par   
  244.         UpdatePar p
  245. Next
  246.        
  247. ;
  248. ; Move Camera
  249. ; Cursor keys + A = zoom in , Z = zoom out
  250. ; , roll left . roll right
  251. ;
  252.        
  253. If KeyDown(200) Then
  254. MoveEntity camera,0,3.6,0
  255. TurnEntity camera,1,0,0
  256. EndIf
  257.  
  258. If KeyDown(208) Then
  259. MoveEntity camera,0,-3.6,0
  260. TurnEntity camera,-1,0,0
  261. EndIf
  262.  
  263. If KeyDown(203) Then
  264. MoveEntity camera,3.6,0,0
  265. TurnEntity camera,0,1.0,0
  266. EndIf
  267.  
  268. If KeyDown(205) Then
  269. MoveEntity camera,-3.6,0,0
  270. TurnEntity camera,0,-1.0,0
  271. EndIf
  272.  
  273. If KeyDown(30) Then
  274. MoveEntity camera,0,0,1.0
  275. EndIf
  276.  
  277. If KeyDown(44) Then
  278. MoveEntity camera,0,0,-1.0
  279. EndIf
  280.  
  281. If KeyDown(51) Then
  282. TurnEntity camera,0,0,1
  283. EndIf
  284.  
  285. If KeyDown(52) Then
  286. TurnEntity camera,0,0,-1
  287. EndIf
  288.  
  289. UpdateWorld
  290. RenderWorld
  291.  
  292. Flip
  293.  
  294. Wend
  295. End


Comments :


stu(Posted 1+ years ago)

 Feel free to play around :-)


puki(Posted 1+ years ago)

 I found it rather interesting.I added:EntityColor sun,255,255,0EntityColor Earth,0,155,255just to sex it up.


puki(Posted 1+ years ago)

 Actually, I wondered why it was a bit sluggish.pxp = CreateSphere(16) is the reason why.Some people might want to change that to pxp = CreateSphere() or even just CreateCube() for a bit of turbo.


stu(Posted 1+ years ago)

 Yes, and there are ways to speed it up still further - deleting entities that are 'kicked out' of the system once they are a certain distance from the Sun - There is also some code in there which is commented out that add a rotating sun texture and an earth texture and rotating earth which makes it a bit prettier, add your own texture maps in place of the ones used and this code can be un-commented.I eventually plan on proper collisions and 'clumping' and on using some pretty lighting and particle effects


Stevie G(Posted 1+ years ago)

 Also, use entitydistance() for your radius and eradius calculations.


stu(Posted 1+ years ago)

 I've now edited the code to make it a little more refined - the sun and earth now 'swallow' particles and I've added some lighting. I've also added a new variable START which sets how far away from the Sun the particles are allowed to begin, changing this value can create some interesting patterns. I've also added Chroma's 'SpaceSphere' as a background


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal