sync on
randomize timer()
autocam off
backdrop on
color backdrop 0
 
maxtriangles=1024
atriangles=0
boxwidth=100
boxheight=100
 
dim trianglex1#(maxtriangles)
dim triangley1#(maxtriangles)
dim trianglex2#(maxtriangles)
dim triangley2#(maxtriangles)
dim trianglex3#(maxtriangles)
dim triangley3#(maxtriangles)
 
make shatter glass(maxtriangles,10,boxwidth,boxheight)
 
atriangles=maxtriangles
 
for i=1 to atriangles
   x1#=trianglex1#(i) : y1#=triangley1#(i)
   x2#=trianglex2#(i) : y2#=triangley2#(i)
   x3#=trianglex3#(i) : y3#=triangley3#(i)
   make object triangle i+10,x1#,y1#,0,x2#,y2#,0,x3#,y3#,0
   position object i+10,0,0,100
   if x1#=0 and y1#=0 and x2#=0 and y2#=0 and x3#=0 and y3#=0 then end
   color object i+10,rgb(150,150,255)
   ghost object on i+10
next i
 
dim partvelx#(maxtriangles)
dim partvely#(maxtriangles)
dim partvelz#(maxtriangles)
 
position camera boxwidth/2,boxheight/2,0
 
sync rate 40
 
dcamroty#=0
acamroty#=0
 
do
 
   if spacekey()=1 and shattering=0
      Shatter Glass(maxtriangles)
      shattering=1
      shattertime=70
   endif
 
   if shattering=1
      gosub HandleShatter
   endif
 
   if inkey$()="e"
      for i=1 to atriangles
         delete object i+10
      next i
      end
   endif
 
   sync
 
loop
 
end
 
HandleShatter:
 
   for i=1 to maxtriangles
      partvelx#(i)=partvelx#(i)*0.95
      partvely#(i)=partvely#(i)*0.95
      partvelz#(i)=partvelz#(i)*0.95
      partvely#(i)=partvely#(i)-0.5
      x#=object position x(i+10)
      y#=object position y(i+10)
      z#=object position z(i+10)
      x#=x#+partvelx#(i)
      y#=y#+partvely#(i)
      z#=z#+partvelz#(i)
      if y#+triangley1#(i)<=0 or y#+triangley2#(i)<=0 or y#+triangley3#(i)<=0
         lowesty#=triangley1#(i)
         if triangley2#(i)<lowesty# then lowesty#=triangley2#(i)
         if triangley3#(i)<lowesty# then lowesty#=triangley3#(i)
         y#=0-lowesty#
         partvely#(i)=partvely#(i)*-0.6
         partvelx#(i)=partvelx#(i)*0.80
         partvelz#(i)=partvelz#(i)*0.80
      endif
      position object i+10,x#,y#,z#
   next i
   shattertime=shattertime-1
   if shattertime<=0
      shattering=0
      for i=1 to maxtriangles
         position object i+10,0,0,100
      next i
   endif
 
return
 
function make shatter glass(maxtriangles,divisions,boxwidth,boxheight)
 
   trianglex1#(1)=0
   triangley1#(1)=0
   trianglex2#(1)=boxwidth
   triangley2#(1)=0
   trianglex3#(1)=0
   triangley3#(1)=boxheight
 
   trianglex1#(2)=boxwidth
   triangley1#(2)=0
   trianglex2#(2)=boxwidth
   triangley2#(2)=boxheight
   trianglex3#(2)=0
   triangley3#(2)=boxheight
 
   atriangles=2
   if 2^divisions>maxtriangles
      repeat
         divisions=divisions-1
      until 2^divisions<=maxtriangles
   endif
 
   for j=1 to divisions-1
      tempatri=atriangles
      for i=1 to atriangles
         tempatri=tempatri+1
         x1#=trianglex1#(i) : y1#=triangley1#(i)
         x2#=trianglex2#(i) : y2#=triangley2#(i)
         x3#=trianglex3#(i) : y3#=triangley3#(i)
         trianglex1#(tempatri)=x1#
         triangley1#(tempatri)=y1#
         trianglex2#(tempatri)=x2#
         triangley2#(tempatri)=y2#
         trianglex3#(tempatri)=x3#
         triangley3#(tempatri)=y3#
         distances2#=((x2#-x3#)^2+(y2#-y3#)^2)
         distances3#=((x1#-x3#)^2+(y1#-y3#)^2)
         distances1#=((x2#-x1#)^2+(y2#-y1#)^2)
         totaldis#=distances1#+distances2#+distances3#
         mindis#=totaldis#/4.2
         rightprop=0
         rem start
         repeat
            corner=rnd(2)+1
            if corner=1 and distances2#>mindis# then rightprop=1
            if corner=2 and distances3#>mindis# then rightprop=1
            if corner=3 and distances1#>mindis# then rightprop=1
         until rightprop=1
         remend
         `corner=2
         if corner=1    : rem side=2
            inbetweenx#=(x2#+x3#)/2
            inbetweeny#=(y2#+y3#)/2
            trianglex2#(i)=inbetweenx#
            triangley2#(i)=inbetweeny#
            trianglex3#(tempatri)=inbetweenx#
            triangley3#(tempatri)=inbetweeny#
         endif
         if corner=2    : rem side=3
            inbetweenx#=(x1#+x3#)/2
            inbetweeny#=(y1#+y3#)/2
            trianglex3#(i)=inbetweenx#
            triangley3#(i)=inbetweeny#
            trianglex1#(tempatri)=inbetweenx#
            triangley1#(tempatri)=inbetweeny#
         endif
         if corner=3    : rem side=1
            inbetweenx#=(x2#+x1#)/2
            inbetweeny#=(y2#+y1#)/2
            trianglex1#(i)=inbetweenx#
            triangley1#(i)=inbetweeny#
            trianglex2#(tempatri)=inbetweenx#
            triangley2#(tempatri)=inbetweeny#
         endif
      next i
      atriangles=tempatri
   next j
 
endfunction
 
function Shatter Glass(maxtriangles)
 
   radius#=50
   radiuspow2#=radius#*radius#
   cx#=50
   cy#=50
   for i=1 to maxtriangles
      `partvelx#(i)=rnd(40)/10-2
      partvelx#(i)=(trianglex1#(i)-cx#)/50
      partvely#(i)=rnd(60)/10-2
      partvelz#(i)=rnd(20)/10
      x#=trianglex1#(i)
      y#=triangley1#(i)
      diff#=radiuspow2#-((x#-cx#)^2+(y#-cy#)^2)
      if diff#>=0 then partvelz#(i)=partvelz#(i)+sqrt(diff#)/8 else diff#=0
      `partlife(i)=100
   next i
 
endfunction
 
function linetriangle(x1#,y1#,x2#,y2#,x3#,y3#)
 
   line x1,y1,x2,y2
   line x2,y2,x3,y3
   line x3,y3,x1,y1
 
endfunction