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 // returns <1.37136364,1.37136734> for 1 mass at <0,0>
33 Vector<float> World::ForceAt(Vector<float> p1, float m1) const {
34 Vector<float> force(0, 0);
36 for (int i = 0; i < count; ++i) {
37 const Vector<float> p2(i % size.y, i / size.y);
38 if (p1 == p2) continue;
40 const Vector<float> diff(p2 - p1);
41 const Vector<float> dir(Norm(diff));
43 const float m2 = masses[i];
44 const float r2 = Dot(diff, diff);
46 const float mag = G * ((m1 * m2) / r2) * 2; // double because m2 is our reference frame
55 // returns <1.37136674,1.37136662> for 1 mass at <0,0>
56 // (should be less prone to rounding loss, i.e. more accurate)
57 Vector<float> World::ForceAt(Vector<float> p1, float m1) const {
58 Vector<float> force(0, 0);
60 for (Vector<int> p2(0, 0); p2.y < size.y; ++p2.y) {
61 Vector<float> rowForce(0, 0);
62 for (p2.x = 0; p2.x < size.x; ++p2.x) {
63 const int i = Index(p2);
64 if (p1 == Vector<float>(p2)) continue;
66 const Vector<float> diff(Vector<float>(p2) - p1);
67 const Vector<float> dir(Norm(diff));
69 const float m2 = masses[i];
70 const float r2 = Dot(diff, diff);
72 const float mag = G * ((m1 * m2) / r2) * 2; // double because m2 is our reference frame
74 rowForce += dir * mag;