C:\> Rostislav Persion's Projects

.:: Android Brownian Motion Simulator ::.
Android Brownian Motion Simulator






This program simulates borwnian motion on an android. It also uses the accelerometer to affect the forces on the objects.



SOFTWARE WRITTEN IN BASIC4ANDROID
   1:  Version=1.70
   2:  IconFile=
   3:  FullScreen=False
   4:  IncludeTitle=True
   5:  NumberOfModules=0
   6:  Package=com.slava.brownian
   7:  Label=Brownian Motion by Rostislav Persion
   8:  VersionCode=1
   9:  VersionString=
  10:  Orientation=portrait
  11:  CanInstallToExternalStorage=False
  12:  DoNotOverwriteManifest=False
  13:  NumberOfFiles=0
  14:  NumberOfLibraries=2
  15:  Library1=core
  16:  Library2=phone
  17:  @EndOfDesignText@
  18:  Sub Process_Globals
  19:   
  20:      
  21:      Dim Accelerometer As PhoneAccelerometer
  22:      Dim AccX As Double
  23:      Dim AccY As Double
  24:      
  25:      
  26:      Dim Timer1 As Timer
  27:      
  28:      Dim xx(100) As Double
  29:      Dim yy(100) As Double
  30:      Dim vx(100) As Double
  31:      Dim vy(100) As Double
  32:      Dim ax(100) As Double
  33:      Dim ay(100) As Double
  34:      Dim r(100) As Double
  35:      Dim dt As Double
  36:      
  37:      Dim w As Double
  38:      Dim h As Double
  39:      Dim diffx As Double
  40:      Dim diffy As Double
  41:      Dim dist1 As Double
  42:      Dim dist2 As Double
  43:      
  44:      Dim xval As Double
  45:      Dim yval As Double
  46:   
  47:      
  48:  End Sub
  49:   
  50:  Sub Globals
  51:      Dim Canvas1 As Canvas
  52:      
  53:   
  54:      
  55:  End Sub
  56:   
  57:  Sub Activity_Create(FirstTime As Boolean)
  58:      
  59:      
  60:      
  61:      canvas1.Initialize(Activity)
  62:      
  63:      Globe
  64:  End Sub
  65:   
  66:   
  67:  Sub Globe
  68:      
  69:   
  70:   
  71:   
  72:      
  73:      w = Canvas1.Bitmap.Width 
  74:      h = Canvas1.Bitmap.Height
  75:      
  76:      xx(0) = w/2
  77:      yy(0) = h/2
  78:      vx(0) = Rnd(-5,5)
  79:      vy(0) = Rnd(-5,5)
  80:      ax(0) = 0
  81:      ay(0) = 0
  82:      r(0) = 100
  83:      
  84:      For i = 1 To 99
  85:          xx(i) = Rnd(0,w)
  86:          yy(i) = Rnd(0,h)
  87:          vx(i) = Rnd(-50,50)    
  88:          vy(i) = Rnd(-50,50)
  89:          ax(0) = 0
  90:          ay(0) = 0
  91:          r(i) = Rnd(10,15)
  92:      Next 
  93:      
  94:      dt = .1
  95:      
  96:      Accelerometer.StartListening("Accelerometer") 
  97:        
  98:        Timer1.Initialize("MainEvent", 10)    
  99:      Timer1.Enabled = True
 100:       
 101:    
 102:  End Sub
 103:   
 104:  Sub MainEvent_Tick
 105:          
 106:          For i = 0 To 99
 107:          
 108:              For j = 0 To 99
 109:                  ax(j) = 0.0;
 110:                  ay(j) = 0.0;
 111:              Next
 112:                  
 113:              For j = 0 To 99
 114:              
 115:                  If i <> j Then
 116:                      diffx = xx(i) - xx(j)
 117:                      diffy = yy(i) - yy(j)
 118:                      dist1 = Sqrt(Power(diffx,2)+Power(diffy,2))
 119:                      dist2 = r(i) + r(j)
 120:                      
 121:                      If dist1 < dist2 Then
 122:                          deltaDistance = (dist2 - dist1)
 123:                      
 124:                          xval = (((deltaDistance * 10.0) / dist2) * diffx)
 125:                          yval = (((deltaDistance * 10.0) / dist2) * diffy)
 126:                          
 127:                          ax(i) = ax(i) + xval
 128:                          ay(i) = ay(i) + yval
 129:   
 130:                      End If
 131:                  
 132:                  End If
 133:              Next
 134:          
 135:              vx(i) = (vx(i) + (ax(i) * dt)) * .99 + AccX
 136:              vy(i) = (vy(i) + (ay(i) * dt)) * .99 + AccY
 137:              xx(i) = xx(i) + (vx(i) * dt)
 138:              yy(i) = yy(i) + (vy(i) * dt)
 139:           
 140:              If xx(i)-r(i) < 0 Then
 141:                  vx(i) = -vx(i)
 142:                  xx(i) = 0 + r(i)
 143:              End If
 144:              If yy(i)-r(i) < 0 Then
 145:                  vy(i) = -vy(i)
 146:                  yy(i) = 0 + r(i)
 147:              End If
 148:              If xx(i)+r(i) > w Then
 149:                  vx(i) = -vx(i)
 150:                  xx(i) = w - r(i)
 151:              End If
 152:              If yy(i)+r(i) > h Then
 153:                  vy(i) = -vy(i)
 154:                  yy(i) = h - r(i)
 155:              End If
 156:              
 157:              
 158:          Next 
 159:          
 160:          Canvas1.DrawColor(Colors.Black)
 161:          
 162:          Canvas1.DrawCircle(xx(0),yy(0),r(0),Colors.Red,True,5)
 163:          For i = 1 To 99
 164:              Canvas1.DrawCircle(xx(i),yy(i),r(i),Colors.Green,True,5)
 165:          Next 
 166:              Activity.Invalidate
 167:          DoEvents 
 168:   
 169:  End Sub
 170:  Sub Activity_Touch (Action As Int, X As Float, Y As Float)
 171:      xx(0) = x
 172:      yy(0) = y
 173:      vx(0) = 0
 174:      vy(0) = 0
 175:      
 176:  End Sub
 177:   
 178:  Sub Activity_Resume
 179:      Accelerometer.StartListening("Accelerometer")
 180:      Timer1.Enabled = True
 181:  End Sub
 182:   
 183:  Sub Activity_Pause (UserClosed As Boolean)
 184:      Accelerometer.StopListening
 185:      Timer1.Enabled = False
 186:  End Sub
 187:   
 188:   
 189:   
 190:  Sub Accelerometer_AccelerometerChanged (X As Float, Y As Float, Z As Float)
 191:      
 192:      
 193:      AccX = -(x/10) * 10
 194:      Accy = (y/10) * 10
 195:   
 196:      
 197:  End Sub