randomize timer()
ink rgb(0,255,0),rgb(0,0,0)
 
`number of circles to be created
numberofcircles=20
`if 2 circles are overlapped, overlapped=1, if not, overlapped=0 (used later in program)
overlapped=0
`create some arrays. x position, y position, radius, x and y velocity respectively.
dim x(numberofcircles) as float
dim y(numberofcircles) as float
dim r(numberofcircles) as integer
dim xvelocity(numberofcircles) as float
dim yvelocity(numberofcircles) as float
 
`puts numbers such as radius into the arrays
for n=1 to numberofcircles
   r(n)=30
   x(n)=rnd(screen width()-2*r(n)-10)+r(n)+5
   y(n)=rnd(screen height()-2*r(n)-10)+r(n)+5
   xvelocity(n)=rnd(1)-0.5
   yvelocity(n)=rnd(1)-0.5
next n
 
`this section of the code calculates overlapped circles and fixes the problem.
do
   overlapped=0
   for n=1 to numberofcircles-1
   for m=n+1 to numberofcircles
         if sqrt((x(m)-x(n))^2+(y(m)-y(n))^2)<=r(m)+r(n)
            overlapped=1
            x(n)=rnd(screen width()-2*r(n)-10)+r(n)+5
            y(n)=rnd(screen height()-2*r(n)-10)+r(n)+5
         endif
      next m
   next n
   if overlapped=0 then exit
loop
 
`main loop
do
cls
print "SCREEN FPS: ";screen fps();" Hold spacebar to turn on gravity."
 
`checks for circles colliding with the edges of the screen
gosub checkboundarycollisions
`this is the part I need help with
gosub circlecollisions
`moves the circles based on their velocity
gosub movecircles
 
`**************** Make sphere 1 controled by the mouse.
   x(1) = MouseX()
   y(1) = MouseY()
`****************
`draws the circles on the screen
gosub drawcircles
 
if upkey()=1 then inc r(1)
if downkey()=1 then dec r(1)
 
loop
 
checkboundarycollisions:
for n=1 to numberofcircles
 
if x(n)>=screen width()-r(n)
xvelocity(n)=-xvelocity(n)
x(n)=screen width()-r(n)-1
endif
if x(n)<=0+r(n)
xvelocity(n)=-xvelocity(n)
x(n)=0+r(n)+1
endif
 
if y(n)>=screen height()-r(n)
yvelocity(n)=-yvelocity(n)
y(n)=screen height()-r(n)-1
endif
if y(n)<=0+r(n)
yvelocity(n)=-yvelocity(n)
y(n)=0+r(n)+1
endif
 
next n
return
 
circlecollisions:
for n=1 to numberofcircles-1
for m=n+1 to numberofcircles
`this next line checks if the circles are overlapped
   Dist# = sqrt((x(m)-x(n))^2+(y(m)-y(n))^2)
   if Dist#<=r(m)+r(n)
      ` Get the collision normal.
      Ang#   = atanfull(x(m)-x(n),y(m)-y(n))
      ` Find out how much each sphere has penetrated eachother
      Push#  = (r(m)+r(n) - Dist#) * .5
      ` Get the x/y distances me must add to overcome the intersections.
      XOver# = Sin(Ang#) * Push#
      YOver# = Cos(Ang#) * Push#
      ` position the spheres so that they are just touching.
      x(m) = x(m) + XOver#
      y(m) = y(m) + YOver#
 
      x(n) = x(n) - XOver#
      y(n) = y(n) - YOver#
      ` Add the rebounds.
      xvelocity(n) = xvelocity(n) - XOver# * 1
      yvelocity(n) = yvelocity(n) - YOver# * 1
      xvelocity(m) = xvelocity(m) + XOver# * 1
      yvelocity(m) = yvelocity(m) + YOver# * 1
   endif
next m
next n
 
movecircles:
   for n=1 to numberofcircles
      `add gravity
      if spacekey()=1 then inc yvelocity(n), .05
      `stop the circles from moving too fast
      if xvelocity(n)>5 then xvelocity(n)=5
      if yvelocity(n)>5 then xvelocity(n)=5
      `moves the circles
      inc x(n),xvelocity(n)
      inc y(n),yvelocity(n)
      `Add some friction
      xvelocity(n) = xvelocity(n) * 0.99
      yvelocity(n) = yvelocity(n) * 0.99
   next n
return
 
drawcircles:
   for n=1 to numberofcircles
      circle x(n),y(n),r(n)
   next n
return