TGC Codebase Backup



Strong Gravity by NewGuy

19th Jul 2006 23:05
Summary

A ring of dots are created going in all directions and are pull around by the gravity of the red "planets"



Description

A ring of dots are created going in all directions and are pull around by the gravity of the red "planets". This is party a simulation it uses the gravity equasion
G = (GravConstant*M1*M2)/D^2
to calculate how much force the planets exert, but each dot does not effect any other dot and does not get destroyed when it hits a planet (making what looks like sparks). This demonstration creates a cool effect and is commented.

If you use this code or it helps you please give credit, and feel free to leave a comment

Click the mouse to make new dots
it will restart (with new dots) if they all get destroyed



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
type DotInfo
   Xpos#
   Ypos#
   Xspeed#
   Yspeed#
   Used
endtype

type CircleInfo
   GravX#
   GravY#
   Mass#
endtype


dim Dots(360) as DotInfo
dim Circles(5) as CircleInfo

input "Speed? (1)",Speed#
input "Gravitational constant? (10)",GravConst#

sync on
sync rate 30
do

`make 360 dots all going differnt directions
for DotSlot = 0 to 360
   Dots(DotSlot).Xspeed# = NewXvalue(0,DotSlot,Speed#)
   Dots(DotSlot).Yspeed# = NewZvalue(0,DotSlot,Speed#)

   Dots(DotSlot).Xpos# = screen width()/2
   Dots(DotSlot).Ypos# = screen height()/2

   Dots(DotSlot).Used = 1
next DotSlot

`make 5 circles with random gravity/pos
for CircleSlot = 0 to 5
   Circles(CircleSlot).GravX# = rnd(screen width()/2)+screen width()/4
   Circles(CircleSlot).GravY# = rnd(Screen height()/2)+Screen height()/4
   Circles(CircleSlot).Mass# = rnd(6)+3
next CircleSlot

`set the gravitational constant
#constant GravConstant = GravConst#
cls

do
for DotSlot = 0 to 360
if Dots(DotSlot).Used = 1
inc DotsExist
   for CircleSlot = 0 to 5
      `get distance
      Dist# = sqrt((Dots(DotSlot).Xpos#-Circles(CircleSlot).GravX#)^2 + (Dots(DotSlot).Ypos#-Circles(CircleSlot).GravY#)^2)

      `Get the amount of gravity
      Gravity# = (GravConstant*1*Circles(CircleSlot).Mass#/3)/(Dist#^2)

      `use gravity as the step value and figure out how much speed needs to be added
      X# = newXvalue(0,atanfull(Dots(DotSlot).Xpos#-Circles(CircleSlot).GravX#,Dots(DotSlot).Ypos#-Circles(CircleSlot).GravY#)+180,Gravity#)
      Y# = newZvalue(0,atanfull(Dots(DotSlot).Xpos#-Circles(CircleSlot).GravX#,Dots(DotSlot).Ypos#-Circles(CircleSlot).GravY#)+180,Gravity#)

      `ajust the dots speed
      Dots(DotSlot).Xspeed# = Dots(DotSlot).Xspeed#+X#
      Dots(DotSlot).Yspeed# = Dots(DotSlot).Yspeed#+Y#
   next CircleSlot

   `rember its old position (for drawing the line)
   OldXpos# = Dots(DotSlot).Xpos#
   OldYpos# = Dots(DotSlot).Ypos#

   `move the dot
   Dots(DotSlot).Xpos# = Dots(DotSlot).Xpos# + Dots(DotSlot).Xspeed#
   Dots(DotSlot).Ypos# = Dots(DotSlot).Ypos# + Dots(DotSlot).Yspeed#

   `if they go to far away then delete them
   if Dots(DotSlot).Xpos# < -50 or Dots(DotSlot).Xpos# > screen width()+50 or Dots(DotSlot).Ypos# < -50 or Dots(DotSlot).Ypos# > screen height()+50 then Dots(DotSlot).Used = 0

   `draw the dot (and where it was) make shure it still exists
   if Dots(DotSlot).Used = 1 then line OldXpos#,OldYpos#,Dots(DotSlot).Xpos#,Dots(DotSlot).Ypos#
endif
next DotSlot

`redraw all the planets
for CircleSlot = 0 to 5
   ink rgb(255,0,0),0
   circle Circles(CircleSlot).GravX#,Circles(CircleSlot).GravY#,Circles(CircleSlot).Mass#/2
   ink rgb(255,255,255),0
next CircleSlot

`if all the dots have been destroyed then restart
if DotsExist = 0 then exit
DotsExist = 0

`if the mouse is clicked then restart
if mouseclick() = 1 then exit

`refresh the screen
sync
cls

loop
loop