#include "utils.h"
#include "types.h"
+internal bool
+bodies_collide(Body* b1, Body* b2)
+{
+ const f32 dist_2 = v2f_smag(b1->pos - b2->pos);
+ const f32 radii_sum_2 = (b1->mass + b2->mass) * (b1->mass + b2->mass);
+ return dist_2 < radii_sum_2;
+}
+
+// NOTE(Brendan): Leaves body unmodified, returns if the body can move along the specified vector.
+internal bool
+body_can_move(Body* body, const Array<Body> other_bodies, V2f d)
+{
+ // HACK(Brendan): There should be a way to not have to move the body to test collisions with it.
+ body->pos += d;
+ defer { body->pos -= d; };
+
+ For (other_bodies)
+ {
+ if (body == &it) continue;
+ if (bodies_collide(body, &it)) return false;
+ }
+
+ return true;
+}
+
+void
+body_calculate_move(Body* body, const Array<Body> other_bodies, f64 dt)
+{
+ body->post_update_vel = V2f { 0.0f, 0.0f, };
+
+ if (body_can_move(body, other_bodies, V2f { (f32) (body->vel.x * dt), 0.0f }))
+ body->post_update_vel.x += body->vel.x * dt;
+
+ if (body_can_move(body, other_bodies, V2f { 0.0f, (f32) (body->vel.y * dt) }))
+ body->post_update_vel.y += body->vel.y * dt;
+}
+
+void
+body_apply_move(Body* body)
+{
+ body->pos += body->post_update_vel;
+}
\ No newline at end of file
foreach (i, 0, CIRCLE_POINT_COUNT)
{
f32 t = (f32) i / (f32) CIRCLE_POINT_COUNT;
- circle_points[i].x = cos(t * 2 * PI) / 2.0f;
- circle_points[i].y = sin(t * 2 * PI) / 2.0f;
+ circle_points[i].x = cos(t * 2 * PI);
+ circle_points[i].y = sin(t * 2 * PI);
}
GLuint vertex_buffer;
{
For (state->bodies)
{
- it.pos += it.vel * dt;
+ if (rand() % 50 == 0)
+ {
+ it.vel = V2f{ randf(-50.0f, 50.0f), randf(-50.0f, 50.0f) };
+ }
+
+ body_calculate_move(&it, state->bodies, dt);
+ }
+
+ For (state->bodies)
+ {
+ body_apply_move(&it);
}
}
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);
+ tmp_body.mass = randf(5.0f, 25.0f);
state->bodies.push(tmp_body);
}