Making Gravity do your bidding

Sign in to queue

Description

FunWithGravity7[1] The nice fellows at BoneSoft have created a neat little program at CodeProject that shows off how to do some pretty physics simulations in c#.  They talk about how they did the calculations.

Here is how he does a full calculation loop:

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;
    }
    ...
}

Just a quick glance at the code, with the Parallel Programming extensions for .Net, I bet we could make this program able to handle more.

Tag:

C4FNews

The Discussion

Add Your 2 Cents