From ce1a59f3d525123dc33677b31edb05d47d9a2665 Mon Sep 17 00:00:00 2001 From: Hector van der Aa Date: Tue, 29 Sep 2026 22:18:36 +0200 Subject: [PATCH] Fixed stupid shit --- main.c | 57 ++++++------------------------------------------------ vec_util.h | 7 ++++--- 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/main.c b/main.c index 6d349e9..e4d89f2 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,9 @@ #define _POSIX_C_SOURCE 200809L +#include "objects.h" #include #include #include -#include #include #include #include @@ -11,16 +11,8 @@ #include #include -static const Vector2 NULL_VEC2 = {0, 0}; static const long TIMESTEP = 1000000; -typedef struct { - float mass; - float radius; - Vector2 position; - Vector2 speed; -} mass_body_t; - int center_x_text(const char *text, int width, int size) { return (width - MeasureText(text, size)) / 2; } @@ -29,42 +21,6 @@ static inline int center_y_text(int height, int size) { return (height - size) / 2; } -Vector2 transform_to_center(Vector2 v) { - int width = GetRenderWidth(); - int height = GetRenderHeight(); - Vector2 result = {v.x + width / 2.0, v.y + height / 2.0}; - return result; -} - -Vector2 vec2_sum(const Vector2 *a, const Vector2 *b) { - Vector2 result = {a->x + b->x, a->y + b->y}; - return result; -} -Vector2 vec2_diff(const Vector2 *a, const Vector2 *b) { - Vector2 result = {a->x - b->x, a->y - b->y}; - return result; -} - -float vec2_len(Vector2 v) { return sqrtf(v.x * v.x + v.y * v.y); } - -Vector2 vec2_normalize(const Vector2 v) { - float len = vec2_len(v); - if (len == 0.0) { - return NULL_VEC2; - } - Vector2 result = {v.x / len, v.y / len}; - return result; -} - -Vector2 vec2_scale(const Vector2 *normal, const float len) { - Vector2 result = {len * normal->x, len * normal->y}; - return result; -} - -float vec2_dot(const Vector2 *a, const Vector2 *b) { - return a->x * b->x + a->y * b->y; -} - Vector2 body_collision(mass_body_t *a, mass_body_t *b) { Vector2 diff = vec2_diff(&a->position, &b->position); float len = vec2_len(diff); @@ -131,7 +87,6 @@ bool vec2_is_null(Vector2 v) { 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); @@ -153,14 +108,14 @@ void *physics(void *args) { 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); + collide(&body_1, &body_2, &collision, 1.2); } uni->bodies[y] = body_2; } } collision = edge_collision(&body_1); if (!vec2_is_null(collision)) { - collide_rigid(&body_1, &collision, 1.0); + collide_rigid(&body_1, &collision, 1.2); } uni->bodies[i] = body_1; } @@ -186,17 +141,17 @@ int main(void) { pthread_t physics_thread; const char *welcome_text = "Welcome to Fucked Physics!"; universe_t universe = {0, NULL}; - pthread_create(&physics_thread, NULL, physics, (void *)&universe); universe.bodies = malloc(1024 * sizeof(mass_body_t)); - mass_body_t body_1 = {100, 50, {-200, -100}, {0.1, 0.00000000001}}; - mass_body_t body_2 = {100, 50, {200, 0}, {0.1, 0.000000000001}}; + mass_body_t body_1 = {100, 50, {-200, -200}, {0.1, -0.1}}; + mass_body_t body_2 = {100, 50, {200, 200}, {-0.1, 0.105}}; universe.bodies[0] = body_1; universe.bodies[1] = body_2; universe.num_bodies = 2; SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(800, 450, "Fucked Physics"); SetTargetFPS(120); + pthread_create(&physics_thread, NULL, physics, (void *)&universe); while (!WindowShouldClose()) { diff --git a/vec_util.h b/vec_util.h index dc45509..69ed0b9 100644 --- a/vec_util.h +++ b/vec_util.h @@ -1,11 +1,9 @@ #ifndef VEC_UTIL_H_ #define VEC_UTIL_H_ -#include #include +#include static const Vector2 NULL_VEC2 = {0, 0}; - - Vector2 transform_to_center(Vector2 v) { int width = GetRenderWidth(); int height = GetRenderHeight(); @@ -26,6 +24,9 @@ float vec2_len(Vector2 v) { return sqrtf(v.x * v.x + v.y * v.y); } Vector2 vec2_normalize(const Vector2 v) { float len = vec2_len(v); + if (len == 0) { + return NULL_VEC2; + } Vector2 result = {v.x / len, v.y / len}; return result; }