Advertisement
Grey_Hugentobler

Gravity Applet (float)

Apr 2nd, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. //Code by Grey Hugentobler, March 31st, 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 int g = 1;
  13. //T is the time since the simulation started
  14. public float t = 0;
  15. //dT is the increment of time we will advance the simulation by each step
  16. public float dt = .02f;
  17. //number of particles
  18. public final int n = 80;
  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 center = new Vector(0, 0);
  25.  
  26. //Mass stores the masses of the particles
  27. public float[] mass = new float[n];
  28. //InverseMass stores the inverse mass of each particle
  29. public float[] inverseMass = new float[n];
  30. //Position is a collection of the particles position Vectors
  31. public Vector[] position = new Vector[n];
  32. //Velocity is a collection of the particles velocity Vectors
  33. public Vector[] velocity = new Vector[n];
  34. //Acceleration is derived from NetForce using Mass.
  35. public Vector[] acceleration = new Vector[n];
  36. //NetForce is where we store the force of all the particles on each individual particle
  37. public Vector[] netForce = new Vector[n];
  38. //Force is where we store the force of each particle on each the other particles
  39. public Vector[][] force = new Vector[n][n];
  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 dim;
  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 generator = new Random();
  55.  
  56. // initiate Applet
  57. public void init()
  58. {
  59. //figure out how large the window is
  60. dim = getSize();
  61.  
  62. //standard deviation of the velocity
  63. float sigmav = 1;
  64.  
  65. //generate our distribution of particles
  66. for(int i = 0; i < n; i++)
  67. {
  68. mass[i] = 100*(2-generator.nextFloat());
  69. inverseMass[i] = 1/mass[i];
  70. position[i] = new Vector(Math.round(dim.width*(.5*generator.nextDouble()+ .25)), Math.round(dim.height*(.5*generator.nextDouble()+ .25)));
  71. velocity[i] = new Vector(Math.round(sigmav*generator.nextGaussian()), Math.round(sigmav*generator.nextGaussian()));
  72. acceleration[i] = new Vector(0, 0);
  73. }
  74.  
  75. //correct for net momentum so that the system doesn't go off screen
  76. //create Vector representing net momentum
  77. Vector netMomentum = new Vector(0, 0);
  78.  
  79. //find total momentum and total mass
  80. for(int i = 0; i < n; i++)
  81. {
  82. netMomentum = netMomentum.add(velocity[i].multiply(mass[i]));
  83. totalMass += mass[i];
  84. }
  85.  
  86. inverseTotalMass = 1/totalMass;
  87.  
  88. //create vector representing net velocity
  89. Vector netVelocity = netMomentum.multiply(inverseTotalMass);
  90.  
  91. //modify all the velocities to cancel the net momentum
  92. for(int i = 0; i < n; i++)
  93. {
  94. velocity[i]=velocity[i].subtract(netVelocity);
  95. }
  96.  
  97. //the background is black, like space
  98. setBackground(Color.black);
  99.  
  100. //start a new thread to run the simulation
  101. Thread newThread;
  102. newThread = new Thread (this);
  103. newThread.start();
  104. }
  105.  
  106. public void paint(Graphics onscreen)
  107. {
  108. //change dim to the current size of the Applet
  109. dim = getSize();
  110. //change the offscreen image to the dimensions of the Applet
  111. offscreen = createImage(dim.width,dim.height);
  112. bufferGraphics = offscreen.getGraphics();
  113.  
  114. // Draw all the little yellow dots
  115. bufferGraphics.setColor(Color.yellow);
  116. for(int i = 0; i < n; i++)
  117. {
  118. bufferGraphics.fillRect((int) (position[i].x() - 1), (int) (position[i].y() - 1), 2, 2);
  119. }
  120.  
  121. //draw the time to the screen in a green serif font
  122. if(showTime)
  123. {
  124. bufferGraphics.setFont(new Font("serif", Font.BOLD, 12));
  125. bufferGraphics.setColor(Color.green);
  126. bufferGraphics.drawString(((int) t + ""), 0, 12);
  127. }
  128.  
  129. //draw the center of mass
  130. if(showCenter)
  131. {
  132. bufferGraphics.setColor(Color.white);
  133. bufferGraphics.fillRect((int) (center.x() - 1), (int) (center.y() - 1), 2, 2);
  134. }
  135.  
  136. //draw it to the applet
  137. onscreen.drawImage(offscreen,0,0,this);
  138. }
  139.  
  140. public void run()
  141. {
  142. while(true)
  143. {
  144. //sleep the thread for twenty milliseconds
  145. try
  146. {
  147. Thread.sleep((int) (1000*dt));
  148. }
  149. catch(Exception e) {}
  150.  
  151. //create some storage variables to make the calculations easier
  152. Vector radius;
  153. float divisor;
  154.  
  155. //find all the forces of each particle on each other particle
  156. for(int i = 0; i < n; i++)
  157. {
  158. for(int j = n - 1; j >= i; j--)
  159. {
  160. if(i == j)
  161. {
  162. force[i][j] = new Vector(0, 0);
  163. }
  164. else
  165. {
  166. radius = (position[i].subtract(position[j]));
  167. divisor = radius.dot(radius);
  168. if(divisor == 0)
  169. {
  170. force[i][j] = new Vector(0, 0);
  171. }
  172. else
  173. {
  174. force[i][j] = radius.multiply(-g*mass[i]*mass[j]/divisor);
  175. }
  176. force[j][i] = force[i][j].negative();
  177. }
  178. }
  179. }
  180.  
  181. //clear the net Force
  182. for(int i = 0; i < n; i++)
  183. {
  184. netForce[i] = new Vector(0, 0);
  185. }
  186.  
  187. //find sum up the forces to find the total force
  188. for(int i = 0; i < n; i++)
  189. {
  190. for(int j = 0; j < n; j++)
  191. {
  192. netForce[i] = netForce[i].add(force[i][j]);
  193. }
  194. }
  195.  
  196. //convert force to acceleration
  197. for(int i = 0; i < n; i++)
  198. {
  199. acceleration[i] = netForce[i].multiply(inverseMass[i]);
  200. }
  201.  
  202. //integrate the position and velocity
  203. for(int i = 0; i < n; i++)
  204. {
  205. position[i]=position[i].add(velocity[i].multiply(dt));
  206. velocity[i]=velocity[i].add(acceleration[i].multiply(dt));
  207. }
  208.  
  209. //increment the time variable
  210. if(showTime)
  211. {
  212. t+=dt;
  213. }
  214.  
  215. if(showCenter)
  216. {
  217. //intermediate between the properties of the particles and the center of mass
  218. Vector massPosition = new Vector(0, 0);
  219.  
  220. //add up the masses multiplied by the positions, the masses have already been added up
  221. for(int i = 0; i < n; i++)
  222. {
  223. massPosition = massPosition.add(position[i].multiply(mass[i]));
  224. }
  225.  
  226. //calculate the center of mass
  227. center = massPosition.multiply(inverseTotalMass);
  228. }
  229.  
  230. repaint();
  231. }
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement