change BodyType to an enum struct
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 26 Oct 2020 18:11:40 +0000 (13:11 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 26 Oct 2020 18:11:40 +0000 (13:11 -0500)
include/physics.h
src/physics.cpp
src/sim.cpp

index 0bf8aef7109c29c4f1df37632e252d331d9a484d..20a52b570390622d8c1cef4becfa545b7c4f3d53 100644 (file)
@@ -4,15 +4,14 @@
 #include <vecmath.h>
 #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
index 93c886d3080d45100d0949a3029da9f382901258..d334d4e4ba6434231b4ab461bd216e2ccb82c5ee 100644 (file)
@@ -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<Body> 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<i32>(body->body_type)][static_cast<i32>(body->body_type)];
+        f32 force_mag = get_force_magnitude_at_distance(br, d);
         
         force += norm_dir * force_mag;
     }
index 15083f3db29fbd5248c9d1ddd94c0fa932b782e8..283e944ce9dc8c91667b6520bfb348a886037a59 100644 (file)
@@ -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<BodyType> (rand() % 2) * 2;
+        tmp_body.body_type = static_cast<BodyType> ((rand() % 2) * 2);
         state->bodies.push(tmp_body);
     }
 }