Compare commits

...
2 Commits
Author SHA1 Message Date
h3cx ff3dcfcf8e Merge branch 'main' of git.h3cx.dev:h3cx/fphysics 2026-09-30 23:47:02 +02:00
h3cx d4987568d7 Gravity something works idk 2026-09-30 23:46:14 +02:00
4 changed files with 59 additions and 37 deletions

No files matched your search

+15 -16
View File
@@ -4,23 +4,22 @@
#include <raylib.h> #include <raylib.h>
void gravity(universe_t *uni) { void gravity(universe_t *uni) {
mass_body_t body_1; const float dt = 1.0f / simx;
mass_body_t body_2; for (unsigned int i = 0; i < uni->num_bodies; i++) {
for (unsigned int i = 0; i < uni->num_bodies - 1; i++) { mass_body_t *body_1 = &uni->bodies[i];
body_1 = uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) { for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
body_2 = uni->bodies[y]; mass_body_t *body_2 = &uni->bodies[y];
float force = Vector2 separation = vec2_diff(body_2->position, body_1->position);
simx * NEWTON_G * body_1.mass * body_2.mass / float distance_squared = vec2_dot(separation, separation);
(vec2_len(vec2_diff(body_2.position, body_1.position)) * TIMESTEP); if (distance_squared == 0.0f) {
Vector2 acceleration = continue;
vec2_normalize(vec2_diff(body_2.position, body_1.position)); }
Vector2 acceleration_1 = vec2_scale(acceleration, force / body_1.mass); Vector2 direction = vec2_normalize(separation);
Vector2 acceleration_2 = vec2_scale(acceleration, -force / body_2.mass); float factor = NEWTON_G * dt / distance_squared;
body_1.speed = vec2_sum(body_1.speed, acceleration_1); body_1->speed =
body_2.speed = vec2_sum(body_1.speed, acceleration_2); vec2_sum(body_1->speed, vec2_scale(direction, factor * body_2->mass));
uni->bodies[y] = body_2; body_2->speed = vec2_sum(body_2->speed,
vec2_scale(direction, -factor * body_1->mass));
} }
uni->bodies[i] = body_1;
} }
} }
+40 -18
View File
@@ -7,7 +7,7 @@
#include "magnetism.h" #include "magnetism.h"
======= =======
#include "objects.h" #include "objects.h"
>>>>>>> aa4f0dfe52bab37ef3bf82a47c44316c74c3ccbc #include "vec_utils.h"
#include <bits/time.h> #include <bits/time.h>
#include <math.h> #include <math.h>
#include <pthread.h> #include <pthread.h>
@@ -35,9 +35,8 @@ void *physics(void *args) {
universe_t *uni = (universe_t *)args; universe_t *uni = (universe_t *)args;
struct timespec ot, ct; struct timespec ot, ct;
while (true) { while (true) {
printf("a");
clock_gettime(CLOCK_MONOTONIC, &ct); clock_gettime(CLOCK_MONOTONIC, &ct);
// gravity(uni); gravity(uni);
mechanics(uni); mechanics(uni);
magnetism(uni); magnetism(uni);
ot = ct; ot = ct;
@@ -64,10 +63,15 @@ int main(void) {
const char *welcome_text = "Welcome to Fucked Physics!"; const char *welcome_text = "Welcome to Fucked Physics!";
universe_t universe = {0, NULL}; universe_t universe = {0, NULL};
universe.bodies = malloc(1024 * sizeof(mass_body_t)); universe.bodies = malloc(1024 * sizeof(mass_body_t));
universe.num_bodies = 2; universe.num_bodies = 3;
universe.bodies[0] =
(mass_body_t){10000000000000.0f, 1000, {-0, 0}, {0, 0}, 200};
universe.bodies[1] =
(mass_body_t){100000000.0f, 0.1, {-10000.1, 0}, {0, -0.2}, 50};
universe.bodies[2] =
(mass_body_t){100000000.0f, 0.1, {10000, 0}, {0, -0.2}, 50};
universe.bodies[0] = (mass_body_t){100000000000.0f, 200, {0, 0}, {0, 0}};
universe.bodies[1] = (mass_body_t){100, 50, {500, 0}, {0, 2}};
SetConfigFlags(FLAG_WINDOW_RESIZABLE); SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(800, 450, "Fucked Physics"); InitWindow(800, 450, "Fucked Physics");
SetTargetFPS(120); SetTargetFPS(120);
@@ -80,11 +84,17 @@ int main(void) {
key_pressed = GetKeyPressed(); key_pressed = GetKeyPressed();
switch (key_pressed) { switch (key_pressed) {
case KEY_I: case KEY_I:
v_scale += 0.1; if (v_scale >= 0.1) {
v_scale += 0.1;
} else {
v_scale += 0.01;
}
break; break;
case KEY_O: case KEY_O:
if (v_scale > 0.1) { if (v_scale > 0.1) {
v_scale -= 0.1; v_scale -= 0.1;
} else if (v_scale > 0.01) {
v_scale -= 0.01;
} }
break; break;
case KEY_LEFT: case KEY_LEFT:
@@ -102,23 +112,35 @@ int main(void) {
} }
} while (key_pressed != 0); } while (key_pressed != 0);
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) ||
IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) {
Vector2 mouse_pos = vec2_scale(
transform_to_center(vec2_scale(GetMousePosition(), -1)), -1);
mouse_pos = vec2_scale(mouse_pos, 1 / v_scale);
universe.bodies[universe.num_bodies] =
(mass_body_t){100000.0f, 0.1, mouse_pos, {0, 0}, 10};
universe.num_bodies++;
}
if (canvas_offset.x + GetRenderWidth() * 0.5 / v_scale > if (canvas_offset.x + GetRenderWidth() * 0.5 / v_scale >
GetRenderWidth() * 5) { GetRenderWidth() * 50) {
canvas_offset.x = canvas_offset.x =
-GetRenderWidth() * 0.5 / v_scale + GetRenderWidth() * 5; -GetRenderWidth() * 0.5 / v_scale + GetRenderWidth() * 50;
} else if (canvas_offset.x - GetRenderWidth() * 0.5 / v_scale < } else if (canvas_offset.x - GetRenderWidth() * 0.5 / v_scale <
-GetRenderWidth() * 5) { -GetRenderWidth() * 50) {
canvas_offset.x = GetRenderWidth() * 0.5 / v_scale - GetRenderWidth() * 5; canvas_offset.x =
GetRenderWidth() * 0.5 / v_scale - GetRenderWidth() * 50;
} }
if (canvas_offset.y + GetRenderHeight() * 0.5 / v_scale > if (canvas_offset.y + GetRenderHeight() * 0.5 / v_scale >
GetRenderHeight() * 5) { GetRenderHeight() * 50) {
canvas_offset.y = canvas_offset.y =
-GetRenderHeight() * 0.5 / v_scale + GetRenderHeight() * 5; -GetRenderHeight() * 0.5 / v_scale + GetRenderHeight() * 50;
} else if (canvas_offset.y - GetRenderHeight() * 0.5 / v_scale < } else if (canvas_offset.y - GetRenderHeight() * 0.5 / v_scale <
-GetRenderHeight() * 5) { -GetRenderHeight() * 50) {
canvas_offset.y = canvas_offset.y =
GetRenderHeight() * 0.5 / v_scale - GetRenderHeight() * 5; GetRenderHeight() * 0.5 / v_scale - GetRenderHeight() * 50;
} }
BeginDrawing(); BeginDrawing();
@@ -128,9 +150,9 @@ int main(void) {
Vector2 body_position = vec2_scale(universe.bodies[i].position, v_scale); Vector2 body_position = vec2_scale(universe.bodies[i].position, v_scale);
body_position = vec2_sum(body_position, scaled_offset); body_position = vec2_sum(body_position, scaled_offset);
DrawCircleV(transform_to_center(body_position), DrawCircleV(transform_to_center(body_position),
universe.bodies[i].radius * v_scale, universe.bodies[i].draw_radius * v_scale,
(Color){((256 / universe.num_bodies) * i) % 256, (Color){((256 / universe.num_bodies) * i) % 256 / 2,
255 * (i % 2), 255 * (i % 2) * (i % 3), 255}); 255 * (i % 2) / 2, 255 * (i % 2) * (i % 3) / 2, 255});
} }
DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20), DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20),
+3 -3
View File
@@ -48,8 +48,8 @@ void collide(mass_body_t *a, mass_body_t *b, const Vector2 normal,
} }
void collide_rigid(mass_body_t *a, float restitution) { void collide_rigid(mass_body_t *a, float restitution) {
float max_x = GetRenderWidth() * 10 / 2.0f - a->radius; float max_x = GetRenderWidth() * 100 / 2.0f - a->radius;
float max_y = GetRenderHeight() * 10 / 2.0f - a->radius; float max_y = GetRenderHeight() * 100 / 2.0f - a->radius;
if (a->position.x > max_x) { if (a->position.x > max_x) {
a->position.x = max_x; a->position.x = max_x;
@@ -86,7 +86,7 @@ void mechanics(universe_t *uni) {
body_2 = uni->bodies[y]; body_2 = uni->bodies[y];
collision = body_collision(&body_1, &body_2); collision = body_collision(&body_1, &body_2);
if (!vec2_is_null(collision)) { if (!vec2_is_null(collision)) {
collide(&body_1, &body_2, collision, 1.0); collide(&body_1, &body_2, collision, 0.99);
} }
uni->bodies[y] = body_2; uni->bodies[y] = body_2;
} }
+1
View File
@@ -7,6 +7,7 @@ typedef struct {
float radius; float radius;
Vector2 position; Vector2 position;
Vector2 speed; Vector2 speed;
float draw_radius;
float charge; float charge;
} mass_body_t; } mass_body_t;