implement barnes-hut particle repulsion using octree

This commit is contained in:
2026-02-22 23:29:56 +01:00
parent f07f2772c4
commit e43e505110
5 changed files with 302 additions and 8 deletions

View File

@ -5,6 +5,7 @@
#define PRINT_TIMINGS
// #define WEB
#define BARNES_HUT
// Window
constexpr int INITIAL_WIDTH = 800;
@ -35,8 +36,10 @@ constexpr float MASS = 1.0;
constexpr float SPRING_CONSTANT = 5.0;
constexpr float DAMPENING_CONSTANT = 1.0;
constexpr float REST_LENGTH = 2.0;
constexpr float REPULSION_FORCE = 0.1;
constexpr float REPULSION_FORCE = 2.0;
constexpr float REPULSION_RANGE = 5.0 * REST_LENGTH;
constexpr float THETA = 1.0; // Barnes-Hut [0.5, ~]
constexpr float SOFTENING = 0.01; // Barnes-Hut [0.01, 1.0]
constexpr int REPULSION_GRID_REFRESH = 5; // Updates between grid rebuilds
constexpr float VERLET_DAMPENING = 0.05; // [0, 1]

53
include/octree.hpp Normal file
View File

@ -0,0 +1,53 @@
#ifndef __OCTREE_HPP_
#define __OCTREE_HPP_
#include <raylib.h>
#include <raymath.h>
#include <vector>
class OctreeNode {
public:
Vector3 mass_center;
float mass_total;
Vector3 box_min; // area start
Vector3 box_max; // area end
int children[8];
int mass_id;
bool leaf;
public:
OctreeNode()
: mass_center(Vector3Zero()), mass_total(0.0),
children(-1, -1, -1, -1, -1, -1, -1, -1), mass_id(-1), leaf(true) {}
~OctreeNode() {}
};
class Octree {
public:
std::vector<OctreeNode> nodes;
public:
Octree() {}
Octree(const Octree &copy) = delete;
Octree &operator=(const Octree &copy) = delete;
Octree(Octree &&move) = delete;
Octree &operator=(Octree &&move) = delete;
~Octree() {}
public:
auto CreateNode(const Vector3 &box_min, const Vector3 &box_max) -> int;
auto GetOctant(int node_idx, const Vector3 &pos) -> int;
auto GetChildBounds(int node_idx, int octant) -> std::pair<Vector3, Vector3>;
auto Insert(int node_idx, int mass_id, const Vector3 &pos, float mass)
-> void;
auto CalculateForce(int node_idx, const Vector3 &pos) -> Vector3;
};
#endif

View File

@ -9,6 +9,10 @@
#include "config.hpp"
#include "puzzle.hpp"
#ifdef BARNES_HUT
#include "octree.hpp"
#endif
class Mass {
public:
const float mass;
@ -53,22 +57,32 @@ public:
class MassSpringSystem {
private:
// Uniform grid
std::vector<Mass *> mass_pointers;
#ifdef BARNES_HUT
// Barnes-Hut
Octree octree;
#else
// Uniform grid
std::vector<int> mass_indices;
std::vector<int64_t> cell_ids;
int last_build;
int last_masses_count;
int last_springs_count;
#endif
public:
// This is the main ownership of all the states/masses/springs.
// Everything is stored multiple times but idc.
// TODO: Everything is stored multiple times but idc (currently).
std::unordered_map<State, Mass> masses;
std::unordered_map<std::pair<State, State>, Spring> springs;
public:
MassSpringSystem() : last_build(REPULSION_GRID_REFRESH) {};
MassSpringSystem() {
#ifndef BARNES_HUT
last_build = REPULSION_GRID_REFRESH;
#endif
};
MassSpringSystem(const MassSpringSystem &copy) = delete;
MassSpringSystem &operator=(const MassSpringSystem &copy) = delete;
@ -78,7 +92,11 @@ public:
~MassSpringSystem() {};
private:
#ifdef BARNES_HUT
auto BuildOctree() -> void;
#else
auto BuildUniformGrid() -> void;
#endif
public:
auto AddMass(float mass, bool fixed, const State &state) -> void;
@ -100,7 +118,9 @@ public:
auto VerletUpdate(float delta_time) -> void;
#ifndef BARNES_HUT
auto InvalidateGrid() -> void;
#endif
};
#endif