randomization
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 27 Oct 2020 20:21:09 +0000 (15:21 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 27 Oct 2020 20:21:09 +0000 (15:21 -0500)
src/physics.cpp
src/sim.cpp

index 9b973d5f06e6a0dd305842cdb5c3ae7281f5bb2a..a627cf8ab31bcb54863e5f19dfd47950e1e808dd 100644 (file)
@@ -8,7 +8,7 @@
 //     0 < d < 1:                      (1 / d) - 1
 //     1 <= d < distance_range + 1:    (-4 * max_force / (distance_range ^ 2)) * (d - (distance_range / 2) - 1) ^ 2 + max_force,
 //     otherwise:                      0
-//
+// }
 struct BodyRelation
 {
     f32 distance_range;
@@ -20,7 +20,9 @@ get_force_magnitude_at_distance(BodyRelation br, f32 d)
 {
     if (0 < d && d < 1)
     {
-        return (1.0f / d) - 1.0f;
+        // TEMP(Brendan)
+        persist const f32 repulsion_force = 100.0f;
+        return repulsion_force * (1 - d) / d;
     }
     
     if (1 <= d && d < br.distance_range + 1)
@@ -35,14 +37,7 @@ get_force_magnitude_at_distance(BodyRelation br, f32 d)
     return 0;
 }
 
-internal const
-BodyRelation body_relations[(i32) BodyType::Count][(i32) BodyType::Count] = {
-    //              Red             Green           Blue            White
-    /* Red */   { { 1.0f, 300.0f }, { 0.0f, 0.0f },  { 10.0f, -100.0f }, { 0.0f, 0.0f } },
-    /* Green */ { { 0.0f, 0.0f },  { 0.0f, 0.0f },  { 0.0f, 0.0f },    { 0.0f, 0.0f } },
-    /* Blue */  { { 4.0f, 200.0f }, { 0.0f, 0.0f },  { 3.0f, -100.0f },   { 0.0f, 0.0f } },
-    /* White */ { { 0.0f, 0.0f },  { 0.0f, 0.0f },  { 0.0f, 0.0f },    { 0.0f, 0.0f } },
-};
+internal BodyRelation body_relations[(i32) BodyType::Count][(i32) BodyType::Count];
 
 internal bool
 bodies_collide(Body* b1, Body* b2)
@@ -69,10 +64,19 @@ body_can_move(Body* body, const Array<Body> other_bodies, V2f d)
     return true;
 }
 
-// TODO(Brendan): REMOVE THIS
-internal Array<Body *> other_bodies;
+// TODO(Brendan): :Parallel This is used to allow for a statically allocated array for the quad tree results. This will need to be a thread local variable.
+thread_local internal Array<Body *> other_bodies;
 void nocheckin_init_other_bodies()
 {
+    foreach (i, 0, (i32) BodyType::Count)
+    {
+        foreach (j, 0, (i32) BodyType::Count)
+        {
+            body_relations[i][j].max_force = randf(-200.0f, 200.0f);
+            body_relations[i][j].distance_range = randf(2.0f, 5.0f);
+        }
+    }
+    
     other_bodies.init();
     other_bodies.ensure_capacity(1024);
 }
@@ -99,7 +103,7 @@ body_accumulate_move(Body* body, QuadTree<Body>* qt_bodies, f64 dt)
         force += norm_dir * force_mag;
     }
     
-    force += body->vel * -5.f;
+    force += body->vel * -6.f;
     
     body->acc = force * (1.0f / body->mass);
     body->vel += body->acc * dt;
index 878b10904cfe34f8c6d90a9f6c5f75c18e896ff7..8e53d2626e03cb30c19318533d9f693725854edb 100644 (file)
@@ -27,7 +27,7 @@
 #define WINDOW_TITLE           "N-Body Simulation"
 
 // TODO(Brendan): Remove this
-#define PARTICLE_COUNT 512
+#define PARTICLE_COUNT 1500
 
 internal void
 glfw_key_handler(GLFWwindow* window, i32 key, i32 scancode, i32 action, i32 mods)
@@ -209,8 +209,8 @@ sim_state_init(SimState* state)
         Body tmp_body;
         tmp_body.pos = V2f{ randf(0, 1200), randf(0, 1200) };
         tmp_body.vel = V2f{ 0.0f, 0.0f };
-        tmp_body.mass = randf(5.0f, 7.0f);
-        tmp_body.body_type = static_cast<BodyType> ((rand() % 2) * 2);
+        tmp_body.mass = randf(3.0f, 6.0f);
+        tmp_body.body_type = static_cast<BodyType> ((rand() % 4));
         state->bodies.push(tmp_body);
     }