feat: bodies attract

This commit is contained in:
2025-11-19 18:45:25 +01:00
parent a5add0765f
commit b33e9b37a2
5 changed files with 79 additions and 10 deletions

View File

@@ -1,21 +1,23 @@
#ifndef SIMS_HPP
#define SIMS_HPP
#include "utils.hpp"
#include <SDL3/SDL_pixels.h>
#include <SDL3/SDL_render.h>
#include <utility>
class Body {
private:
std::pair<float, float> old_positions[200];
public:
float x;
float y;
float vx = 1;
float vy = 1;
std::pair<float, float> 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

13
include/utils.hpp Normal file
View File

@@ -0,0 +1,13 @@
#ifndef UTILS_H
#define UTILS_H
#include <utility>
// constants
const long double EARTH_MASS = 5.972e24;
const long double G = 6.67430e-11L;
std::pair<int, int> get_random_position(int x_min, int x_max, int y_min, int y_max);
#endif

View File

@@ -2,6 +2,7 @@
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_video.h>
#include <cstdio>
#include <cstdlib>
#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;

View File

@@ -3,6 +3,7 @@
#include <SDL3/SDL_render.h>
#include "../../include/sims.hpp"
#include "../../include/gfx.hpp"
#include "../../include/utils.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
@@ -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<int, int> 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();
}

15
src/utils/rand.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include <random>
#include <utility>
using namespace std;
// no idea how it does it. gets a random {x,y} pair within a range
pair<int, int> 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)};
}