-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimulation.cpp
More file actions
30 lines (26 loc) · 1 KB
/
Simulation.cpp
File metadata and controls
30 lines (26 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "Simulation.h"
Simulation::Simulation(float G, float softening, int substeps)
: G(G), softening(softening), substeps(substeps) {}
void Simulation::addBody(RigidBody* body) {
bodies.push_back(body);
}
void Simulation::step(float dt) {
float sub_dt = dt / substeps;
for (int s = 0; s < substeps; s++) {
for (int i = 0; i < (int)bodies.size(); i++) {
for (int j = i + 1; j < (int)bodies.size(); j++) {
vec2D diff = bodies[j]->position - bodies[i]->position;
float r2 = diff.dot(diff);
float denom = r2 + softening * softening;
float force_mag = G * bodies[i]->getMass() * bodies[j]->getMass() / denom;
vec2D force = diff.normalize() * force_mag;
bodies[i]->applyForce(force);
bodies[j]->applyForce(force * (-1.0f));
}
}
for (RigidBody* body : bodies) {
body->update(sub_dt);
body->clearForces();
}
}
}