TGC Codebase Backup



Hypnotic particles and gravity wells by flibX0r

6th Mar 2005 6:10
Summary

One fine evening I was rather bored, and whipped up this little effect. Don't stare at it for too long, it tends to have a hypnotic effect



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` me so bored, dunno what to do tonight

` set teh display mode
set display mode 640, 480, 32

` define teh constants
#constant TOTAL_PARTICLE_COUNT   1023
#constant GRAVITY_CONSTANT       20
#constant BOUNCEREDUCTION        0.5

sync on
sync rate 60
randomize timer()
hide mouse

type Particle
   x as float
   y as float
   vx as float
   vy as float
   mass as float
   colour
endtype

dim p(TOTAL_PARTICLE_COUNT+1) as Particle
dim c(10) as integer

c(0) = rgb(255,0,0)
c(1) = rgb(255,128,0)
c(2) = rgb(255,255,0)
c(3) = rgb(128,255,0)
c(4) = rgb(0,255,0)
c(4) = rgb(0,255,128)
c(5) = rgb(0,255,255)
c(6) = rgb(0,128,255)
c(7) = rgb(0,0,255)
c(8) = rgb(0,128,0)
c(9) = 0

`INITILISATION
`for i=0 to TOTAL_PARTICLE_COUNT
for i=0 to 31
   for j=0 to 31
      p(i+j*32).x = rnd(100) + 270.5
      p(i+j*32).y = rnd(100) + 190.5
      p(i+j*32).vx = 0
      p(i+j*32).vy = 0
      p(i+j*32).mass = 4
      p(i+j*32).colour = 0
   next j
next i

make memblock 1, 640*480*4+12
write memblock dword 1, 0, 640
write memblock dword 1, 4, 480
write memblock dword 1, 8, 32
for i=12 to (640*480*4+11) step 4
   write memblock byte 1, i, 0         :`Blue
   write memblock byte 1, i+1, 0       :`Green
   write memblock byte 1, i+2, 0       :`Red
   write memblock byte 1, i+3, 10      :`Alpha
next i
make image from memblock 1, 1

`MAIN LOOP
`This is the part to edit. basically it just applies
`forces to the particles, draws them, and repeats

do
   `cls

   `Apply forces
   if inkey$()<>" "
      FORCE_GRAV_POINT(-1, 100, 100)
      FORCE_GRAV_POINT(-1, 540, 100)
      FORCE_GRAV_POINT(-1, 100, 380)
      FORCE_GRAV_POINT(-1, 540, 380)

      `Update positions
      for i=0 to TOTAL_PARTICLE_COUNT
         p(i).x = p(i).x + p(i).vx
         p(i).y = p(i).y + p(i).vy
      next i

   endif

   `No going outside the screen
   `if one does, then its back to the start
   for i=0 to TOTAL_PARTICLE_COUNT
      if (p(i).vx*p(i).vx)+(p(i).vy*p(i).vy)>36
         p(i).x = rnd(100) + 270.5
         p(i).y = rnd(100) + 190.5
         p(i).vx = 0
         p(i).vy = 0
         p(i).colour = p(i).colour+1
         if p(i).colour=9
            p(i).colour=0
            p(i).x = rnd(100) + 270.5
            p(i).y = rnd(100) + 190.5
         endif
      endif
   next i

   `Draw the particles
   for i=0 to TOTAL_PARTICLE_COUNT
      ink c(p(i).colour), 0
      dot p(i).x, p(i).y
   next i

   `draw the image to create the trail effect
   paste image 1, 0, 0, 1

   sync

loop

`Functions

`Gravity Point
function FORCE_GRAV_POINT(particle, gx, gy)

   ink RGB(255,255,255), 0
   circle gx, gy, 10

   dist as float
   accel as float

   if particle = -1
      for i=0 to TOTAL_PARTICLE_COUNT
         dist = (gx-p(i).x)*(gx-p(i).x) + (gy-p(i).y)*(gy-p(i).y)
         accel = GRAVITY_CONSTANT*(p(i).mass)/dist
         dist = sqrt(dist)
         p(i).vx = p(i).vx + accel*(gx-p(i).x)/dist
         p(i).vy = p(i).vy + accel*(gy-p(i).y)/dist
      next i
   else
      dist = (gx-p(particle).x)*(gx-p(particle).x) + (gy-p(particle).y)*(gy-p(particle).y)
      dist = sqrt(dist)
      accel = GRAVITY_CONSTANT*(p(particle).mass)/dist
      p(particle).vx = p(particle).vx + accel*(gx-p(particle).x)/dist
      p(particle).vy = p(particle).vy + accel*(gy-p(particle).y)/dist
   endif

endfunction