From b33e9b37a27614fd6e3279a5f3259abc4a2e5052 Mon Sep 17 00:00:00 2001 From: Alejandro Laguna Date: Wed, 19 Nov 2025 18:45:25 +0100 Subject: [PATCH] feat: bodies attract --- include/sims.hpp | 11 +++++++---- include/utils.hpp | 13 +++++++++++++ src/main.cpp | 7 ++++--- src/sims/two_bodies.cpp | 43 ++++++++++++++++++++++++++++++++++++++--- src/utils/rand.cpp | 15 ++++++++++++++ 5 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 include/utils.hpp create mode 100644 src/utils/rand.cpp diff --git a/include/sims.hpp b/include/sims.hpp index 9d870d3..db36d33 100644 --- a/include/sims.hpp +++ b/include/sims.hpp @@ -1,21 +1,23 @@ #ifndef SIMS_HPP #define SIMS_HPP +#include "utils.hpp" #include #include #include class Body { private: + std::pair old_positions[200]; + +public: float x; float y; float vx = 1; float vy = 1; - std::pair old_positions[200]; - -public: SDL_Renderer* renderer; SDL_Color color = {255, 255, 255, 255}; // white + long double mass = EARTH_MASS; Body(int x_pos, int y_pos); @@ -23,6 +25,7 @@ public: void live(); }; -void two_bodies(SDL_Renderer* r, Body &a, Body &b); +void two_bodies(SDL_Renderer* renderer, Body &a, Body &b); +Body create_random_body(); #endif diff --git a/include/utils.hpp b/include/utils.hpp new file mode 100644 index 0000000..e30d6d2 --- /dev/null +++ b/include/utils.hpp @@ -0,0 +1,13 @@ +#ifndef UTILS_H +#define UTILS_H + +#include + +// constants + +const long double EARTH_MASS = 5.972e24; +const long double G = 6.67430e-11L; + +std::pair get_random_position(int x_min, int x_max, int y_min, int y_max); + +#endif diff --git a/src/main.cpp b/src/main.cpp index 60b16f2..98074a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include "../include/sims.hpp" @@ -30,9 +31,9 @@ bool event_handling(SDL_Event e) { void sim(SDL_Renderer *r, SDL_Event e) { bool exit = false; - Body body1 = Body(50, 50); + Body body1 = create_random_body(); body1.renderer = r; - Body body2 = Body(50, 100); + Body body2 = create_random_body(); body2.renderer = r; while (!exit) { @@ -52,7 +53,7 @@ int main() { } SDL_Window *window; - window = SDL_CreateWindow("Physics Sim", 720, 540, SDL_WINDOW_RESIZABLE); + window = SDL_CreateWindow("Physics Sim", 1920, 1080, SDL_WINDOW_FULLSCREEN); if (window == NULL) { std::printf("SDL_CreateWindow() failed"); return EXIT_FAILURE; diff --git a/src/sims/two_bodies.cpp b/src/sims/two_bodies.cpp index 63b505e..196b7e2 100644 --- a/src/sims/two_bodies.cpp +++ b/src/sims/two_bodies.cpp @@ -3,6 +3,7 @@ #include #include "../../include/sims.hpp" #include "../../include/gfx.hpp" +#include "../../include/utils.hpp" #include #include #include @@ -41,14 +42,50 @@ void Body::draw_tail() { void Body::live() { - x += vx; - y += sin(x * 0.1); + x += vx / 10000000000; + y += vy / 10000000000; draw_circle(renderer, x, y, 10, true); draw_tail(); } -void two_bodies(SDL_Renderer* r, Body& a, Body& b) { +Body create_random_body() { + pair pos = get_random_position(100, 700, 100, 700); + + Body b = Body(pos.first, pos.second); + return b; +} + +void two_bodies(SDL_Renderer* renderer, Body& a, Body& b) { + + float dt = 0.016; // 60 ticks per second, so the delta between one update and the next one is 0.016s + + float dx = b.x - a.x; + float dy = b.y - a.y; + float r = sqrtf(dx*dx + dy*dy); + + double F = G * (a.mass * b.mass) / (r*r); + double ux = dx / r; + double uy = dy / r; + + double Fx = F * ux; + double Fy = F * uy; + a.vx += (Fx / a.mass) * dt; + a.vy += (Fy / a.mass) * dt; + + b.vx -= (Fx / b.mass) * dt; + b.vy -= (Fy / b.mass) * dt; + + if (r < 11) { + a.vx = 0; + b.vx = 0; + a.vy = 0; + b.vy = 0; + } + + SDL_Log("F: %f", F); + a.live(); b.live(); } + diff --git a/src/utils/rand.cpp b/src/utils/rand.cpp new file mode 100644 index 0000000..513dd2a --- /dev/null +++ b/src/utils/rand.cpp @@ -0,0 +1,15 @@ +#include +#include + +using namespace std; + +// no idea how it does it. gets a random {x,y} pair within a range +pair get_random_position(int x_min, int x_max, int y_min, int y_max) { + static random_device rd; + static mt19937 gen(rd()); + + uniform_int_distribution<> distrib_x(x_min, x_max); + uniform_int_distribution<> distrib_y(y_min, y_max); + + return {distrib_x(gen), distrib_y(gen)}; +}