moving circles around
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Oct 2020 03:50:35 +0000 (22:50 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Oct 2020 03:50:35 +0000 (22:50 -0500)
include/container.h
include/vecmath.h
src/sim.cpp
src/vecmath.cpp

index fb9801e45f5ae890d1de326179d32d14a2626b72..0d292c2a351e4ad38620927428c2d35ba8ec21cc 100644 (file)
@@ -110,7 +110,7 @@ struct Array
         count -= 1;
     }
     
-    bool contains(const T& x)
+    bool contains(const T& x) const
     {
         for (u32 i = 0; i < count; i++)
         {
@@ -121,12 +121,12 @@ struct Array
     }
     
     
-    T* begin()
+    T* begin() const
     {
         return &data[0];
     }
     
-    T* end()
+    T* end() const
     {
         return &data[count];
     }
index 4449d45ae2f9bf5e7580d3faaf4cf2c547fa7a80..fd4492c2d88cba4a75cafd56324bf183072e8b47 100644 (file)
@@ -11,6 +11,11 @@ struct V2f
 V2f operator+(V2f a, V2f b);
 V2f operator-(V2f a, V2f b);
 V2f operator*(V2f a, f32 scalar);
+
+void operator+=(V2f& a, const V2f& b);
+void operator-=(V2f& a, const V2f& b);
+void operator*=(V2f& a, const f32& s);
+
 f32 v2f_dot (V2f a, V2f b);
 f32 v2f_smag(V2f a);
 f32 v2f_mag (V2f a);
index e1de3520edeeb58609f0d0d6824b283f5f6b9d85..28cae4f9b05e4bbd95846f5e54bdec58db68ee20 100644 (file)
@@ -190,26 +190,25 @@ create_circle_mesh()
 
 
 
+internal Array<Body> bodies;
 
 // NOTE(Brendan): dt is expected to be in units of "per second".
 internal void
 update(f64 dt)
 {
+    For (bodies)
+    {
+        // it.vel *= 0.99f;
+        it.pos += it.vel * dt;
+    }
 }
 
 internal GLuint body_buffer;
-internal Array<Body> bodies;
-
 internal GLuint circle_mesh;
+
 internal void
 draw()
 {
-    For(bodies)
-    {
-        it.pos = V2f{ randf(0, 800), randf(0, 800) };
-        it.vel = V2f{ 0.0f, 0.0f };
-        it.mass = randf(10.0f, 50.0f);
-    }
     
     glBindBuffer(GL_ARRAY_BUFFER, body_buffer);
     glBufferSubData(GL_ARRAY_BUFFER, 0, bodies.count * sizeof(Body), bodies.data);
@@ -219,7 +218,7 @@ draw()
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
     glBindVertexArray(circle_mesh);
-    glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, 128);
+    glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, bodies.count);
     glBindVertexArray(-1);
     
     glfwSwapBuffers(window);
@@ -269,16 +268,13 @@ main(i32 argc, char* argv[])
     GLuint ortho_mat_loc = glGetUniformLocation(program, "u_proj");
     glUniformMatrix4fv(ortho_mat_loc, 1, false, (f32 *) ortho_mat);
     
-    bodies.ensure_capacity(128);
-    
-    // NOTE(Brendan): Setting the count like this does not guarantee that the data will be initialized.
-    bodies.count = 128;
-    
-    For(bodies)
+    foreach (i, 0, 128)
     {
-        it.pos = V2f{ randf(0, 800), randf(0, 800) };
-        it.vel = V2f{ 0.0f, 0.0f };
-        it.mass = randf(10.0f, 50.0f);
+        Body tmp_body;
+        tmp_body.pos = V2f{ randf(0, 800), randf(0, 800) };
+        tmp_body.vel = V2f{ randf(-50.0f, 50.0f), randf(-50.0f, 50.0f) };
+        tmp_body.mass = randf(10.0f, 50.0f);
+        bodies.push(tmp_body);
     }
     
     {
index 2315d49e607867bf5f0aae68513c97d194d1b724..994c98d1fa871eb01445fc0716a9777043a5bc8c 100644 (file)
@@ -21,6 +21,11 @@ operator*(V2f a, f32 scalar)
     return (V2f) { .x = a.x * scalar, .y = a.y * scalar };
 }
 
+// NOTE(Brendan): I wish these could be implicitly defined since their definitions are so easy.
+void operator+=(V2f& a, const V2f& b) { a = a + b; }
+void operator-=(V2f& a, const V2f& b) { a = a - b; }
+void operator*=(V2f& a, const f32& s) { a = a * s; }
+
 f32
 v2f_dot(V2f a, V2f b)
 {