Fixed some things, broke some things, we now have some sort of a config idk timestep broken

This commit is contained in:
h3cx committed 2026-10-01 23:26:07 +02:00
1 parent c729ab4f90
commit 0891150f9d
5 files changed
+138 -67

No files matched your search

+81 -54
View File
@@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "consts.h"
#include "gravity.h"
#include "magnetism.h"
@@ -11,6 +12,7 @@
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
@@ -50,34 +52,48 @@ void *physics(void *args) {
}
}
int main(void) {
int main(int argc, char **argv) {
if (argc != 2) {
printf("No config provided\n");
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
printf("File not found");
return EXIT_FAILURE;
}
int cam_lock = -1;
float v_scale = 1;
Vector2 canvas_offset = {0, 0};
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 = 7;
universe.num_bodies = 0;
char line[1024];
fgets(line, 1024, file);
while (fgets(line, 1024, file)) {
printf("%s", line);
float mass, radius, x, y, vx, vy, draw_radius, charge;
char name[32];
sscanf(line, "%f, %f, {%f, %f}, {%f, %f}, %f, %f, %31s", &mass, &radius, &x,
&y, &vx, &vy, &draw_radius, &charge, name);
universe.bodies[universe.num_bodies] = (mass_body_t){
.mass = mass,
.radius = radius / EARTH_RADIUS,
.position = (Vector2){x * UA / EARTH_RADIUS, y * UA / EARTH_RADIUS},
.speed = (Vector2){vx / EARTH_RADIUS, vy / EARTH_RADIUS},
.draw_radius = draw_radius / EARTH_RADIUS,
.charge = charge};
memcpy((void *)&universe.bodies[universe.num_bodies].name, (void *)&name,
32);
universe.num_bodies++;
}
fclose(file);
universe.bodies[0] =
(mass_body_t){330432.0f, 696340 / RT, {0, 0}, {0, 0}, 600, 0, "Sun"};
universe.bodies[1] = (mass_body_t){
0.055f, 2439.7 / RT, {0.387 * UA / RT, 0}, {0, -38.86 / RT}, 80,
0, "Mercury"};
universe.bodies[2] = (mass_body_t){
0.815f, 6051.8 / RT, {0.723 * UA / RT, 0}, {0, -34.79 / RT}, 190,
0, "Venus"};
universe.bodies[3] = (mass_body_t){
1.0f, 1.0f, {1 * UA / RT, 0}, {0, -29.29 / RT}, 200, 0, "Earth"};
universe.bodies[4] = (mass_body_t){
0.107f, 3389.5 / RT, {1.524 * UA / RT, 0}, {0, -21.97 / RT}, 106,
0, "Mars"};
universe.bodies[5] = (mass_body_t){
317.83f, 69911 / RT, {5.203 * UA / RT, 0}, {0, -12.43 / RT}, 400,
0, "Jupiter"};
universe.bodies[6] = (mass_body_t){
95.159f, 58232 / RT, {9.582 * UA / RT, 0}, {0, -9.11 / RT}, 300,
0, "Saturn"};
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(800, 450, "Fucked Physics");
SetTargetFPS(120);
@@ -90,18 +106,10 @@ int main(void) {
key_pressed = GetKeyPressed();
switch (key_pressed) {
case KEY_I:
if (v_scale >= 0.1) {
v_scale += 0.1;
} else {
v_scale += 0.01;
}
v_scale *= 1.26;
break;
case KEY_O:
if (v_scale > 0.1) {
v_scale -= 0.1;
} else if (v_scale > 0.01) {
v_scale -= 0.01;
}
v_scale /= 1.26;
break;
case KEY_LEFT:
canvas_offset.x += 100;
@@ -115,6 +123,38 @@ int main(void) {
case KEY_DOWN:
canvas_offset.y -= 100;
break;
case KEY_ONE:
cam_lock = 1;
break;
case KEY_TWO:
cam_lock = 2;
break;
case KEY_THREE:
cam_lock = 3;
break;
case KEY_FOUR:
cam_lock = 4;
break;
case KEY_FIVE:
cam_lock = 5;
break;
case KEY_SIX:
cam_lock = 6;
break;
case KEY_SEVEN:
cam_lock = 7;
break;
case KEY_EIGHT:
cam_lock = 8;
break;
case KEY_NINE:
cam_lock = 9;
break;
case KEY_ZERO:
cam_lock = 0;
break;
case KEY_F:
cam_lock = -1;
}
} while (key_pressed != 0);
@@ -133,34 +173,21 @@ int main(void) {
universe.num_bodies++;
}
if (canvas_offset.x + GetRenderWidth() * 0.5 / v_scale >
GetRenderWidth() * 50) {
canvas_offset.x =
-GetRenderWidth() * 0.5 / v_scale + GetRenderWidth() * 50;
} else if (canvas_offset.x - GetRenderWidth() * 0.5 / v_scale <
-GetRenderWidth() * 50) {
canvas_offset.x =
GetRenderWidth() * 0.5 / v_scale - GetRenderWidth() * 50;
}
if (canvas_offset.y + GetRenderHeight() * 0.5 / v_scale >
GetRenderHeight() * 50) {
canvas_offset.y =
-GetRenderHeight() * 0.5 / v_scale + GetRenderHeight() * 50;
} else if (canvas_offset.y - GetRenderHeight() * 0.5 / v_scale <
-GetRenderHeight() * 50) {
canvas_offset.y =
GetRenderHeight() * 0.5 / v_scale - GetRenderHeight() * 50;
}
BeginDrawing();
ClearBackground(RAYWHITE);
for (unsigned int i = 0; i < universe.num_bodies; i++) {
Vector2 scaled_offset = vec2_scale(canvas_offset, v_scale);
Vector2 local_offset = canvas_offset;
if (cam_lock != -1) {
local_offset = vec2_scale(universe.bodies[cam_lock].position, -1);
}
Vector2 scaled_offset = vec2_scale(local_offset, v_scale);
Vector2 body_position = vec2_scale(universe.bodies[i].position, v_scale);
body_position = vec2_sum(body_position, scaled_offset);
body_position = transform_to_center(body_position);
float draw_radius = universe.bodies[i].draw_radius * v_scale;
if (draw_radius < 2) {
draw_radius = 2;
}
DrawCircleV(body_position, draw_radius,
(Color){((256 / universe.num_bodies) * i) % 256 / 2,
255 * (i % 2) / 2, 255 * (i % 2) * (i % 3) / 2, 255});
@@ -170,8 +197,8 @@ int main(void) {
(int)(body_position.y - 8), 16, BLACK);
}
DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20),
center_y_text(GetRenderHeight(), 20), 20, BLACK);
// DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20),
// center_y_text(GetRenderHeight(), 20), 20, BLACK);
EndDrawing();
}