small tiny changes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 27 Oct 2020 17:10:26 +0000 (12:10 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 27 Oct 2020 17:10:26 +0000 (12:10 -0500)
include/physics.h
src/physics.cpp
src/sim.cpp

index 632d658310703fe5402599cb466fc2e66fbb4e2f..18cfb56059c416746acb734c8823aa15427b6bbe 100644 (file)
@@ -27,7 +27,7 @@ struct Body
 };
 
 void nocheckin_init_other_bodies();
-void body_accumulate_move(Body* body, QuadTree<Body>& other_bodies, f64 dt);
-void body_apply_move(Body* body);
+void body_accumulate_move(Body* body, QuadTree<Body>* qt_bodies, f64 dt);
+void body_apply_move(Body* body, f64 dt);
 
 #endif //PHYSICS_H
\ No newline at end of file
index a2a43fc39c4961f16607bb947f2507b900656bbb..9b973d5f06e6a0dd305842cdb5c3ae7281f5bb2a 100644 (file)
@@ -38,9 +38,9 @@ get_force_magnitude_at_distance(BodyRelation br, f32 d)
 internal const
 BodyRelation body_relations[(i32) BodyType::Count][(i32) BodyType::Count] = {
     //              Red             Green           Blue            White
-    /* Red */   { { 10.0f, 10.0f }, { 0.0f, 0.0f },  { 10.0f, -2.0f }, { 0.0f, 0.0f } },
+    /* 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, 10.0f }, { 0.0f, 0.0f },  { 3.0f, -2.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 } },
 };
 
@@ -78,12 +78,12 @@ void nocheckin_init_other_bodies()
 }
 
 void
-body_accumulate_move(Body* body, QuadTree<Body>& qt_bodies, f64 dt)
+body_accumulate_move(Body* body, QuadTree<Body>* qt_bodies, f64 dt)
 {
     V2f force = { 0.0f, 0.0f };
     
     other_bodies.clear();
-    qt_bodies.query(AABB { body->pos.x - 300, body->pos.y - 300, 600, 600 }, &other_bodies);
+    qt_bodies->query(AABB { body->pos.x - 300, body->pos.y - 300, 600, 600 }, &other_bodies);
     
     For (other_bodies)
     {
@@ -106,7 +106,7 @@ body_accumulate_move(Body* body, QuadTree<Body>& qt_bodies, f64 dt)
 }
 
 void
-body_apply_move(Body* body)
+body_apply_move(Body* body, f64 dt)
 {
-    body->pos += body->vel;
+    body->pos += body->vel * dt;
 }
\ No newline at end of file
index 7a3abd8d646e9504df02bb21adeff19338baedd1..878b10904cfe34f8c6d90a9f6c5f75c18e896ff7 100644 (file)
@@ -26,6 +26,9 @@
 #define WINDOW_HEIGHT          900
 #define WINDOW_TITLE           "N-Body Simulation"
 
+// TODO(Brendan): Remove this
+#define PARTICLE_COUNT 512
+
 internal void
 glfw_key_handler(GLFWwindow* window, i32 key, i32 scancode, i32 action, i32 mods)
 {
@@ -199,9 +202,9 @@ sim_state_init(SimState* state)
 {
     // NOTE(Brendan): Need to initialize the array since it does not get constructed because I refuse to use the 'new' keyword. alloc<T> uses malloc under the hood and cannot initialize the result.
     state->bodies.init();
-    state->bodies.ensure_capacity(2048);
+    state->bodies.ensure_capacity(PARTICLE_COUNT);
     
-    foreach (i, 0, 2048)
+    foreach (i, 0, PARTICLE_COUNT)
     {
         Body tmp_body;
         tmp_body.pos = V2f{ randf(0, 1200), randf(0, 1200) };
@@ -211,39 +214,21 @@ sim_state_init(SimState* state)
         state->bodies.push(tmp_body);
     }
     
-    state->qt_body_allocator.init(2048);
+    state->qt_body_allocator.init(PARTICLE_COUNT);
 }
 
-internal f64 left_over_dt = 0.0;
 // NOTE(Brendan): dt is expected to be in units of "per second".
 internal void
 update(SimState* state, f64 dt)
 {
-    const f64 step = 0.01;
+    persist const f64 step = 0.01;
     
     state->qt_bodies.init(AABB { -(f32) window_width, -(f32)window_height, (f32) window_width * 2, (f32) window_height * 2 });
     state->qt_body_allocator.reset();
     For (state->bodies) state->qt_bodies.insert(&it, &state->qt_body_allocator);
     
-    For (state->bodies) body_accumulate_move(&it, state->qt_bodies, step);
-    For (state->bodies) {
-        body_apply_move(&it);
-        /*
-        if (it.pos.x < -window_width) it.pos.x = -window_width;
-        else if (it.pos.x >= window_width * 2) it.pos.x = window_width * 2;
-        
-        if (it.pos.y < -window_height) it.pos.y = -window_height;
-        else if (it.pos.y >= window_height * 2) it.pos.y = window_height * 2;
-*/
-        
-        /*
-        if (it.pos.x < 0) { it.pos.x = 0; it.vel.x = 0; }
-        else if (it.pos.x >= window_width) { it.pos.x = window_width; it.vel.x = 0; }
-        
-        if (it.pos.y < 0) { it.pos.y = 0; it.vel.y = 0; }
-        else if (it.pos.y >= window_height) { it.pos.y = window_height; it.vel.y = 0; }
-*/
-    }
+    For (state->bodies) body_accumulate_move(&it, &state->qt_bodies, step);
+    For (state->bodies) body_apply_move(&it, step);
 }
 
 // NOTE CLEANUP(Brendan): Bunch of graphics state that should go elsewhere.