commit a79693b3443ec022ebc0f84a1aafe2e7818a2888 Author: Hector van der Aa Date: Tue Sep 29 20:10:10 2026 +0200 Some level of fucked physics needs to be isolated to thread diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..cee399f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.20) + +project(fuckedphysics + VERSION 0.1.0 + LANGUAGES C +) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS OFF) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE) +endif() + +find_package(raylib REQUIRED) + +add_executable(${PROJECT_NAME} + main.c +) + +target_link_libraries(${PROJECT_NAME} + PRIVATE + raylib +) + +target_compile_options(${PROJECT_NAME} + PRIVATE + -Wall + -Wextra + -Wpedantic +) diff --git a/main.c b/main.c new file mode 100644 index 0000000..179694b --- /dev/null +++ b/main.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include + +static const Vector2 NULL_VEC2 = {0, 0}; + +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; +} + +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); + 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); + if (len > a->radius + b->radius) { + return NULL_VEC2; + } + Vector2 result = vec2_normalize(diff); + return result; +} + +Vector2 edge_collision(mass_body_t *b) { + float width = GetRenderWidth(); + float height = GetRenderHeight(); + Vector2 result = {0, 0}; + if (b->position.x + b->radius > width / 2) { + result.x = -1; + } else if (b->position.x - b->radius < -width / 2) { + result.x = 1; + } + + if (b->position.y + b->radius > height / 2) { + result.y = -1; + } else if (b->position.y - b->radius < -height / 2) { + result.y = 1; + } + + if (result.x == 0 && result.y == 0) { + return NULL_VEC2; + } + + return vec2_normalize(result); +} + +void collide(mass_body_t *a, mass_body_t *b, const Vector2 *normal, + float resitution) { + Vector2 relative = vec2_diff(&b->speed, &a->speed); + float v_normal = vec2_dot(&relative, normal); + + float factor = -(1 + resitution); + float j = factor * v_normal; + + j /= (1.0 / a->mass) + (1.0 / b->mass); + + Vector2 final = vec2_scale(normal, j); + + Vector2 a_diff = vec2_scale(&final, 1.0 / a->mass); + a->speed = vec2_diff(&a->speed, &a_diff); + Vector2 b_diff = vec2_scale(&final, 1.0 / b->mass); + b->speed = vec2_sum(&b->speed, &b_diff); +} + +void collide_rigid(mass_body_t *a, const Vector2 *normal, float restitution) { + + float v_normal = vec2_dot(&a->speed, normal); + + Vector2 delta_v = vec2_scale(normal, -(1.0 + restitution) * v_normal); + a->speed = vec2_sum(&a->speed, &delta_v); +} + +bool vec2_is_null(Vector2 v) { + if (v.x == 0 && v.y == 0) { + return true; + } + return false; +} + +typedef struct universe { + unsigned int num_bodies; + mass_body_t *bodies; +} universe_t; + +void *physics(void *args) {} + +int main(void) { + const char *welcome_text = "Welcome to Fucked Physics!"; + universe_t universe = {0, NULL}; + + universe.bodies = malloc(1024 * sizeof(mass_body_t)); + + mass_body_t body_1 = {100, 50, {-200, -100}, {0.5, 0.1}}; + mass_body_t body_2 = {100, 50, {200, 0}, {-0.5, 0}}; + + SetConfigFlags(FLAG_WINDOW_RESIZABLE); + InitWindow(800, 450, "Fucked Physics"); + SetTargetFPS(120); + + while (!WindowShouldClose()) { + + BeginDrawing(); + ClearBackground(RAYWHITE); + DrawCircleV(transform_to_center(body_1.position), body_1.radius, RED); + DrawCircleV(transform_to_center(body_2.position), body_2.radius, GREEN); + body_1.position = vec2_sum(&body_1.position, &body_1.speed); + body_2.position = vec2_sum(&body_2.position, &body_2.speed); + + Vector2 collision = body_collision(&body_1, &body_2); + if (!vec2_is_null(collision)) { + collide(&body_1, &body_2, &collision, 1.0); + } + + collision = edge_collision(&body_1); + if (!vec2_is_null(collision)) { + collide_rigid(&body_1, &collision, 2.0); + } + + collision = edge_collision(&body_2); + if (!vec2_is_null(collision)) { + collide_rigid(&body_2, &collision, 2.0); + } + + DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20), + center_y_text(GetRenderHeight(), 20), 20, BLACK); + EndDrawing(); + } + + CloseWindow(); + return EXIT_SUCCESS; +}