3 #include "../graphics/const.h"
8 World::World(Vector<int> size)
10 , count(size.x * size.y)
11 , masses(count, 1000000000.0f)
12 , colors(count, Color(0x7F, 0x7F, 0x7F)) {
17 void World::Update(float dt) {
18 for (Entity &e : entities) {
19 e.acc = ForceAt(e.pos, e.mass) / e.mass;
25 Entity &World::AddEntity(const Entity &e) {
26 entities.emplace_back(e);
27 return entities.back();
31 Vector<float> World::ForceAt(Vector<float> p1, float m1) const {
32 Vector<float> force(0, 0);
34 for (int i = 0; i < count; ++i) {
35 const Vector<float> p2(i % size.y, i / size.y);
36 if (p1 == p2) continue;
38 const Vector<float> diff(p2 - p1);
39 const Vector<float> dir(Norm(diff));
41 const float m2 = masses[i];
42 const float r2 = Dot(diff, diff);
44 const float mag = G * ((m1 * m2) / r2) * 2; // double because m2 is our reference frame