Compare commits
3
Commits
ff3dcfcf8e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0891150f9d
|
||
|
|
c729ab4f90
|
||
|
|
2a7669aa21 |
No files matched your search
+4
-2
@@ -24,6 +24,7 @@ add_executable(${PROJECT_NAME}
|
|||||||
mechanics.c
|
mechanics.c
|
||||||
vec_utils.c
|
vec_utils.c
|
||||||
gravity.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 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_
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
void gravity(universe_t *uni) {
|
void gravity(universe_t *uni) {
|
||||||
const float dt = 1.0f / simx;
|
const float dt = PHYSICS_TIMESTEP;
|
||||||
for (unsigned int i = 0; i < uni->num_bodies; i++) {
|
for (unsigned int i = 0; i < uni->num_bodies; i++) {
|
||||||
mass_body_t *body_1 = &uni->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++) {
|
||||||
@@ -15,7 +15,7 @@ void gravity(universe_t *uni) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Vector2 direction = vec2_normalize(separation);
|
Vector2 direction = vec2_normalize(separation);
|
||||||
float factor = NEWTON_G * dt / distance_squared;
|
float factor = SIMULATION_G * dt / distance_squared;
|
||||||
body_1->speed =
|
body_1->speed =
|
||||||
vec2_sum(body_1->speed, vec2_scale(direction, factor * body_2->mass));
|
vec2_sum(body_1->speed, vec2_scale(direction, factor * body_2->mass));
|
||||||
body_2->speed = vec2_sum(body_2->speed,
|
body_2->speed = vec2_sum(body_2->speed,
|
||||||
|
|||||||
+20
-12
@@ -6,19 +6,27 @@
|
|||||||
#define EPSILON 8.85418782E-12
|
#define EPSILON 8.85418782E-12
|
||||||
|
|
||||||
void magnetism(universe_t* uni){
|
void magnetism(universe_t* uni){
|
||||||
mass_body_t body_1;
|
for (unsigned int i = 0; i < uni->num_bodies; i++) {
|
||||||
mass_body_t body_2;
|
mass_body_t *body_1 = &uni->bodies[i];
|
||||||
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++) {
|
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
|
||||||
body_2 = uni->bodies[y];
|
mass_body_t *body_2 = &uni->bodies[y];
|
||||||
diff=vec2_diff(body_2.position,body_1.position);
|
if (body_1->charge == 0 || body_2->charge == 0) {
|
||||||
force = vec2_scale(diff,body_1.charge*body_2.charge/(4*PI*EPSILON*vec2_len(diff)));
|
continue;
|
||||||
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));
|
Vector2 diff = vec2_diff(body_2->position, body_1->position);
|
||||||
uni->bodies[y] = body_2;
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,19 +2,17 @@
|
|||||||
|
|
||||||
#include "consts.h"
|
#include "consts.h"
|
||||||
#include "gravity.h"
|
#include "gravity.h"
|
||||||
#include "mechanics.h"
|
|
||||||
<<<<<<< HEAD
|
|
||||||
#include "magnetism.h"
|
#include "magnetism.h"
|
||||||
=======
|
#include "mechanics.h"
|
||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
#include "vec_utils.h"
|
#include "vec_utils.h"
|
||||||
#include <bits/time.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>
|
||||||
|
|
||||||
@@ -26,51 +24,75 @@ 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;
|
||||||
gravity(uni);
|
next_step.tv_nsec += LOOP_TIMESTEP % 1000000000LL;
|
||||||
mechanics(uni);
|
if (next_step.tv_nsec >= 1000000000L) {
|
||||||
magnetism(uni);
|
next_step.tv_nsec -= 1000000000L;
|
||||||
ot = ct;
|
next_step.tv_sec++;
|
||||||
long long elapsed = timespec_diff_ns(ct, ot);
|
}
|
||||||
long long remaining = TIMESTEP / simx - elapsed;
|
gravity(uni);
|
||||||
|
magnetism(uni);
|
||||||
if (remaining > 0) {
|
mechanics(uni);
|
||||||
long long sleep_time = remaining * 9 / 10;
|
struct timespec now;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
struct timespec sleep_time_spec = {0, sleep_time};
|
if (now.tv_sec > next_step.tv_sec ||
|
||||||
thrd_sleep(&sleep_time_spec, NULL);
|
(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;
|
float v_scale = 1;
|
||||||
Vector2 canvas_offset = {0, 0};
|
Vector2 canvas_offset = {0, 0};
|
||||||
pthread_t physics_thread;
|
pthread_t physics_thread;
|
||||||
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 = 3;
|
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[0] =
|
universe.bodies[universe.num_bodies] = (mass_body_t){
|
||||||
(mass_body_t){10000000000000.0f, 1000, {-0, 0}, {0, 0}, 200};
|
.mass = mass,
|
||||||
universe.bodies[1] =
|
.radius = radius / EARTH_RADIUS,
|
||||||
(mass_body_t){100000000.0f, 0.1, {-10000.1, 0}, {0, -0.2}, 50};
|
.position = (Vector2){x * UA / EARTH_RADIUS, y * UA / EARTH_RADIUS},
|
||||||
universe.bodies[2] =
|
.speed = (Vector2){vx / EARTH_RADIUS, vy / EARTH_RADIUS},
|
||||||
(mass_body_t){100000000.0f, 0.1, {10000, 0}, {0, -0.2}, 50};
|
.draw_radius = draw_radius / EARTH_RADIUS,
|
||||||
|
.charge = charge};
|
||||||
|
|
||||||
|
memcpy((void *)&universe.bodies[universe.num_bodies].name, (void *)&name,
|
||||||
|
32);
|
||||||
|
universe.num_bodies++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||||
InitWindow(800, 450, "Fucked Physics");
|
InitWindow(800, 450, "Fucked Physics");
|
||||||
@@ -84,18 +106,10 @@ int main(void) {
|
|||||||
key_pressed = GetKeyPressed();
|
key_pressed = GetKeyPressed();
|
||||||
switch (key_pressed) {
|
switch (key_pressed) {
|
||||||
case KEY_I:
|
case KEY_I:
|
||||||
if (v_scale >= 0.1) {
|
v_scale *= 1.26;
|
||||||
v_scale += 0.1;
|
|
||||||
} else {
|
|
||||||
v_scale += 0.01;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case KEY_O:
|
case KEY_O:
|
||||||
if (v_scale > 0.1) {
|
v_scale /= 1.26;
|
||||||
v_scale -= 0.1;
|
|
||||||
} else if (v_scale > 0.01) {
|
|
||||||
v_scale -= 0.01;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
canvas_offset.x += 100;
|
canvas_offset.x += 100;
|
||||||
@@ -109,54 +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 (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) ||
|
if (universe.num_bodies < 1024 &&
|
||||||
IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) {
|
(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) ||
|
||||||
|
IsMouseButtonDown(MOUSE_RIGHT_BUTTON))) {
|
||||||
Vector2 mouse_pos = vec2_scale(
|
Vector2 mouse_pos = vec2_scale(
|
||||||
transform_to_center(vec2_scale(GetMousePosition(), -1)), -1);
|
transform_to_center(vec2_scale(GetMousePosition(), -1)), -1);
|
||||||
mouse_pos = vec2_scale(mouse_pos, 1 / v_scale);
|
mouse_pos = vec2_scale(mouse_pos, 1 / v_scale);
|
||||||
|
|
||||||
universe.bodies[universe.num_bodies] =
|
universe.bodies[universe.num_bodies] =
|
||||||
(mass_body_t){100000.0f, 0.1, mouse_pos, {0, 0}, 10};
|
(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++;
|
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();
|
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;
|
||||||
|
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);
|
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),
|
body_position = transform_to_center(body_position);
|
||||||
universe.bodies[i].draw_radius * v_scale,
|
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,
|
(Color){((256 / universe.num_bodies) * i) % 256 / 2,
|
||||||
255 * (i % 2) / 2, 255 * (i % 2) * (i % 3) / 2, 255});
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-15
@@ -73,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, 0.99);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,12 +3,13 @@
|
|||||||
#include "vec_utils.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 draw_radius;
|
||||||
float charge;
|
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
|
||||||
Reference in new issue
Block a user