Advertisement
EArthnuker

2D Gravity Simualtor (broken)

Nov 7th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import math as m
  2. import scipy as SP
  3. import scipy.constants as const
  4. import matplotlib.pyplot as plt
  5. import pylab as PL
  6. from copy import deepcopy as DC
  7. def applyforces(mat,timestep):
  8.     G=const.G
  9.     mat2=DC(mat)
  10.     pi=m.pi
  11.     for i1 in xrange(len(mat2)):
  12.         sv1=0
  13.         sv2=0
  14.         o1=mat[i1]
  15.         x1,y1=o1[0]
  16.         m1=o1[2] #mass in kg
  17.         v1x,v1y=o1[1]
  18.         for i2 in xrange(len(mat2)):
  19.             o2=mat[i2]
  20.             x2,y2=o2[0]
  21.             m2=o2[2] #mass in kg
  22.             v2x,v2y=o2[1]
  23.             d=m.sqrt((x1-x2)**2+(y1-y2)**2) #distance in meters
  24.             if (x1,y1)!=(x2,y2) and m2:
  25.                 f=G*((m1*m2)/(d**2)) #grav. force in newton
  26.                 a1=f/m1 #acceleration in m/s^2
  27.                 a2=f/m2 #acceleration in m/s^2
  28.                 v1=a1*timestep #velocity in m/s
  29.                 v2=a2*timestep #velocity in m/s
  30.                 sv1+=v1
  31.                 sv2+=v2
  32.             ang=m.atan2(y1-y2,x1-x2) # angle between points
  33.             v1x,v1y=v1x+sv1*m.sin(ang),v1y+sv1*m.cos(ang)
  34.             v2x,v2y=v2x+sv2*m.sin(ang),v2y+sv2*m.cos(ang)
  35.             dx1=v1x*timestep
  36.             dy1=v1y*timestep
  37.             dx2=v1x*timestep
  38.             dy2=v2x*timestep
  39.             x1,y1=x1-dx1,y1-dy1 #new position
  40.             x2,y2=x2-dx2,y2-dy2 #new position
  41.             mat2[i1][0]=[x1,y1]
  42.             mat2[i2][0]=[x2,y2]
  43.             mat2[i1][1]=[v1x,v1y]
  44.             mat2[i2][1]=[v2x,v2y]
  45.             #elif m2:
  46.             #   mat2[i1][2]+=mat2[i2][2] #add mass
  47.             #   mat2[i1][1][0]+=mat2[i1][1][0] #add vx
  48.             #   mat2[i1][1][1]+=mat2[i1][1][1] #add vy
  49.             #   mat2[i2][2]=0 # remove mass
  50.     return mat2
  51. rx=10
  52. ry=10
  53. t=0
  54. timestep=100 # sec
  55. M=(rx*ry)*10
  56. objmat=[]
  57. scalef=(rx*ry)*5
  58. print "building object matrix..."
  59. for x in xrange(rx):
  60.     for y in xrange(ry):
  61.         mass=M
  62.         vx=0
  63.         vy=0
  64.         objmat.append([[x,y],[vx,vy],mass])
  65. fig=plt.figure()
  66. ax=fig.add_subplot(1,1,1)
  67. t=0
  68. while 1:
  69.     t+=1
  70.     if (t*timestep)%1==0:
  71.         ax.cla()
  72.         for obj in objmat:
  73.             #print "x",obj[0][0],"y",obj[0][1],"vx",obj[1][0],"vy",obj[1][1]
  74.             ax.plot(obj[0][0],obj[0][1],'ro',ms=obj[2]/float(scalef))
  75.         plt.savefig(str(t)+".png")
  76.     print t
  77.     print "applying forces"
  78.     objmat=applyforces(objmat,timestep)
  79. print "done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement