SImplify acceleration addition. #1
@ -88,8 +88,8 @@ void Simulator::calculate_forces() {
|
|||||||
|
|
||||||
// Calculate force magnitude using Newton's law of gravitation
|
// Calculate force magnitude using Newton's law of gravitation
|
||||||
// F = G * m1 * m2 / r^2 BUT G = 1, and we'll multiply by the opposite mass later
|
// F = G * m1 * m2 / r^2 BUT G = 1, and we'll multiply by the opposite mass later
|
||||||
// for the acceleration
|
// for the acceleration. Use r^3 to avoid normalizing vec when multiplying later
|
||||||
Decimal force_magnitude = 1 / (dist * dist);
|
Decimal force_magnitude = 1 / (dist * dist * dist);
|
||||||
|
|
||||||
// Calculate acceleration for both bodies
|
// Calculate acceleration for both bodies
|
||||||
Decimal acc_magnitude_i = force_magnitude * bodies[j].getMass();
|
Decimal acc_magnitude_i = force_magnitude * bodies[j].getMass();
|
||||||
@ -98,19 +98,13 @@ void Simulator::calculate_forces() {
|
|||||||
// Convert to vector form
|
// Convert to vector form
|
||||||
Acceleration acc_i, acc_j;
|
Acceleration acc_i, acc_j;
|
||||||
for (int k = 0; k < 3; ++k) {
|
for (int k = 0; k < 3; ++k) {
|
||||||
acc_i[k] = -vec[k] * acc_magnitude_i / dist;
|
acc_i[k] = -vec[k] * acc_magnitude_i;
|
||||||
acc_j[k] = vec[k] * acc_magnitude_j / dist;
|
acc_j[k] = vec[k] * acc_magnitude_j;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to current accelerations
|
// Add this component to the two bodies
|
||||||
Acceleration current_acc_i = bodies[i].getAcceleration();
|
bodies[i].addAcceleration(acc_i);
|
||||||
Acceleration current_acc_j = bodies[j].getAcceleration();
|
bodies[j].addAcceleration(acc_j);
|
||||||
for (int k = 0; k < 3; ++k) {
|
|
||||||
current_acc_i[k] += acc_i[k];
|
|
||||||
current_acc_j[k] += acc_j[k];
|
|
||||||
}
|
|
||||||
bodies[i].setAcceleration(current_acc_i);
|
|
||||||
bodies[j].setAcceleration(current_acc_j);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user