|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article is a fine example of what happens when programmers get bored. There's nothing exciting in the code, maybe a gentle introduction to GDI+. It was just a fun project to mess with, and it's addictive to play with. So I thought I might share it. I was thinking about the fact that every particle in the universe is constantly gravitationally affected by every particle in the universe, and wanted to see what it would look like modeled in software. It's been a number of years since I cracked a Physics book, so I can't guaranty my accuracy with this project... But it sure is pretty. The CodeCalculating Acceleration Due to GravityThe method I chose for calculating the gravitational effect of each particle Vector unit = p2.Location - p.Location;
Then, calculate the magnitude of the resulting vector:
float magnitude = (float)Math.Sqrt(
(unit.X * unit.X)
+ (unit.Y * unit.Y)
+ (unit.Z * unit.Z)
);
Then, you have what you need to calculate the acceleration.
float factor = (
GravitationalConstant * (
(p.Mass * p2.Mass) / (magnitude * magnitude * magnitude)
)
) / p.Mass;
The resulting vector is what's needed to alter your particle's velocity due to the gravity of particle The full calculation loop looks like this. Notice that for each particle, you calculate the gravitational effect on it by every other particle. foreach (Particle p in particles) {
...
if (particles.Count > 1) {
Vector a = new Vector();
foreach (Particle p2 in particles) {
if (object.ReferenceEquals(p, p2)) { continue; }
Vector unit = p2.Location - p.Location;
float magnitude = (float)Math.Sqrt(
(unit.X * unit.X)
+ (unit.Y * unit.Y)
+ (unit.Z * unit.Z));
float factor = (GravitationalConstant * (
(p.Mass * p2.Mass) / (magnitude * magnitude * magnitude)
)) / p.Mass;
unit *= factor;
a += unit;
}
p.Velocity += a;
}
...
}
Conservation of Momentum with Inelastic CollisionsMy collision detection is primitive. I'm only looking to see if a particle's bounding rectangle intersects with another. The Z axes was an afterthought, and in case the X and Y are intersecting, I look to see if the Z locations are with 5 of each other. I got lazy, what can I say. I wasn't that interested in the Z axes, but wanted to include it in the calculations. In any event, when two particles collide, I combine their colors and masses. I increase their size slightly. And combine their momentum to find the resulting particle's velocity.
private Particle Merge(Particle i, Particle j, Graphics g) {
...
Vector v = ((i.Velocity * i.Mass) + (j.Velocity * j.Mass));
v /= i.Mass + j.Mass;
...
}
The Demo ApplicationControlsThere are menu options to Start, Stop, and Pause the simulation. Pause has a keyboard shortcut of F5, and Start has Ctrl+E (Execute from SQL Query Analyzer :). You must start the simulation before you can add particles, and you can restart at any time to reset everything. Clicking anywhere in the picture box will create a particle at that location. The text boxes to the left under the check boxes determines the properties that the new particle will have. You can set the X, Y, and Z components of its initial velocity, its mass, and its size in pixels.
ButtonsThe three buttons under the text boxes, labeled "B", "M" & "S", set the properties to predefined values for convenience. "B" creates a massive particle, "M" creates a medium, and "S" creates a small. The "Background" button allows you to change the picture box's background color. Check Boxes
The visual representation of the velocity and acceleration vectors is scaled up quite a bit so that they are visible at the scales I was working with. With more massive examples, the acceleration vector line can be pretty long. I may look at changing it to scale variably based on masses, but probably not.
Particle ListViewFinally, the list view at the bottom of the window shows some attributes of the particles. The list view now shows all particles but only when paused. While paused, if you select particles in the list view and hit ConclusionI don't think there's much to learn from this project, other than some simple GDI+ usage, and I suppose if you're not familiar with operator overloads, you could find some interesting things in the History07/24/2008Based on the suggestions by protix & Zimriel I've updated calculations to more accurately represent orbits. I fixed the non-tracing flickering by drawing to an image and replacing the picturebox's image (poor man's double buffer). And tracing has been redone as well. Originally, I wanted to clear the image with a white color with a low alpha so that previous draws would fade. However, drawing an alpha color to the entire image takes far too long. In the end, I wound up having each particle keep track of a number of previous position, which get redrawn with decreasing alpha values. There may be a better solution, but this works for now.
|
||||||||||||||||||||||||||||||||