From 0600226b92385a63841d0d5124d0800e345a0f98 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 27 Oct 2020 12:10:26 -0500 Subject: [PATCH] small tiny changes --- include/physics.h | 4 ++-- src/physics.cpp | 12 ++++++------ src/sim.cpp | 33 +++++++++------------------------ 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/include/physics.h b/include/physics.h index 632d658..18cfb56 100644 --- a/include/physics.h +++ b/include/physics.h @@ -27,7 +27,7 @@ struct Body }; void nocheckin_init_other_bodies(); -void body_accumulate_move(Body* body, QuadTree& other_bodies, f64 dt); -void body_apply_move(Body* body); +void body_accumulate_move(Body* body, QuadTree* qt_bodies, f64 dt); +void body_apply_move(Body* body, f64 dt); #endif //PHYSICS_H \ No newline at end of file diff --git a/src/physics.cpp b/src/physics.cpp index a2a43fc..9b973d5 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -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& qt_bodies, f64 dt) +body_accumulate_move(Body* body, QuadTree* 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& 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 diff --git a/src/sim.cpp b/src/sim.cpp index 7a3abd8..878b109 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -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 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. -- 2.25.1