TGC Codebase Backup



Real Gravity by NewGuy

19th Jul 2006 22:39
Summary

Creates 200 planets with their own mass and velocity. Each one effects every other one and can some times orbit each other in a frictionless enviorment.



Description

Creates 200 planets with their own mass and velocity. Each one effects every other one and can some times orbit each other in a frictionless enviorment. As they collide they grow in mass (and gravitational pull). The force of inpact DOES NOT effect any velocity. Put the mouse near any dot/circle to see its mass!

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

Hold the mouse near a dot to see its mass
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
                                    
                                    `Created by NewGuy
`Creates 200 planets with their own mass and velocity.
`Each one effects every other one and can some times orbit each other
`in a frictionless enviorment. As they collide they grow in mass
`(and gravitational pull). The force of inpact DOES NOT effect any
`velocity. Put the mouse near any dot/circle to see its mass!


`---------------------------------------------------
`HOLD THE MOUSE near any of the dots to see its mass

`CLICK THE MOUSE to restart the dots

`It will randomly place dots every time they are all destroyed
`-------------------------------------------------------------

set text font "Arial"
set text size 16

set display mode 1024,768,32

type DotInfo
   Xpos#
   Ypos#
   Xspeed#
   Yspeed#
   Mass#
   Used
endtype

dim Dots(200) as DotInfo

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

sync on
sync rate 30
set text size 10

do
`make 200 dots all going differnt directions
for DotSlot = 0 to 200
   Dots(DotSlot).Xspeed# = newXvalue(0,DotSlot,Speed#)
   Dots(DotSlot).Yspeed# = newZvalue(0,DotSlot,Speed#)

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

   Dots(DotSlot).Mass# = 1

   Dots(DotSlot).Used = 1
next DotSlot

#constant GravConstant = GravConst#

do
for DotSlot = 0 to 200
if Dots(DotSlot).Used = 1
inc DotsExist
   for CircleSlot = 0 to 200
   if Dots(CircleSlot).Used = 1 and CircleSlot <> DotSlot

      `get distance
      Dist# = sqrt((Dots(DotSlot).Xpos#-Dots(CircleSlot).Xpos#)^2 + (Dots(DotSlot).Ypos#-Dots(CircleSlot).Ypos#)^2)

      `if it hits the circle then get rid of it
      if Dist# <= Dots(CircleSlot).Mass#/3 then Dots(DotSlot).Used = 0 : Dots(CircleSlot).Mass# = Dots(CircleSlot).Mass# + Dots(DotSlot).Mass#

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

      `calculate the differnce in speeds (with gravity)
      X# = newXvalue(0,atanfull(Dots(DotSlot).Xpos#-Dots(CircleSlot).Xpos#,Dots(DotSlot).Ypos#-Dots(CircleSlot).Ypos#)+180,Gravity#)
      Y# = newZvalue(0,atanfull(Dots(DotSlot).Xpos#-Dots(CircleSlot).Xpos#,Dots(DotSlot).Ypos#-Dots(CircleSlot).Ypos#)+180,Gravity#)

      Dots(DotSlot).Xspeed# = Dots(DotSlot).Xspeed#+X#
      Dots(DotSlot).Yspeed# = Dots(DotSlot).Yspeed#+Y#
   endif
   next CircleSlot

   `rember old pos and get new pos
   OldXpos# = Dots(DotSlot).Xpos#
   OldYpos# = Dots(DotSlot).Ypos#

   Dots(DotSlot).Xpos# = Dots(DotSlot).Xpos# + Dots(DotSlot).Xspeed#
   Dots(DotSlot).Ypos# = Dots(DotSlot).Ypos# + Dots(DotSlot).Yspeed#

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

     `if the dot is close to the mouse tell its mass
     Dist# = sqrt((Dots(DotSlot).Xpos#-mouseX())^2 +(Dots(DotSlot).Ypos#-MouseY())^2)
     if Dist# < 25
        line Dots(DotSlot).Xpos#,Dots(DotSlot).Ypos#,Dots(DotSlot).Xpos#-4,Dots(DotSlot).Ypos#-4

        TxtMsg# = int(Dots(DotSlot).Mass#*100)
        text Dots(DotSlot).Xpos#-12,Dots(DotSlot).Ypos#-12,str$(TxtMsg#)
     endif
   endif

   if Dots(DotSlot).Xpos# < 0 or Dots(DotSlot).Xpos# > screen width() or Dots(DotSlot).Ypos# < 0 or Dots(DotSlot).Ypos# > screen height()
      Dots(DotSlot).Used = 0
   endif
endif
next DotSlot

`if all dots have been destroyed then exit (and make new ones)
if DotsExist = 0 then exit
DotsExist = 0

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

sync
cls

loop
loop