Advertisement
Grey_Hugentobler

Gravity Applet 1.2

Apr 3rd, 2011
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. //Code by Grey Hugentobler, March 31st, 2011 through April 3rd, 2011
  2.  
  3. package GravityApplet;
  4.  
  5. import java.awt.*;
  6. import java.util.*;
  7. import javax.swing.*;
  8.  
  9. public class GravityApplet extends JApplet implements Runnable
  10. {
  11. //G is the gravitational constant for the depicted universe
  12. public final float gravitationalConstant = .0001f;
  13. //t is the time since the simulation started
  14. public float time = 0;
  15. //dt is the increment of time we will advance the simulation by each step
  16. public final float deltaTime = .01f;
  17. //number of particles
  18. public int numberOfPlanets = 200;
  19. //total mass of the system
  20. public float totalMass = 0;
  21. //inverse total mass of the system
  22. public float inverseTotalMass = 0;
  23. //center of mass of the system (mechanism to check that the net momentum is in fact 0)
  24. public Vector centerOfMass = new Vector(0, 0);
  25.  
  26. //Mass stores the masses of the particles
  27. public ArrayList<Float> mass = new ArrayList<Float>();
  28. //inverseMass stores the inverse mass of each particle
  29. public ArrayList<Float> inverseMass = new ArrayList<Float>();
  30. //Position is a collection of the particles position Vectors
  31. public ArrayList<Vector> position = new ArrayList<Vector>();
  32. //Velocity is a collection of the particles velocity Vectors
  33. public ArrayList<Vector> velocity = new ArrayList<Vector>();
  34. //Acceleration is derived from NetForce using Mass.
  35. public ArrayList<Vector> acceleration = new ArrayList<Vector>();
  36. //Color is where the random colors are stored
  37. public ArrayList<Color> color = new ArrayList<Color>();
  38. //size stores the radius of each blob determined by mass
  39. public ArrayList<Integer> size = new ArrayList<Integer>();
  40.  
  41. //bufferGraphics is the Graphics buffer
  42. public Graphics bufferGraphics;
  43. //offscreen is the image being put together offscreen
  44. public Image offscreen;
  45. //dim is the current dimensions of the applet
  46. public Dimension dimensions;
  47.  
  48. //Show center or not
  49. public boolean showCenter = true;
  50. //Show time or not
  51. public boolean showTime = true;
  52.  
  53. //Random number Generator
  54. public Random randomNumberGeneratot = new Random();
  55.  
  56. // initiate Applet
  57. public void init()
  58. {
  59. //figure out how large the window is
  60. dimensions = getSize();
  61.  
  62. //standard deviation of the radius
  63. double sigmar = 1000;
  64. //standard deviation of the velocity
  65. double sigmav = 100;
  66. //maximum mass
  67. float maxMass = 10;
  68. //minimum mass
  69. float minMass = 2;
  70.  
  71. //generate our distribution of particles
  72. for(int i = 0; i < numberOfPlanets; i++)
  73. {
  74. mass.add(i, minMass + (maxMass-minMass)*randomNumberGeneratot.nextFloat());
  75. inverseMass.add(i, 1/mass.get(i));
  76. double random1=sigmar*randomNumberGeneratot.nextDouble();
  77. double random2=2*Math.PI*randomNumberGeneratot.nextDouble();
  78. double random3=sigmav*randomNumberGeneratot.nextDouble();
  79. double random4=2*Math.PI*randomNumberGeneratot.nextDouble();
  80. position.add(i, new Vector(Math.round(random1*Math.sin(random2)+0.5*dimensions.width),Math.round(random1*Math.cos(random2)+0.5*dimensions.height)));
  81. velocity.add(i, new Vector(Math.round(random3*Math.sin(random4)),Math.round(random3*Math.cos(random4))));
  82. acceleration.add(i, new Vector(0, 0));
  83. float randomRed = randomNumberGeneratot.nextFloat();
  84. float randomGreen = randomNumberGeneratot.nextFloat();
  85. float randomBlue = randomNumberGeneratot.nextFloat();
  86. color.add(i, new Color(randomRed, randomGreen, randomBlue));
  87. size.add(i, ((int) Math.sqrt(mass.get(i))));
  88. }
  89.  
  90. //correct for net momentum so that the system doesn't go off screen
  91. //create Vector representing net momentum
  92. Vector netMomentum = new Vector(0, 0);
  93.  
  94. //find total momentum and total mass
  95. for(int i = 0; i < numberOfPlanets; i++)
  96. {
  97. netMomentum = netMomentum.add(velocity.get(i).multiply(mass.get(i)));
  98. totalMass += mass.get(i);
  99. }
  100.  
  101. inverseTotalMass = 1/totalMass;
  102.  
  103. //create vector representing net velocity
  104. Vector netVelocity = netMomentum.multiply(inverseTotalMass);
  105.  
  106. //modify all the velocities to cancel the net momentum
  107. for(int i = 0; i < numberOfPlanets; i++)
  108. {
  109. velocity.set(i, velocity.get(i).subtract(netVelocity));
  110. }
  111.  
  112. //set the background to black, like space
  113. setBackground(Color.black);
  114.  
  115. //start a new thread to run the simulation
  116. Thread newThread;
  117. newThread = new Thread (this);
  118. newThread.start();
  119. }
  120.  
  121. public void paint(Graphics onscreenGraphics)
  122. {
  123. //change dimensions to the current size of the Applet
  124. dimensions = getSize();
  125.  
  126. //change the offscreen image to the dimensions of the Applet
  127. offscreen = createImage(dimensions.width,dimensions.height);
  128. bufferGraphics = offscreen.getGraphics();
  129.  
  130. // Draw all the little dots
  131. for(int i = 0; i < numberOfPlanets; i++)
  132. {
  133. bufferGraphics.setColor(color.get(i));
  134. bufferGraphics.fillOval((int) (position.get(i).x() - size.get(i)), (int) (position.get(i).y() - size.get(i)), 2*size.get(i), 2*size.get(i));
  135. }
  136.  
  137. //draw the time to the screen in a green serif bold font
  138. if(showTime)
  139. {
  140. bufferGraphics.setFont(new Font("serif", Font.BOLD, 12));
  141. bufferGraphics.setColor(Color.green);
  142. bufferGraphics.drawString(((int) time + ""), 0, 12);
  143. bufferGraphics.drawString(((int) mass.size() + ""), 0, 800);
  144. }
  145.  
  146. //draw the center of mass
  147. if(showCenter)
  148. {
  149. bufferGraphics.setColor(Color.white);
  150. bufferGraphics.fillRect((int) (centerOfMass.x() - 1), (int) (centerOfMass.y() - 1), 2, 2);
  151. }
  152.  
  153. //draw it all to the applet
  154. onscreenGraphics.drawImage(offscreen,0,0,this);
  155. }
  156.  
  157. public void run()
  158. {
  159. while(true)
  160. {
  161. //sleep the thread for dt seconds
  162. try
  163. {
  164. Thread.sleep((int) (1000*deltaTime));
  165. }
  166. catch(Exception e) {}
  167.  
  168. //create some storage variables to make the calculations
  169. //NetForce is where we store the force of all the particles on each individual particle
  170. Vector[] netForce = new Vector[numberOfPlanets];
  171. //Force is where we store the force of each particle on each the other particles
  172. Vector[][] force = new Vector[numberOfPlanets][numberOfPlanets];
  173. //radius is the Vector that will point at the next planet
  174. Vector radius;
  175. //divisor is the magnitude of radius squared
  176. float divisor;
  177. //size of the radiuses of the planets added together
  178. float addedSize;
  179. /*
  180. for(int i = 0; i < numberOfPlanets ; i++)
  181. {
  182. for(int j = i + 1; j < numberOfPlanets; j++)
  183. {
  184. radius = (position.get(j).subtract(position.get(i)));
  185. divisor = radius.dot(radius);
  186. addedSize =size.get(i) + size.get(j);
  187. if(divisor <= addedSize*addedSize);
  188. {
  189. velocity.set(i, velocity.get(i).multiply(mass.get(i)).add(velocity.get(j).multiply(mass.get(j))).multiply(1/(mass.get(i)+mass.get(j))));
  190. position.set(i, position.get(i).multiply(mass.get(i)).add(position.get(j).multiply(mass.get(j))).multiply(1/(mass.get(i)+mass.get(j))));
  191. mass.set(i, mass.get(i)+mass.get(j));
  192. inverseMass.set(i, 1/mass.get(i));
  193. int red = Math.round(0.5f*(color.get(i).getRed()+color.get(j).getRed()));
  194. int green = Math.round(0.5f*(color.get(i).getGreen()+color.get(j).getGreen()));
  195. int blue = Math.round(0.5f*(color.get(i).getBlue()+color.get(j).getBlue()));
  196. color.set(i, new Color(red, green, blue));
  197. size.set(i, ((int) Math.sqrt(mass.get(i))));
  198. mass.remove(j);
  199. inverseMass.remove(j);
  200. position.remove(j);
  201. velocity.remove(j);
  202. acceleration.remove(j);
  203. color.remove(j);
  204. size.remove(j);
  205. numberOfPlanets--;
  206. j--;
  207. }
  208. }
  209. }
  210. */
  211. //find all the forces of each planet on each other planet
  212. for(int i = 0; i < numberOfPlanets; i++)
  213. {
  214. for(int j = numberOfPlanets - 1; j >= i; j--)
  215. {
  216. radius = (position.get(i).subtract(position.get(j)));
  217. divisor = radius.dot(radius);
  218. if(i == j)
  219. {
  220. force[i][j] = new Vector(0, 0);
  221. }
  222. else
  223. {
  224. force[i][j] = radius.multiply(-gravitationalConstant*mass.get(i)*mass.get(j)/divisor);
  225. }
  226. force[j][i] = force[i][j].negative();
  227. }
  228. }
  229.  
  230. //set the net force to zero
  231. for(int i = 0; i < numberOfPlanets; i++)
  232. {
  233. netForce[i] = new Vector(0, 0);
  234. }
  235.  
  236. //sum up the forces to find the total force
  237. for(int i = 0; i < numberOfPlanets; i++)
  238. {
  239. for(int j = 0; j < numberOfPlanets; j++)
  240. {
  241. netForce[i] = netForce[i].add(force[i][j]);
  242. }
  243. }
  244.  
  245. //convert force to acceleration
  246. for(int i = 0; i < numberOfPlanets; i++)
  247. {
  248. acceleration.set(i, netForce[i].multiply(inverseMass.get(i)));
  249. }
  250.  
  251. //integrate the position and velocity
  252. for(int i = 0; i < numberOfPlanets; i++)
  253. {
  254. position.set(i, position.get(i).add(velocity.get(i).multiply(deltaTime)));
  255. velocity.set(i, velocity.get(i).add(acceleration.get(i).multiply(deltaTime)));
  256. }
  257.  
  258. //increment the time variable
  259. if(showTime)
  260. {
  261. time+=deltaTime;
  262. }
  263.  
  264. if(showCenter)
  265. {
  266. //intermediate between the properties of the particles and the center of mass
  267. Vector massPosition = new Vector(0, 0);
  268.  
  269. //add up the masses multiplied by the positions, the masses have already been added up
  270. for(int i = 0; i < numberOfPlanets; i++)
  271. {
  272. massPosition = massPosition.add(position.get(i).multiply(mass.get(i)));
  273. }
  274.  
  275. //calculate the center of mass
  276. centerOfMass = massPosition.multiply(inverseTotalMass);
  277. }
  278.  
  279. repaint();
  280. }
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement