From a20e88da5722b55016cbd77a39e9dd8dcb94bb15 Mon Sep 17 00:00:00 2001 From: tfaour Date: Mon, 2 Jun 2025 11:02:11 -0400 Subject: [PATCH] SImplify acceleration addition. --- src/simulator.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/simulator.cpp b/src/simulator.cpp index 42ccb4d..931651e 100644 --- a/src/simulator.cpp +++ b/src/simulator.cpp @@ -88,8 +88,8 @@ void Simulator::calculate_forces() { // 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 - // for the acceleration - Decimal force_magnitude = 1 / (dist * dist); + // for the acceleration. Use r^3 to avoid normalizing vec when multiplying later + Decimal force_magnitude = 1 / (dist * dist * dist); // Calculate acceleration for both bodies Decimal acc_magnitude_i = force_magnitude * bodies[j].getMass(); @@ -98,19 +98,13 @@ void Simulator::calculate_forces() { // Convert to vector form Acceleration acc_i, acc_j; for (int k = 0; k < 3; ++k) { - acc_i[k] = -vec[k] * acc_magnitude_i / dist; - acc_j[k] = vec[k] * acc_magnitude_j / dist; + acc_i[k] = -vec[k] * acc_magnitude_i; + acc_j[k] = vec[k] * acc_magnitude_j; } - // Add to current accelerations - Acceleration current_acc_i = bodies[i].getAcceleration(); - Acceleration current_acc_j = bodies[j].getAcceleration(); - 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); + // Add this component to the two bodies + bodies[i].addAcceleration(acc_i); + bodies[j].addAcceleration(acc_j); } } }