Compare commits

...
21 Commits
Author SHA1 Message Date
h3cx 0891150f9d Fixed some things, broke some things, we now have some sort of a config idk timestep broken 2026-10-01 23:26:07 +02:00
h3cx c729ab4f90 Fixed orbit 2026-10-01 19:09:54 +02:00
Pierre Barbier 2a7669aa21 Solar system in main 2026-10-01 11:18:38 +02:00
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
Pierre 09384b878f added charges 2026-09-30 23:44:19 +02:00
Pierre e7cd82c720 merged? 2026-09-30 22:56:09 +02:00
Pierre 9fcf86cbbf start of electromagnetism Tesla would be proud 2026-09-30 22:26:06 +02:00
h3cx aa4f0dfe52 Update to non pointers and not working gravit 2026-09-30 22:23:04 +02:00
h3cx 397d818c62 Updated canvas size 2026-09-30 21:51:34 +02:00
h3cx 41001daa4f Merge branch 'main' of git.h3cx.dev:h3cx/fphysics 2026-09-30 21:50:12 +02:00
h3cx 8eeb27ce4c Added window logic 2026-09-30 21:50:09 +02:00
Pierre 2866a38169 removed debug print 2026-09-30 21:46:44 +02:00
Pierre b3dcc9aa66 fixed waiting till heat death problem 2026-09-30 21:46:28 +02:00
Pierre f7e2d85e37 separated the headers and c files properly 2026-09-30 21:38:44 +02:00
h3cx 14152cb5a0 Updated edge collision 2026-09-30 20:54:55 +02:00
h3cx 848af2bcac Fixed sticking 2026-09-30 09:07:28 +02:00
Pierre 6f57a943b2 funny janky version 2026-09-29 22:45:44 +02:00
h3cx ce1a59f3d5 Fixed stupid shit 2026-09-29 22:18:36 +02:00
h3cx 2823d3e0c0 Merge branch 'main' of git.h3cx.dev:h3cx/fphysics 2026-09-29 22:07:53 +02:00
h3cx 6c98e3007c Throttled loop, x and y properly fucked up 2026-09-29 22:07:20 +02:00
18 changed files with 498 additions and 179 deletions

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
CompileFlags:
Add: [-xc, -std=c11]
If:
PathMatch: [".*\\.h$"]
CompileFlags:
Add: [-x, c-header]
+12 -5
View File
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.20)
set(CMAKE_C_COMPILER gcc)
project(fuckedphysics
VERSION 0.1.0
LANGUAGES C
@@ -19,6 +21,10 @@ find_package(raylib REQUIRED)
add_executable(${PROJECT_NAME}
main.c
mechanics.c
vec_utils.c
gravity.c
magnetism.c
)
target_link_libraries(${PROJECT_NAME}
@@ -26,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
+16
View File
@@ -0,0 +1,16 @@
#ifndef CONSTS_H_
#define CONSTS_H_
#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_
-8
View File
@@ -1,8 +0,0 @@
#include "cphysics.h"
void gravity(universe_t* univ){
}
void magnetism(universe_t* univ){
}
-7
View File
@@ -1,7 +0,0 @@
#ifndef CPHYSICS_H_
#define CPHYSICS_H_
#include "objects.h"
void gravity(universe_t* univ);
void magnetism(universe_t* univ);
void mechanics(universe_t* univ);
#endif // CPHYSICS_H_
+25
View File
@@ -0,0 +1,25 @@
#include "gravity.h"
#include "consts.h"
#include "vec_utils.h"
#include <raylib.h>
void gravity(universe_t *uni) {
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++) {
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));
}
}
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#ifndef GRAVITY_H_
#define GRAVITY_H_
#include "consts.h"
#include "objects.h"
void gravity(universe_t *uni);
#endif // MECHANICS_H_
+32
View File
@@ -0,0 +1,32 @@
#include "magnetism.h"
#include "consts.h"
#include "vec_utils.h"
#include <math.h>
#define EPSILON 8.85418782E-12
void magnetism(universe_t* uni){
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));
}
}
}
+7
View File
@@ -0,0 +1,7 @@
#ifndef MAGNETISM_H
#define MAGNETISM_H
#include "objects.h"
void magnetism(universe_t* univ);
#endif // MAGNETISM_H
+171 -112
View File
@@ -1,145 +1,204 @@
#define _POSIX_C_SOURCE 200809L
#include "consts.h"
#include "gravity.h"
#include "magnetism.h"
#include "mechanics.h"
#include "objects.h"
#include "vec_utils.h"
#include <errno.h>
#include <math.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include "objects.h"
#include <string.h>
#include <time.h>
#include <unistd.h>
int center_x_text(const char *text, int width, int size) {
return (width - MeasureText(text, size)) / 2;
}
static inline int center_y_text(int height, int size) { return (height - size) / 2; }
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;
static inline int center_y_text(int height, int size) {
return (height - size) / 2;
}
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;
}
void *physics(void *args) {
universe_t* uni= (universe_t*)args;
mass_body_t body_1;
mass_body_t body_2;
Vector2 collision;
struct timespec ot, ct;
clock_gettime(CLOCK_REALTIME,&ot);
while(true){
clock_gettime(CLOCK_REALTIME,&ct);
for(unsigned int i=0;i<uni->num_bodies;i++){
body_1=uni->bodies[i];
body_1.position = vec2_sum(&body_1.position, &body_1.speed);
for(unsigned int y=0;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;
}
collision = edge_collision(&body_1);
if (!vec2_is_null(collision)) {
collide_rigid(&body_1, &collision, 1.0);
}
uni->bodies[i]=body_1;
}
ot=ct;
universe_t *uni = (universe_t *)args;
struct timespec next_step;
clock_gettime(CLOCK_MONOTONIC, &next_step);
while (true) {
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) {
}
}
}
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};
pthread_create(&physics_thread,NULL,physics,(void*)&universe);
universe.bodies = malloc(1024 * sizeof(mass_body_t));
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);
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}};
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()) {
BeginDrawing();
ClearBackground(RAYWHITE);
for(unsigned int i=0;i<universe.num_bodies;i++){
DrawCircleV(transform_to_center(universe.bodies[i].position), universe.bodies[i].radius, (Color){((256/universe.num_bodies)*i)%256,255*(i%2),255*(i%2)*(i%3),255});
int key_pressed = 0;
do {
key_pressed = GetKeyPressed();
switch (key_pressed) {
case KEY_I:
v_scale *= 1.26;
break;
case KEY_O:
v_scale /= 1.26;
break;
case KEY_LEFT:
canvas_offset.x += 100;
break;
case KEY_RIGHT:
canvas_offset.x -= 100;
break;
case KEY_UP:
canvas_offset.y += 100;
break;
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 (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);
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 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});
// 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();
}
+95
View File
@@ -0,0 +1,95 @@
#include "mechanics.h"
#include "objects.h"
#include <stdio.h>
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;
}
if (len == 0) {
return (Vector2){1, 0};
}
Vector2 result = vec2_normalize(diff);
return result;
}
void collide(mass_body_t *a, mass_body_t *b, const Vector2 normal,
float resitution) {
float inverse_a = 1.0f / a->mass;
float inverse_b = 1.0f / b->mass;
float inverse_total = inverse_a + inverse_b;
float overlap =
a->radius + b->radius - vec2_len(vec2_diff(a->position, b->position));
Vector2 correction = vec2_scale(normal, overlap / inverse_total);
Vector2 a_move = vec2_scale(correction, inverse_a);
Vector2 b_move = vec2_scale(correction, inverse_b);
a->position = vec2_sum(a->position, a_move);
b->position = vec2_diff(b->position, b_move);
Vector2 relative = vec2_diff(b->speed, a->speed);
float v_normal = vec2_dot(relative, normal);
if (v_normal <= 0) {
return;
}
float factor = -(1 + resitution);
float j = factor * v_normal;
j /= inverse_total;
Vector2 final = vec2_scale(normal, j);
Vector2 a_diff = vec2_scale(final, inverse_a);
a->speed = vec2_diff(a->speed, a_diff);
Vector2 b_diff = vec2_scale(final, inverse_b);
b->speed = vec2_sum(b->speed, b_diff);
}
void collide_rigid(mass_body_t *a, float restitution) {
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;
if (a->speed.x > 0)
a->speed.x *= -restitution;
} else if (a->position.x < -max_x) {
a->position.x = -max_x;
if (a->speed.x < 0)
a->speed.x *= -restitution;
}
if (a->position.y > max_y) {
a->position.y = max_y;
if (a->speed.y > 0)
a->speed.y *= -restitution;
} else if (a->position.y < -max_y) {
a->position.y = -max_y;
if (a->speed.y < 0)
a->speed.y *= -restitution;
}
}
void mechanics(universe_t *uni) {
// Advance every body to the same simulated time before checking contacts.
for (unsigned int i = 0; i < uni->num_bodies; i++) {
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);
}
}
if (WALL_COLLISIONS) {
collide_rigid(body_1, 1.0f);
}
}
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#ifndef MECHANICS_H_
#define MECHANICS_H_
#include "objects.h"
#include "consts.h"
void mechanics(universe_t* uni);
#endif // MECHANICS_H_
+8 -5
View File
@@ -1,12 +1,15 @@
#ifndef OBJECTS_H_
#define OBJECTS_H_
#include "vec_util.h"
#include "vec_utils.h"
typedef struct {
float mass;
float radius;
Vector2 position;
Vector2 speed;
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
-42
View File
@@ -1,42 +0,0 @@
#ifndef VEC_UTIL_H_
#define VEC_UTIL_H_
#include <raylib.h>
#include <math.h>
static const Vector2 NULL_VEC2 = {0, 0};
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;
}
#endif // VEC_UTIL_H_
+45
View File
@@ -0,0 +1,45 @@
#include "vec_utils.h"
bool vec2_is_null(Vector2 v) {
if (v.x == 0 && v.y == 0) {
return true;
}
return false;
}
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) {
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;
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#ifndef VEC_UTILS_H_
#define VEC_UTILS_H_
#include <math.h>
#include <raylib.h>
static const Vector2 NULL_VEC2 = {0, 0};
bool vec2_is_null(Vector2 v) ;
Vector2 transform_to_center(Vector2 v);
Vector2 vec2_sum(const Vector2 a, const Vector2 b);
float vec2_len(Vector2 v) ;
Vector2 vec2_normalize(const Vector2 v) ;
Vector2 vec2_scale(const Vector2 normal, const float len) ;
float vec2_dot(const Vector2 a, const Vector2 b) ;
Vector2 vec2_diff(const Vector2 a, const Vector2 b) ;
#endif // VEC_UTILS_H_