214 lines
6.0 KiB
C
214 lines
6.0 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "objects.h"
|
|
#include "vec_util.h"
|
|
#include <bits/time.h>
|
|
#include <math.h>
|
|
#include <pthread.h>
|
|
#include <raylib.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <threads.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
static const long TIMESTEP = 1000000;
|
|
static const long simx = 2;
|
|
|
|
int center_x_text(const char *text, int width, int size) {
|
|
return (width - MeasureText(text, size)) / 2;
|
|
}
|
|
|
|
static inline int center_y_text(int height, int size) {
|
|
return (height - size) / 2;
|
|
}
|
|
|
|
Vector2 body_collision(mass_body_t *a, mass_body_t *b) {
|
|
Vector2 diff = vec2_diff(&a->position, &b->position);
|
|
float len = vec2_len(diff);
|
|
if (len > a->radius + b->radius) {
|
|
return NULL_VEC2;
|
|
}
|
|
if (len == 0) {
|
|
return (Vector2){1, 0};
|
|
}
|
|
Vector2 result = vec2_normalize(diff);
|
|
return result;
|
|
}
|
|
|
|
void collide(mass_body_t *a, mass_body_t *b, const Vector2 *normal,
|
|
float resitution) {
|
|
float inverse_a = 1.0f / a->mass;
|
|
float inverse_b = 1.0f / b->mass;
|
|
float inverse_total = inverse_a + inverse_b;
|
|
float overlap =
|
|
a->radius + b->radius - vec2_len(vec2_diff(&a->position, &b->position));
|
|
Vector2 correction = vec2_scale(normal, overlap / inverse_total);
|
|
Vector2 a_move = vec2_scale(&correction, inverse_a);
|
|
Vector2 b_move = vec2_scale(&correction, inverse_b);
|
|
a->position = vec2_sum(&a->position, &a_move);
|
|
b->position = vec2_diff(&b->position, &b_move);
|
|
|
|
Vector2 relative = vec2_diff(&b->speed, &a->speed);
|
|
float v_normal = vec2_dot(&relative, normal);
|
|
if (v_normal <= 0) {
|
|
return;
|
|
}
|
|
|
|
float factor = -(1 + resitution);
|
|
float j = factor * v_normal;
|
|
|
|
j /= inverse_total;
|
|
|
|
Vector2 final = vec2_scale(normal, j);
|
|
|
|
Vector2 a_diff = vec2_scale(&final, inverse_a);
|
|
a->speed = vec2_diff(&a->speed, &a_diff);
|
|
Vector2 b_diff = vec2_scale(&final, inverse_b);
|
|
b->speed = vec2_sum(&b->speed, &b_diff);
|
|
}
|
|
|
|
void collide_rigid(mass_body_t *a, float restitution) {
|
|
float max_x = GetRenderWidth() / 2.0f - a->radius;
|
|
float max_y = GetRenderHeight() / 2.0f - a->radius;
|
|
|
|
if (a->position.x > max_x) {
|
|
a->position.x = max_x;
|
|
if (a->speed.x > 0)
|
|
a->speed.x *= -restitution;
|
|
} else if (a->position.x < -max_x) {
|
|
a->position.x = -max_x;
|
|
if (a->speed.x < 0)
|
|
a->speed.x *= -restitution;
|
|
}
|
|
|
|
if (a->position.y > max_y) {
|
|
a->position.y = max_y;
|
|
if (a->speed.y > 0)
|
|
a->speed.y *= -restitution;
|
|
} else if (a->position.y < -max_y) {
|
|
a->position.y = -max_y;
|
|
if (a->speed.y < 0)
|
|
a->speed.y *= -restitution;
|
|
}
|
|
}
|
|
|
|
bool vec2_is_null(Vector2 v) {
|
|
if (v.x == 0 && v.y == 0) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
long long timespec_diff_ns(struct timespec end, struct timespec start) {
|
|
return (end.tv_sec - start.tv_sec) * 1000000000LL +
|
|
(end.tv_nsec - start.tv_nsec);
|
|
}
|
|
|
|
void *physics(void *args) {
|
|
universe_t *uni = (universe_t *)args;
|
|
mass_body_t body_1;
|
|
mass_body_t body_2;
|
|
Vector2 collision;
|
|
Vector2 scaled_speed;
|
|
struct timespec ot, ct;
|
|
while (true) {
|
|
clock_gettime(CLOCK_MONOTONIC, &ct);
|
|
for (unsigned int i = 0; i < uni->num_bodies; i++) {
|
|
body_1 = uni->bodies[i];
|
|
scaled_speed = vec2_scale(&body_1.speed, 1.0 / simx);
|
|
body_1.position = vec2_sum(&body_1.position, &scaled_speed);
|
|
if (i < uni->num_bodies - 1) {
|
|
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
|
|
body_2 = uni->bodies[y];
|
|
collision = body_collision(&body_1, &body_2);
|
|
if (!vec2_is_null(collision)) {
|
|
collide(&body_1, &body_2, &collision, 1.0);
|
|
}
|
|
uni->bodies[y] = body_2;
|
|
}
|
|
}
|
|
collide_rigid(&body_1, 1.1);
|
|
uni->bodies[i] = body_1;
|
|
}
|
|
ot = ct;
|
|
clock_gettime(CLOCK_MONOTONIC, &ct);
|
|
long long elapsed = timespec_diff_ns(ct, ot);
|
|
long long remaining = TIMESTEP / simx - elapsed;
|
|
|
|
if (remaining > 0) {
|
|
long long sleep_time = remaining * 9 / 10;
|
|
|
|
struct timespec sleep_time_spec = {0, sleep_time};
|
|
thrd_sleep(&sleep_time_spec, NULL);
|
|
}
|
|
|
|
do {
|
|
clock_gettime(CLOCK_MONOTONIC, &ct);
|
|
} while (timespec_diff_ns(ct, ot) < TIMESTEP / simx);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
pthread_t physics_thread;
|
|
const char *welcome_text = "Welcome to Fucked Physics!";
|
|
universe_t universe = {0, NULL};
|
|
universe.bodies = malloc(1024 * sizeof(mass_body_t));
|
|
universe.num_bodies = 0;
|
|
for (int slot = -1; slot < 32 * 20 && universe.num_bodies < 1024; slot++) {
|
|
float radius = 12.0f + 8.0f / (universe.num_bodies + 1);
|
|
float x = slot < 0
|
|
? 0.0f
|
|
: -387.5f + 25.0f * (slot % 32) + (slot / 32 % 2) * 12.5f;
|
|
float y = slot < 0 ? 0.0f : -209.0f + 22.0f * (slot / 32);
|
|
if (fabsf(x) + radius > 400.0f || fabsf(y) + radius > 225.0f) {
|
|
continue;
|
|
}
|
|
|
|
bool overlaps = false;
|
|
for (unsigned int j = 0; j < universe.num_bodies; j++) {
|
|
float dx = x - universe.bodies[j].position.x;
|
|
float dy = y - universe.bodies[j].position.y;
|
|
float min_distance = radius + universe.bodies[j].radius;
|
|
if (dx * dx + dy * dy <= min_distance * min_distance) {
|
|
overlaps = true;
|
|
break;
|
|
}
|
|
}
|
|
if (overlaps) {
|
|
continue;
|
|
}
|
|
|
|
unsigned int i = universe.num_bodies++;
|
|
universe.bodies[i] = (mass_body_t){
|
|
radius,
|
|
radius,
|
|
{x, y},
|
|
{-0.1f, 0.8f - i * 0.005f},
|
|
};
|
|
}
|
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
|
InitWindow(800, 450, "Fucked Physics");
|
|
SetTargetFPS(120);
|
|
pthread_create(&physics_thread, NULL, physics, (void *)&universe);
|
|
|
|
while (!WindowShouldClose()) {
|
|
|
|
BeginDrawing();
|
|
ClearBackground(RAYWHITE);
|
|
for (unsigned int i = 0; i < universe.num_bodies; i++) {
|
|
DrawCircleV(transform_to_center(universe.bodies[i].position),
|
|
universe.bodies[i].radius,
|
|
(Color){((256 / universe.num_bodies) * i) % 256,
|
|
255 * (i % 2), 255 * (i % 2) * (i % 3), 255});
|
|
}
|
|
|
|
DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20),
|
|
center_y_text(GetRenderHeight(), 20), 20, BLACK);
|
|
EndDrawing();
|
|
}
|
|
|
|
CloseWindow();
|
|
return EXIT_SUCCESS;
|
|
}
|