Compare commits
10
Commits
41001daa4f
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0891150f9d
|
||
|
|
c729ab4f90
|
||
|
|
2a7669aa21 | ||
|
|
ff3dcfcf8e
|
||
|
|
d4987568d7
|
||
|
|
09384b878f | ||
|
|
e7cd82c720 | ||
|
|
9fcf86cbbf | ||
|
|
aa4f0dfe52
|
||
|
|
397d818c62
|
No files matched your search
+6
-4
@@ -21,9 +21,10 @@ find_package(raylib REQUIRED)
|
|||||||
|
|
||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
main.c
|
main.c
|
||||||
cphysics.c
|
|
||||||
mechanics.c
|
mechanics.c
|
||||||
vec_util.c
|
vec_utils.c
|
||||||
|
gravity.c
|
||||||
|
magnetism.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
@@ -31,9 +32,10 @@ target_link_libraries(${PROJECT_NAME}
|
|||||||
raylib
|
raylib
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_options(${PROJECT_NAME}
|
target_compile_options(${PROJECT_NAME} PRIVATE
|
||||||
PRIVATE
|
|
||||||
-Wall
|
-Wall
|
||||||
-Wextra
|
-Wextra
|
||||||
-Wpedantic
|
-Wpedantic
|
||||||
|
$<$<CONFIG:Debug>:-ggdb -O0>
|
||||||
|
$<$<CONFIG:Release>:-O3>
|
||||||
)
|
)
|
||||||
+38
@@ -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
|
||||||
@@ -1,7 +1,16 @@
|
|||||||
#ifndef CONSTS_H_
|
#ifndef CONSTS_H_
|
||||||
#define CONSTS_H_
|
#define CONSTS_H_
|
||||||
#define TIMESTEP 1000000
|
#define LOOP_TIMESTEP \
|
||||||
#define simx 2
|
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_
|
#endif // CONSTS_H_
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
#include "cphysics.h"
|
|
||||||
#include "objects.h"
|
|
||||||
|
|
||||||
void gravity(universe_t* univ){
|
|
||||||
|
|
||||||
}
|
|
||||||
void magnetism(universe_t* univ){
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#ifndef CPHYSICS_H_
|
|
||||||
#define CPHYSICS_H_
|
|
||||||
#include "objects.h"
|
|
||||||
void gravity(universe_t* univ);
|
|
||||||
void magnetism(universe_t* univ);
|
|
||||||
#endif // CPHYSICS_H_
|
|
||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef MAGNETISM_H
|
||||||
|
#define MAGNETISM_H
|
||||||
|
#include "objects.h"
|
||||||
|
void magnetism(universe_t* univ);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // MAGNETISM_H
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include "consts.h"
|
#include "consts.h"
|
||||||
|
#include "gravity.h"
|
||||||
|
#include "magnetism.h"
|
||||||
#include "mechanics.h"
|
#include "mechanics.h"
|
||||||
#include <bits/time.h>
|
#include "objects.h"
|
||||||
|
#include "vec_utils.h"
|
||||||
|
#include <errno.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <threads.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -20,35 +24,45 @@ static inline int center_y_text(int height, int size) {
|
|||||||
return (height - size) / 2;
|
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) {
|
void *physics(void *args) {
|
||||||
universe_t *uni = (universe_t *)args;
|
universe_t *uni = (universe_t *)args;
|
||||||
struct timespec ot, ct;
|
struct timespec next_step;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &next_step);
|
||||||
while (true) {
|
while (true) {
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ct);
|
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);
|
mechanics(uni);
|
||||||
ot = ct;
|
struct timespec now;
|
||||||
long long elapsed = timespec_diff_ns(ct, ot);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
long long remaining = TIMESTEP / simx - elapsed;
|
if (now.tv_sec > next_step.tv_sec ||
|
||||||
|
(now.tv_sec == next_step.tv_sec && now.tv_nsec > next_step.tv_nsec)) {
|
||||||
if (remaining > 0) {
|
next_step =
|
||||||
long long sleep_time = remaining * 9 / 10;
|
now; // An overrun starts a fresh schedule, without catch-up steps.
|
||||||
|
}
|
||||||
struct timespec sleep_time_spec = {0, sleep_time};
|
// Sleep only until the deadline; processing time is already included.
|
||||||
thrd_sleep(&sleep_time_spec, NULL);
|
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;
|
float v_scale = 1;
|
||||||
Vector2 canvas_offset = {0, 0};
|
Vector2 canvas_offset = {0, 0};
|
||||||
pthread_t physics_thread;
|
pthread_t physics_thread;
|
||||||
@@ -56,38 +70,30 @@ int main(void) {
|
|||||||
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 = 0;
|
universe.num_bodies = 0;
|
||||||
for (int slot = -1; slot < 32 * 20 && universe.num_bodies < 1024; slot++) {
|
char line[1024];
|
||||||
float radius = 12.0f + 8.0f / (universe.num_bodies + 1);
|
fgets(line, 1024, file);
|
||||||
float x = slot < 0
|
while (fgets(line, 1024, file)) {
|
||||||
? 0.0f
|
printf("%s", line);
|
||||||
: -387.5f + 25.0f * (slot % 32) + (slot / 32 % 2) * 12.5f;
|
float mass, radius, x, y, vx, vy, draw_radius, charge;
|
||||||
float y = slot < 0 ? 0.0f : -209.0f + 22.0f * (slot / 32);
|
char name[32];
|
||||||
if (fabsf(x) + radius > 400.0f || fabsf(y) + radius > 225.0f) {
|
sscanf(line, "%f, %f, {%f, %f}, {%f, %f}, %f, %f, %31s", &mass, &radius, &x,
|
||||||
continue;
|
&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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool overlaps = false;
|
fclose(file);
|
||||||
for (unsigned int j = 0; j < universe.num_bodies; j++) {
|
|
||||||
float dx = x - universe.bodies[j].position.x;
|
|
||||||
float dy = y - universe.bodies[j].position.y;
|
|
||||||
float min_distance = radius + universe.bodies[j].radius;
|
|
||||||
if (dx * dx + dy * dy <= min_distance * min_distance) {
|
|
||||||
overlaps = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (overlaps) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int i = universe.num_bodies++;
|
|
||||||
universe.bodies[i] = (mass_body_t){
|
|
||||||
radius,
|
|
||||||
radius,
|
|
||||||
{x, y},
|
|
||||||
{-0.1f, 0.8f - i * 0.005f},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||||
InitWindow(800, 450, "Fucked Physics");
|
InitWindow(800, 450, "Fucked Physics");
|
||||||
SetTargetFPS(120);
|
SetTargetFPS(120);
|
||||||
@@ -100,12 +106,10 @@ 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;
|
v_scale *= 1.26;
|
||||||
break;
|
break;
|
||||||
case KEY_O:
|
case KEY_O:
|
||||||
if (v_scale > 0.1) {
|
v_scale /= 1.26;
|
||||||
v_scale -= 0.1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
canvas_offset.x += 100;
|
canvas_offset.x += 100;
|
||||||
@@ -119,42 +123,82 @@ int main(void) {
|
|||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
canvas_offset.y -= 100;
|
canvas_offset.y -= 100;
|
||||||
break;
|
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);
|
} while (key_pressed != 0);
|
||||||
|
|
||||||
if (canvas_offset.x + GetRenderWidth() * 0.5 / v_scale >
|
if (universe.num_bodies < 1024 &&
|
||||||
GetRenderWidth() * 5) {
|
(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) ||
|
||||||
canvas_offset.x =
|
IsMouseButtonDown(MOUSE_RIGHT_BUTTON))) {
|
||||||
-GetRenderWidth() * 0.5 / v_scale + GetRenderWidth() * 5;
|
Vector2 mouse_pos = vec2_scale(
|
||||||
} else if (canvas_offset.x - GetRenderWidth() * 0.5 / v_scale <
|
transform_to_center(vec2_scale(GetMousePosition(), -1)), -1);
|
||||||
-GetRenderWidth() * 5) {
|
mouse_pos = vec2_scale(mouse_pos, 1 / v_scale);
|
||||||
canvas_offset.x = GetRenderWidth() * 0.5 / v_scale - GetRenderWidth() * 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (canvas_offset.y + GetRenderHeight() * 0.5 / v_scale >
|
universe.bodies[universe.num_bodies] =
|
||||||
GetRenderHeight() * 5) {
|
(mass_body_t){100000.0f, 0.1, mouse_pos, {0, 0}, 10, 0, ""};
|
||||||
canvas_offset.y =
|
snprintf(universe.bodies[universe.num_bodies].name,
|
||||||
-GetRenderHeight() * 0.5 / v_scale + GetRenderHeight() * 5;
|
sizeof(universe.bodies[universe.num_bodies].name), "Body %u",
|
||||||
} else if (canvas_offset.y - GetRenderHeight() * 0.5 / v_scale <
|
universe.num_bodies + 1);
|
||||||
-GetRenderHeight() * 5) {
|
universe.num_bodies++;
|
||||||
canvas_offset.y =
|
|
||||||
GetRenderHeight() * 0.5 / v_scale - GetRenderHeight() * 5;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
for (unsigned int i = 0; i < universe.num_bodies; i++) {
|
for (unsigned int i = 0; i < universe.num_bodies; i++) {
|
||||||
Vector2 scaled_offset = vec2_scale(&canvas_offset, v_scale);
|
Vector2 local_offset = canvas_offset;
|
||||||
Vector2 body_position = vec2_scale(&universe.bodies[i].position, v_scale);
|
if (cam_lock != -1) {
|
||||||
body_position = vec2_sum(&body_position, &scaled_offset);
|
local_offset = vec2_scale(universe.bodies[cam_lock].position, -1);
|
||||||
DrawCircleV(transform_to_center(body_position),
|
}
|
||||||
universe.bodies[i].radius * v_scale,
|
Vector2 scaled_offset = vec2_scale(local_offset, v_scale);
|
||||||
(Color){((256 / universe.num_bodies) * i) % 256,
|
Vector2 body_position = vec2_scale(universe.bodies[i].position, v_scale);
|
||||||
255 * (i % 2), 255 * (i % 2) * (i % 3), 255});
|
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),
|
// DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20),
|
||||||
center_y_text(GetRenderHeight(), 20), 20, BLACK);
|
// center_y_text(GetRenderHeight(), 20), 20, BLACK);
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+28
-31
@@ -2,9 +2,8 @@
|
|||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
Vector2 body_collision(mass_body_t *a, mass_body_t *b) {
|
Vector2 body_collision(mass_body_t *a, mass_body_t *b) {
|
||||||
Vector2 diff = vec2_diff(&a->position, &b->position);
|
Vector2 diff = vec2_diff(a->position, b->position);
|
||||||
float len = vec2_len(diff);
|
float len = vec2_len(diff);
|
||||||
if (len > a->radius + b->radius) {
|
if (len > a->radius + b->radius) {
|
||||||
return NULL_VEC2;
|
return NULL_VEC2;
|
||||||
@@ -16,21 +15,21 @@ Vector2 body_collision(mass_body_t *a, mass_body_t *b) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void collide(mass_body_t *a, mass_body_t *b, const Vector2 *normal,
|
void collide(mass_body_t *a, mass_body_t *b, const Vector2 normal,
|
||||||
float resitution) {
|
float resitution) {
|
||||||
float inverse_a = 1.0f / a->mass;
|
float inverse_a = 1.0f / a->mass;
|
||||||
float inverse_b = 1.0f / b->mass;
|
float inverse_b = 1.0f / b->mass;
|
||||||
float inverse_total = inverse_a + inverse_b;
|
float inverse_total = inverse_a + inverse_b;
|
||||||
float overlap =
|
float overlap =
|
||||||
a->radius + b->radius - vec2_len(vec2_diff(&a->position, &b->position));
|
a->radius + b->radius - vec2_len(vec2_diff(a->position, b->position));
|
||||||
Vector2 correction = vec2_scale(normal, overlap / inverse_total);
|
Vector2 correction = vec2_scale(normal, overlap / inverse_total);
|
||||||
Vector2 a_move = vec2_scale(&correction, inverse_a);
|
Vector2 a_move = vec2_scale(correction, inverse_a);
|
||||||
Vector2 b_move = vec2_scale(&correction, inverse_b);
|
Vector2 b_move = vec2_scale(correction, inverse_b);
|
||||||
a->position = vec2_sum(&a->position, &a_move);
|
a->position = vec2_sum(a->position, a_move);
|
||||||
b->position = vec2_diff(&b->position, &b_move);
|
b->position = vec2_diff(b->position, b_move);
|
||||||
|
|
||||||
Vector2 relative = vec2_diff(&b->speed, &a->speed);
|
Vector2 relative = vec2_diff(b->speed, a->speed);
|
||||||
float v_normal = vec2_dot(&relative, normal);
|
float v_normal = vec2_dot(relative, normal);
|
||||||
if (v_normal <= 0) {
|
if (v_normal <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -42,15 +41,15 @@ void collide(mass_body_t *a, mass_body_t *b, const Vector2 *normal,
|
|||||||
|
|
||||||
Vector2 final = vec2_scale(normal, j);
|
Vector2 final = vec2_scale(normal, j);
|
||||||
|
|
||||||
Vector2 a_diff = vec2_scale(&final, inverse_a);
|
Vector2 a_diff = vec2_scale(final, inverse_a);
|
||||||
a->speed = vec2_diff(&a->speed, &a_diff);
|
a->speed = vec2_diff(a->speed, a_diff);
|
||||||
Vector2 b_diff = vec2_scale(&final, inverse_b);
|
Vector2 b_diff = vec2_scale(final, inverse_b);
|
||||||
b->speed = vec2_sum(&b->speed, &b_diff);
|
b->speed = vec2_sum(b->speed, b_diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
void collide_rigid(mass_body_t *a, float restitution) {
|
void collide_rigid(mass_body_t *a, float restitution) {
|
||||||
float max_x = GetRenderWidth() / 2.0f - a->radius;
|
float max_x = GetRenderWidth() * 100 / 2.0f - a->radius;
|
||||||
float max_y = GetRenderHeight() / 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;
|
||||||
@@ -74,25 +73,23 @@ void collide_rigid(mass_body_t *a, float restitution) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void mechanics(universe_t *uni) {
|
void mechanics(universe_t *uni) {
|
||||||
mass_body_t body_1;
|
// Advance every body to the same simulated time before checking contacts.
|
||||||
mass_body_t body_2;
|
|
||||||
Vector2 collision;
|
|
||||||
Vector2 scaled_speed;
|
|
||||||
for (unsigned int i = 0; i < uni->num_bodies; i++) {
|
for (unsigned int i = 0; i < uni->num_bodies; i++) {
|
||||||
body_1 = uni->bodies[i];
|
mass_body_t *body = &uni->bodies[i];
|
||||||
scaled_speed = vec2_scale(&body_1.speed, 1.0 / simx);
|
body->position = vec2_sum(body->position,
|
||||||
body_1.position = vec2_sum(&body_1.position, &scaled_speed);
|
vec2_scale(body->speed, PHYSICS_TIMESTEP));
|
||||||
if (i < uni->num_bodies - 1) {
|
}
|
||||||
|
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++) {
|
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
|
||||||
body_2 = uni->bodies[y];
|
mass_body_t *body_2 = &uni->bodies[y];
|
||||||
collision = body_collision(&body_1, &body_2);
|
Vector2 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
collide_rigid(&body_1, 1.1);
|
if (WALL_COLLISIONS) {
|
||||||
uni->bodies[i] = body_1;
|
collide_rigid(body_1, 1.0f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
#ifndef OBJECTS_H_
|
#ifndef OBJECTS_H_
|
||||||
#define OBJECTS_H_
|
#define OBJECTS_H_
|
||||||
#include "vec_util.h"
|
#include "vec_utils.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float mass;
|
float mass; // Earth masses.
|
||||||
float radius;
|
float radius; // Earth radii (physical collision radius).
|
||||||
Vector2 position;
|
Vector2 position; // Earth radii.
|
||||||
Vector2 speed;
|
Vector2 speed; // Earth radii per simulated second.
|
||||||
|
float draw_radius;
|
||||||
|
float charge; // Coulombs.
|
||||||
|
char name[32];
|
||||||
} mass_body_t;
|
} mass_body_t;
|
||||||
|
|
||||||
typedef struct universe {
|
typedef struct universe {
|
||||||
|
|||||||
@@ -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
|
||||||
-21
@@ -1,21 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef VEC_UTIL_H_
|
|
||||||
#define VEC_UTIL_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_UTIL_H_
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "vec_util.h"
|
#include "vec_utils.h"
|
||||||
|
|
||||||
|
|
||||||
bool vec2_is_null(Vector2 v) {
|
bool vec2_is_null(Vector2 v) {
|
||||||
@@ -15,12 +15,12 @@ Vector2 transform_to_center(Vector2 v) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 vec2_sum(const Vector2 *a, const Vector2 *b) {
|
Vector2 vec2_sum(const Vector2 a, const Vector2 b) {
|
||||||
Vector2 result = {a->x + b->x, a->y + b->y};
|
Vector2 result = {a.x + b.x, a.y + b.y};
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
Vector2 vec2_diff(const Vector2 *a, const Vector2 *b) {
|
Vector2 vec2_diff(const Vector2 a, const Vector2 b) {
|
||||||
Vector2 result = {a->x - b->x, a->y - b->y};
|
Vector2 result = {a.x - b.x, a.y - b.y};
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,11 +35,11 @@ Vector2 vec2_normalize(const Vector2 v) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 vec2_scale(const Vector2 *normal, const float len) {
|
Vector2 vec2_scale(const Vector2 normal, const float len) {
|
||||||
Vector2 result = {len * normal->x, len * normal->y};
|
Vector2 result = {len * normal.x, len * normal.y};
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
float vec2_dot(const Vector2 *a, const Vector2 *b) {
|
float vec2_dot(const Vector2 a, const Vector2 b) {
|
||||||
return a->x * b->x + a->y * b->y;
|
return a.x * b.x + a.y * b.y;
|
||||||
}
|
}
|
||||||
+21
@@ -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_
|
||||||
Reference in new issue
Block a user