TGC Codebase Backup



Gravitational Art by NewGuy

19th Jul 2006 22:45
Summary

Creates differnt colored lines that move around based on gravity



Description

Creates differnt colored lines that move around based on gravity. See "Real Gravity" in the code base for a more accurate demonstration of the actual gravity (this one is for looks)

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

--Mouse click to reset the lines
--When all lines hit a gravitational point they get reset



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
   Color
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 36
   Dots(DotSlot).Xspeed# = NewXvalue(0,DotSlot*10,Speed#)
   Dots(DotSlot).Yspeed# = NewZvalue(0,DotSlot*10,Speed#)

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

   Dots(DotSlot).Used = 1
   Dots(DotSlot).Color = rgb(rnd(200)+55,Rnd(200)+55,Rnd(200)+55)
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

#constant GravConstant = GravConst#
cls
do
for DotSlot = 0 to 36
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)

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

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

      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#)

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

      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

   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#

   `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

   ink Dots(DotSlot).Color,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

if DotsExist = 0 then exit
DotsExist = 0

if mouseclick() = 1 then exit

sync
`cls

loop
loop