Ejemplo n.º 1
0
void Cluster::calculateAcceleration() // Calculates forces between bodies
{
    double G = gravitationalConstant;
    // Set acceleration to zero to avoid addition from previous run
    resetAllAcceleration();
    for(int i=0; i<numberOfBodies(); i++)
    {
        CelestialBody *body1 = &bodies.at(i);
        for(int j=i+1; j<numberOfBodies(); j++)
        {
            CelestialBody *body2 = &bodies[j];
            vec3 deltaRVector = body2->position - body1->position;    // deltaRVector pointing from body1 to body2
            double dr = deltaRVector.length();
            double dr_cubed = dr*dr*dr;

            double accFactor = body2->mass/dr_cubed;
            vec3 accVector= deltaRVector*accFactor;
            body1->acceleration = body1->acceleration+accVector;     // Force on body1 points same direction as deltaRVector

            accFactor = body1->mass/dr_cubed;
            accVector= deltaRVector*accFactor;
            body2->acceleration = body2->acceleration-accVector;     // Force on body2 points opposite direction as deltaRvector
        }
        // Finally, multiplying the sum of "acceleration vectors" with G to get the proper acceleration
        body1->acceleration = body1->acceleration*G;
    }
}
Ejemplo n.º 2
0
void simulation::calcAllAcceleration(void) {
    resetAllAcceleration(); // Set all accelerations to 0;
    for(unsigned int x = 0; x < bodies.size(); x++) {
        // Evaluate bottom left of calculation matrix
        for(unsigned int y = x+1; y < bodies.size(); y++) {
#ifdef PRINTAC
            std::cerr << x << "-" << y << std::endl;
#endif
            // Same body relationships do not occur
            calcAcceleration(bodies[x], bodies[y]);
        }
    }
}