Compare commits

...
5 Commits
9 changed files with 245 additions and 121 deletions

No files matched your search

+7 -5
View File
@@ -24,6 +24,7 @@ add_executable(${PROJECT_NAME}
mechanics.c
vec_utils.c
gravity.c
magnetism.c
)
target_link_libraries(${PROJECT_NAME}
@@ -31,9 +32,10 @@ target_link_libraries(${PROJECT_NAME}
raylib
)
target_compile_options(${PROJECT_NAME}
PRIVATE
-Wall
-Wextra
-Wpedantic
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall
-Wextra
-Wpedantic
$<$<CONFIG:Debug>:-ggdb -O0>
$<$<CONFIG:Release>:-O3>
)
+38
View File
@@ -0,0 +1,38 @@
mass(EARTH_MASS), radius(km), {x(UA), y(UA)}, {vx(km/s), vy(km/s)}, draw_radius(km), charge(C), name
330432.0, 696340.0, {0, 0}, {0, 0}, 696340, 0, Sun
0.055, 2439.7, {0.387, 0}, {0, -38.86}, 80, 0, Mercury
0.815, 6051.8, {0.723, 0}, {0, -34.79}, 190, 0, Venus
1.0, 6371.0, {1.0, 0}, {0, -29.29}, 200, 0, Earth
0.107, 3389.5, {1.524, 0}, {0, -21.97}, 106, 0, Mars
317.83, 69911.0, {5.203, 0}, {0, -12.43}, 400, 0, Jupiter
95.159, 58232.0, {9.582, 0}, {0, -9.11}, 300, 0, Saturn
14.536, 25362.0, {19.201, 0}, {0, -6.80}, 250, 0, Uranus
17.147, 24622.0, {30.069, 0}, {0, -5.43}, 250, 0, Neptune
0.012294, 1737.4, {1.002570, 0}, {0, -30.3083}, 60, 0, Moon
0.000000001775, 11.27, {1.524063, 0}, {0, -24.1070}, 20, 0, Phobos
0.000000000402, 6.20, {1.524157, 0}, {0, -23.3212}, 16, 0, Deimos
0.00000001674, 21.5, {5.203856, 0}, {0, -43.8901}, 15, 0, Metis
0.00000000335, 8.2, {5.203862, 0}, {0, -43.7679}, 12, 0, Adrastea
0.000001256, 83.5, {5.204213, 0}, {0, -38.8569}, 20, 0, Amalthea
0.000000134, 49.3, {5.204483, 0}, {0, -36.3239}, 18, 0, Thebe
0.014956, 1821.49, {5.205820, 0}, {0, -29.7605}, 65, 0, Io
0.008037, 1560.80, {5.207486, 0}, {0, -26.1695}, 60, 0, Europa
0.024813, 2631.20, {5.210155, 0}, {0, -23.3091}, 75, 0, Ganymede
0.018015, 2410.30, {5.215585, 0}, {0, -20.6330}, 70, 0, Callisto
0.000001591, 85.0, {5.279739, 0}, {0, -15.7520}, 20, 0, Himalia
0.000006346, 198.2, {9.583240, 0}, {0, -23.4089}, 24, 0, Mimas
0.000018084, 252.1, {9.583591, 0}, {0, -21.7338}, 28, 0, Enceladus
0.000103479, 531.1, {9.583970, 0}, {0, -20.4559}, 36, 0, Tethys
0.000184187, 561.4, {9.584523, 0}, {0, -19.1353}, 38, 0, Dione
0.000386792, 763.8, {9.585523, 0}, {0, -17.5935}, 44, 0, Rhea
0.0225294, 2575.0, {9.590168, 0}, {0, -14.6817}, 75, 0, Titan
0.000000938, 135.0, {9.592033, 0}, {0, -14.1371}, 20, 0, Hyperion
0.000303071, 734.5, {9.605803, 0}, {0, -12.3738}, 42, 0, Iapetus
0.000011051, 235.8, {19.201865, 0}, {0, -13.4917}, 25, 0, Miranda
0.000226047, 578.9, {19.202277, 0}, {0, -12.3074}, 38, 0, Ariel
0.000195908, 584.7, {19.202780, 0}, {0, -11.4645}, 38, 0, Umbriel
0.000589398, 788.9, {19.203914, 0}, {0, -10.4458}, 45, 0, Titania
0.000504002, 761.4, {19.204901, 0}, {0, -9.9511}, 44, 0, Oberon
0.000008372, 210.0, {30.069686, 0}, {0, -13.0522}, 25, 0, Proteus
0.00358327, 1353.4, {30.071271, 0}, {0, -1.0406}, 55, 0, Triton
0.000005023, 170.0, {30.105758, 0}, {0, -6.5434}, 23, 0, Nereid
+11 -2
View File
@@ -1,7 +1,16 @@
#ifndef CONSTS_H_
#define CONSTS_H_
#define TIMESTEP 1000000
#define simx 2
#define LOOP_TIMESTEP \
1000000LL // Wall-clock nanoseconds between updates (1 kHz).
#define PHYSICS_TIMESTEP 10.0f // Simulated seconds per update.
#define WALL_COLLISIONS 0 // Enable for bounded collision tests.
#define NEWTON_G 6.674e-11
#define EARTH_MASS 5.9722e24 // Kilograms per simulation mass unit.
#define UA 149.6E6
#define EARTH_RADIUS 6378.14 // Kilometers per simulation distance unit.
#define DISTANCE_UNIT_METERS (EARTH_RADIUS * 1000.0)
#define SIMULATION_G \
(NEWTON_G * EARTH_MASS / \
(DISTANCE_UNIT_METERS * DISTANCE_UNIT_METERS * DISTANCE_UNIT_METERS))
#endif // CONSTS_H_
+15 -16
View File
@@ -4,23 +4,22 @@
#include <raylib.h>
void gravity(universe_t *uni) {
mass_body_t body_1;
mass_body_t body_2;
for (unsigned int i = 0; i < uni->num_bodies - 1; i++) {
body_1 = uni->bodies[i];
const float dt = PHYSICS_TIMESTEP;
for (unsigned int i = 0; i < uni->num_bodies; i++) {
mass_body_t *body_1 = &uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
body_2 = uni->bodies[y];
float force =
simx * NEWTON_G * body_1.mass * body_2.mass /
(vec2_len(vec2_diff(body_2.position, body_1.position)) * TIMESTEP);
Vector2 acceleration =
vec2_normalize(vec2_diff(body_2.position, body_1.position));
Vector2 acceleration_1 = vec2_scale(acceleration, force / body_1.mass);
Vector2 acceleration_2 = vec2_scale(acceleration, -force / body_2.mass);
body_1.speed = vec2_sum(body_1.speed, acceleration_1);
body_2.speed = vec2_sum(body_1.speed, acceleration_2);
uni->bodies[y] = body_2;
mass_body_t *body_2 = &uni->bodies[y];
Vector2 separation = vec2_diff(body_2->position, body_1->position);
float distance_squared = vec2_dot(separation, separation);
if (distance_squared == 0.0f) {
continue;
}
Vector2 direction = vec2_normalize(separation);
float factor = SIMULATION_G * dt / distance_squared;
body_1->speed =
vec2_sum(body_1->speed, vec2_scale(direction, factor * body_2->mass));
body_2->speed = vec2_sum(body_2->speed,
vec2_scale(direction, -factor * body_1->mass));
}
uni->bodies[i] = body_1;
}
}
+21 -13
View File
@@ -6,19 +6,27 @@
#define EPSILON 8.85418782E-12
void magnetism(universe_t* uni){
mass_body_t body_1;
mass_body_t body_2;
Vector2 diff, force;
for (unsigned int i = 0; i < uni->num_bodies-1; i++) {
body_1 = uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
body_2 = uni->bodies[y];
diff=vec2_diff(body_2.position,body_1.position);
force = vec2_scale(diff,body_1.charge*body_2.charge/(4*PI*EPSILON*vec2_len(diff)));
body_1.speed=vec2_sum(body_1.speed,vec2_scale(force,1/body_1.mass));
body_2.speed=vec2_diff(body_2.speed,vec2_scale(force,1/body_2.mass));
uni->bodies[y] = body_2;
for (unsigned int i = 0; i < uni->num_bodies; i++) {
mass_body_t *body_1 = &uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
mass_body_t *body_2 = &uni->bodies[y];
if (body_1->charge == 0 || body_2->charge == 0) {
continue;
}
Vector2 diff = vec2_diff(body_2->position, body_1->position);
float distance_squared = vec2_dot(diff, diff);
if (distance_squared == 0) {
continue;
}
// Charges are in coulombs; convert acceleration to Earth radii/s^2.
float factor = PHYSICS_TIMESTEP * body_1->charge * body_2->charge /
(4 * PI * EPSILON * EARTH_MASS * DISTANCE_UNIT_METERS *
DISTANCE_UNIT_METERS * DISTANCE_UNIT_METERS * distance_squared);
Vector2 impulse = vec2_scale(vec2_normalize(diff), factor);
body_1->speed = vec2_diff(body_1->speed,
vec2_scale(impulse, 1 / body_1->mass));
body_2->speed = vec2_sum(body_2->speed,
vec2_scale(impulse, 1 / body_2->mass));
}
uni->bodies[i] = body_1;
}
}
+125 -61
View File
@@ -2,19 +2,17 @@
#include "consts.h"
#include "gravity.h"
#include "mechanics.h"
<<<<<<< HEAD
#include "magnetism.h"
=======
#include "mechanics.h"
#include "objects.h"
>>>>>>> aa4f0dfe52bab37ef3bf82a47c44316c74c3ccbc
#include <bits/time.h>
#include "vec_utils.h"
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
@@ -26,48 +24,76 @@ static inline int center_y_text(int height, int size) {
return (height - size) / 2;
}
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;
struct timespec ot, ct;
struct timespec next_step;
clock_gettime(CLOCK_MONOTONIC, &next_step);
while (true) {
printf("a");
clock_gettime(CLOCK_MONOTONIC, &ct);
// gravity(uni);
mechanics(uni);
magnetism(uni);
ot = 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);
next_step.tv_sec += LOOP_TIMESTEP / 1000000000LL;
next_step.tv_nsec += LOOP_TIMESTEP % 1000000000LL;
if (next_step.tv_nsec >= 1000000000L) {
next_step.tv_nsec -= 1000000000L;
next_step.tv_sec++;
}
gravity(uni);
magnetism(uni);
mechanics(uni);
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (now.tv_sec > next_step.tv_sec ||
(now.tv_sec == next_step.tv_sec && now.tv_nsec > next_step.tv_nsec)) {
next_step =
now; // An overrun starts a fresh schedule, without catch-up steps.
}
// Sleep only until the deadline; processing time is already included.
while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_step, NULL) ==
EINTR) {
}
do {
clock_gettime(CLOCK_MONOTONIC, &ct);
} while (timespec_diff_ns(ct, ot) < TIMESTEP / simx);
}
}
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 = 2;
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){100000000000.0f, 200, {0, 0}, {0, 0}};
universe.bodies[1] = (mass_body_t){100, 50, {500, 0}, {0, 2}};
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(800, 450, "Fucked Physics");
SetTargetFPS(120);
@@ -80,12 +106,10 @@ int main(void) {
key_pressed = GetKeyPressed();
switch (key_pressed) {
case KEY_I:
v_scale += 0.1;
v_scale *= 1.26;
break;
case KEY_O:
if (v_scale > 0.1) {
v_scale -= 0.1;
}
v_scale /= 1.26;
break;
case KEY_LEFT:
canvas_offset.x += 100;
@@ -99,42 +123,82 @@ 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);
if (canvas_offset.x + GetRenderWidth() * 0.5 / v_scale >
GetRenderWidth() * 5) {
canvas_offset.x =
-GetRenderWidth() * 0.5 / v_scale + GetRenderWidth() * 5;
} else if (canvas_offset.x - GetRenderWidth() * 0.5 / v_scale <
-GetRenderWidth() * 5) {
canvas_offset.x = GetRenderWidth() * 0.5 / v_scale - GetRenderWidth() * 5;
}
if (universe.num_bodies < 1024 &&
(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);
if (canvas_offset.y + GetRenderHeight() * 0.5 / v_scale >
GetRenderHeight() * 5) {
canvas_offset.y =
-GetRenderHeight() * 0.5 / v_scale + GetRenderHeight() * 5;
} else if (canvas_offset.y - GetRenderHeight() * 0.5 / v_scale <
-GetRenderHeight() * 5) {
canvas_offset.y =
GetRenderHeight() * 0.5 / v_scale - GetRenderHeight() * 5;
universe.bodies[universe.num_bodies] =
(mass_body_t){100000.0f, 0.1, mouse_pos, {0, 0}, 10, 0, ""};
snprintf(universe.bodies[universe.num_bodies].name,
sizeof(universe.bodies[universe.num_bodies].name), "Body %u",
universe.num_bodies + 1);
universe.num_bodies++;
}
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);
DrawCircleV(transform_to_center(body_position),
universe.bodies[i].radius * v_scale,
(Color){((256 / universe.num_bodies) * i) % 256,
255 * (i % 2), 255 * (i % 2) * (i % 3), 255});
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});
// Keep labels readable at every zoom level.
DrawText(universe.bodies[i].name,
(int)(body_position.x + draw_radius + 6),
(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();
}
+17 -19
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) {
float max_x = GetRenderWidth() * 10 / 2.0f - a->radius;
float max_y = GetRenderHeight() * 10 / 2.0f - a->radius;
float max_x = GetRenderWidth() * 100 / 2.0f - a->radius;
float max_y = GetRenderHeight() * 100 / 2.0f - a->radius;
if (a->position.x > max_x) {
a->position.x = max_x;
@@ -73,25 +73,23 @@ void collide_rigid(mass_body_t *a, float restitution) {
}
void mechanics(universe_t *uni) {
mass_body_t body_1;
mass_body_t body_2;
Vector2 collision;
Vector2 scaled_speed;
// Advance every body to the same simulated time before checking contacts.
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;
mass_body_t *body = &uni->bodies[i];
body->position = vec2_sum(body->position,
vec2_scale(body->speed, PHYSICS_TIMESTEP));
}
for (unsigned int i = 0; i < uni->num_bodies; i++) {
mass_body_t *body_1 = &uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
mass_body_t *body_2 = &uni->bodies[y];
Vector2 collision = body_collision(body_1, body_2);
if (!vec2_is_null(collision)) {
collide(body_1, body_2, collision, 0.99);
}
}
collide_rigid(&body_1, 1.1);
uni->bodies[i] = body_1;
if (WALL_COLLISIONS) {
collide_rigid(body_1, 1.0f);
}
}
}
+7 -5
View File
@@ -3,11 +3,13 @@
#include "vec_utils.h"
typedef struct {
float mass;
float radius;
Vector2 position;
Vector2 speed;
float charge;
float mass; // Earth masses.
float radius; // Earth radii (physical collision radius).
Vector2 position; // Earth radii.
Vector2 speed; // Earth radii per simulated second.
float draw_radius;
float charge; // Coulombs.
char name[32];
} mass_body_t;
typedef struct universe {
+4
View File
@@ -0,0 +1,4 @@
mass(EARTH_MASS), radius(km), {x(UA), y(UA)}, {vx(km/s), vy(km/s)}, draw_radius(km), charge(C), name
396518.4, 760000, {-0.85, 0}, {0, 45.0}, 220000, 0, Alpha
297388.8, 650000, {-0.35, 0}, {0, -37.0}, 200000, 0, Beta
495648.0, 900000, {1.30, 0}, {0, -14.5}, 250000, 0, Gamma