The Fabulous Particles
The particles applet is an exercise in the use of Java threads, pieces of code which run simultaneous to each other. In this applet, each particle owns its own thread, and the program environment considers and calculates the action of each particle simultaneously (or as close to simultaneity as can be achieved on whatever hardware the applet runs.)
Each particle has its own mass and charge. The mass determines the size of a particle, and how easily it moves, while the charge determines how the particle will react to other particles. Particles with similar polarity (for example, two particles with positive charges or two particles with negative charges) will repel each other, while particles with opposite polarity will attract each other. The greater the charge, the greater the strength of the repulsion or attraction. To see these principles in action, try creating some sample particles, following the instructions below:
- The particles program is set up by default to create particles with a mass of 1000 and a charge of 1, particles I think of as "protons". Click the "Create a particle" button to make one of these protons.
- The particle will manifest itself as a sizable red dot (all positively charged particles are red) in the large rectangular window. The dot is placed at a random location in the window. You may notice some flicker -- this is due to the fact that the dot is continually considering where it should be, and redrawing itself there.
- The dot is totally motionless at the moment, since no other particles are acting upon it. Let's make another particle to affect the first. Enter "1" in the Mass field and "-1" in the Charge field, then click the "Create a particle" button.
- A small blue particle (all negatively charged particles are blue), which I call an "electron" shows itself, again placed initially at a random location in the window. The electron, however, is much more active than the proton, since its mass is far less and the opposite charge and greater mass of the proton is attracting it. The electron will circle around and bounce off the proton for a while, and may even get it moving.
- To crank up the action, create several more electrons and protons. Soon you should have all the particles in motion. Because they are encoded as threads, all the particles consider each other at the same time. Depending on the speed of your machine, you may notice a significant slowdown as you accumulate more and more particles -- each time a new particle is created, it has to take all the others into account at every moment, and vice versa. This can get to be quite a computational burden!
- Experiment with different masses and charges, and by changing the values in the slide bars. These values, along with the equations used to move the particles, are explained below the applet.
Factors and Calculations
Creating a Particle
The first thing that happens when a particle is created is that the program chooses its color: blue for negative particles and red for positive or neutral particles.
Then the particle's diameter and speed limit are calculated as follows:
Finally, the particle is placed at a random location within the graphics rectangle and given a speed of 0.
- diameter = (0.67 * the cube root of the mass + 5.0)
- speed_limit = 800.0 / (diameter2)
Calculating the Forces
Note that the Particles applet does some calculation to determine the size of the rectangular graphics area, and if the browser window is resized in such a way as to exclude a particle, that particle is randomly repositioned inside the resized box. Those calculations are not listed below.
All the particles are placed in a vector, and each thread loops through the vector determining the effect of all the other particles according to the following equations:
These equations and calculations are from a specification written by Michael Main. The implementation of the program and its interface design were done by me.
The Fabulous Particles Applet / Paul O'Brian / obrian at colorado.edu / Revised April 1999
Return to my home page
- The integer distances (in pixels) along the x and y axes between the two particles are calculated and placed into variables distx and disty:
- distx = self's x location - other's x location
- disty = self's y location - other's y location
- A double value d is calculated to determine the total distance between particles. This value is used in force calculations and is determined thus:
- d = square root of (distx2 + disty2)
- The applet calculates a charge force and avoidance force between the two particles. The charge force calculation uses a constant called force_factor, which is defined as the value of the slider bar labeled "Charge factor". This value can be between 1000 and 19500. The avoidance force calculation uses a constant called avoid_factor, which is defined as the value of the slider bar labeled "Avoidance factor". This value can be between 100000 and 499500. The calculations are as follows:
- charge_force = -1 * other particle's charge * self's charge * force_factor / (d2)
- avoid_force = -1 * avoidance_factor / (d3)
- The total force and total acceleration of the particle are calculated:
- total_force = charge_force + avoid_force
- total_accel = total_force / mass
- Next the program determines the x and y accelerations, which are used to adjust the speed of the particle along the x and y axes. This speed is held in variables specific to each particle called xspeeed & yspeed:
- accel_x = (total_accel) * (distx / d)
- accel_y = (total_accel) * (disty / d)
- xspeed = xspeed + accel_x
- yspeed = yspeed + accel_y
- Now the particle's speed is multiplied by a "dampening factor" universal to the entire system. This factor is defined as the value in the slider bar labeled "Dampening factor". This value can be between 0.50 and 1.49. Note that if this value is 1, the factor has no effect (which is the default). If it is less than one, the overall speed of the particles slows, and if it is greater than one, the overall speed of the particles continues to increase until they reach their speed limit (see below):
- xspeed = xspeed * dampening_factor
- yspeed = yspeed * dampening_factor
- Now the applet determines the particles total speed, and adjusts that speed if it exceeds the particle's speed limit (determined at the particle's creation):
- total_speed = Math.sqrt(xspeed2 + yspeed2)
- If total_speed is greater than speed_limit:
- xspeed = xspeed * (speed_limit / total_speed)
- yspeed = yspeed * (speed_limit / total_speed)
- Next, the applet casts the speeds as integers so that they can be used in adjusting the pixel positions of the particles. Then, if the particle isn't heading outside the rectangle, the applet erases the old particle, adds the integer speeds, and redraws the new particle. If the particle is heading outside the rectangle, the applet simply flips the sign of the speed (from positive to negative or vice versa), which "bounces" the particle off the boundary before doing the redrawing.