From 77ccf0c488f9b151381b3fa4720b05936935a8dd Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 26 Oct 2020 13:11:40 -0500 Subject: [PATCH] change BodyType to an enum struct --- include/physics.h | 13 ++++++------- src/physics.cpp | 5 +++-- src/sim.cpp | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/physics.h b/include/physics.h index 0bf8aef..20a52b5 100644 --- a/include/physics.h +++ b/include/physics.h @@ -4,15 +4,14 @@ #include #include "container.h" -typedef u8 BodyType; -enum +enum struct BodyType : u8 { - BodyType_Red = 0, - BodyType_Green = 1, - BodyType_Blue = 2, - BodyType_White = 3, + Red = 0, + Green = 1, + Blue = 2, + White = 3, - BodyType_Count + Count }; struct Body diff --git a/src/physics.cpp b/src/physics.cpp index 93c886d..d334d4e 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -36,7 +36,7 @@ get_force_magnitude_at_distance(BodyRelation br, f32 d) } internal const -BodyRelation body_relations[BodyType_Count][BodyType_Count] = { +BodyRelation body_relations[(i32) BodyType::Count][(i32) BodyType::Count] = { // Red Green Blue White /* Red */ { { 1.0f, 2.0f }, { 0.0f, 0.0f }, { 5.0f, -2.0f }, { 0.0f, 0.0f } }, /* Green */ { { 0.0f, 0.0f }, { 0.0f, 0.0f }, { 0.0f, 0.0f }, { 0.0f, 0.0f } }, @@ -92,7 +92,8 @@ body_calculate_move(Body* body, const Array other_bodies, f64 dt) auto d = v2f_mag(norm_dir) / 25.0f; norm_dir = v2f_norm(norm_dir); - f32 force_mag = get_force_magnitude_at_distance(body_relations[body->body_type][it.body_type], d); + auto br = body_relations[static_cast(body->body_type)][static_cast(body->body_type)]; + f32 force_mag = get_force_magnitude_at_distance(br, d); force += norm_dir * force_mag; } diff --git a/src/sim.cpp b/src/sim.cpp index 15083f3..283e944 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -205,7 +205,7 @@ sim_state_init(SimState* state) tmp_body.pos = V2f{ randf(0, 800), randf(0, 800) }; tmp_body.vel = V2f{ 0.0f, 0.0f }; tmp_body.mass = randf(2.0f, 10.0f); - tmp_body.body_type = static_cast (rand() % 2) * 2; + tmp_body.body_type = static_cast ((rand() % 2) * 2); state->bodies.push(tmp_body); } } -- 2.25.1