build octree async and reuse tree from last frame (disabled as it breaks physics)

This commit is contained in:
2026-03-02 18:52:06 +01:00
parent d62d5c78bf
commit 9c48954a78
13 changed files with 329 additions and 273 deletions

View File

@ -5,6 +5,22 @@
#define THREADPOOL // Enable physics threadpool
#ifdef THREADPOOL
#if defined(_WIN32)
#define NOGDI // All GDI defines and routines
#define NOUSER // All USER defines and routines
#endif
#define BS_THREAD_POOL_NATIVE_EXTENSIONS
#include <BS_thread_pool.hpp>
#if defined(_WIN32) // raylib uses these names as function parameters
#undef near
#undef far
#endif
#endif
// TODO: Using the octree from the last frame completely breaks the physics :/
// #define ASYNC_OCTREE
// Gets set by CMake
// #define BACKWARD // Enable pretty stack traces
// #define TRACY // Enable tracy profiling support

View File

@ -2,49 +2,13 @@
#define MASS_SPRING_SYSTEM_HPP_
#include "octree.hpp"
#include "util.hpp"
#include "config.hpp"
#include <raylib.h>
#include <raymath.h>
#ifdef THREADPOOL
#if defined(_WIN32)
#define NOGDI // All GDI defines and routines
#define NOUSER // All USER defines and routines
#endif
#define BS_THREAD_POOL_NATIVE_EXTENSIONS
#include <BS_thread_pool.hpp>
#if defined(_WIN32) // raylib uses these names as function parameters
#undef near
#undef far
#endif
#endif
class mass_spring_system
{
public:
class mass
{
public:
Vector3 position = Vector3Zero();
Vector3 previous_position = Vector3Zero(); // for verlet integration
Vector3 velocity = Vector3Zero();
Vector3 force = Vector3Zero();
public:
mass() = delete;
explicit mass(const Vector3 _position)
: position(_position), previous_position(_position) {}
public:
auto clear_force() -> void;
auto calculate_velocity(float delta_time) -> void;
auto calculate_position(float delta_time) -> void;
auto verlet_update(float delta_time) -> void;
};
class spring
{
public:
@ -54,9 +18,6 @@ public:
public:
spring(const size_t _a, const size_t _b)
: a(_a), b(_b) {}
public:
static auto calculate_spring_force(mass& _a, mass& _b) -> void;
};
private:
@ -65,26 +26,25 @@ private:
#endif
public:
static constexpr int SMALL_TASK_BLOCK_SIZE = 256;
static constexpr int LARGE_TASK_BLOCK_SIZE = 256;
octree tree;
// This is the main ownership of all the states/masses/springs.
std::vector<mass> masses;
std::vector<Vector3> positions;
std::vector<Vector3> previous_positions; // for verlet integration
std::vector<Vector3> velocities;
std::vector<Vector3> forces;
std::vector<spring> springs;
public:
mass_spring_system()
#ifdef THREADPOOL
: threads(std::thread::hardware_concurrency() - 1, set_thread_name)
: threads(std::thread::hardware_concurrency() - 2, set_mass_springs_pool_thread_name)
#endif
{
infoln("Using Barnes-Hut + Octree repulsion force calculation.");
#ifdef THREADPOOL
infoln("Thread-pool: {} threads.", threads.get_thread_count());
#else
infoln("Thread-pool: Disabled.");
#endif
}
{}
mass_spring_system(const mass_spring_system& copy) = delete;
auto operator=(const mass_spring_system& copy) -> mass_spring_system& = delete;
@ -93,20 +53,22 @@ public:
private:
#ifdef THREADPOOL
static auto set_thread_name(size_t idx) -> void;
static auto set_mass_springs_pool_thread_name(size_t idx) -> void;
#endif
auto build_octree() -> void;
public:
auto clear() -> void;
auto add_mass() -> void;
auto add_spring(size_t a, size_t b) -> void;
auto clear_forces() -> void;
auto calculate_spring_force(size_t s) -> void;
auto calculate_spring_forces() -> void;
auto calculate_repulsion_forces() -> void;
auto verlet_update(float delta_time) -> void;
auto integrate_velocity(size_t m, float dt) -> void;
auto integrate_position(size_t m, float dt) -> void;
auto verlet_update(size_t m, float dt) -> void;
auto update(float dt) -> void;
auto center_masses() -> void;
};

View File

@ -31,22 +31,20 @@ public:
public:
octree() = default;
octree(const octree& copy) = delete;
auto operator=(const octree& copy) -> octree& = delete;
octree(octree&& move) = delete;
auto operator=(octree&& move) -> octree& = delete;
// octree(const octree& copy) = delete;
// auto operator=(const octree& copy) -> octree& = delete;
// octree(octree&& move) = delete;
// auto operator=(octree&& move) -> octree& = delete;
public:
auto create_empty_leaf(const Vector3& box_min, const Vector3& box_max) -> int;
[[nodiscard]] auto get_octant(int node_idx, const Vector3& pos) const -> int;
[[nodiscard]] auto get_child_bounds(int node_idx, int octant) const
-> std::pair<Vector3, Vector3>;
auto create_empty_leaf(const Vector3& box_min, const Vector3& box_max) -> int;
auto insert(int node_idx, int mass_id, const Vector3& pos, float mass, int depth) -> void;
static auto build_octree(octree& t, const std::vector<Vector3>& positions) -> void;
[[nodiscard]] auto calculate_force(int node_idx, const Vector3& pos) const -> Vector3;
};
#endif
#endif

View File

@ -1,6 +1,8 @@
#ifndef PHYSICS_HPP_
#define PHYSICS_HPP_
#include "config.hpp"
#include <atomic>
#include <condition_variable>
#include <mutex>
@ -80,15 +82,16 @@ public:
}
private:
#ifdef ASYNC_OCTREE
static auto set_octree_pool_thread_name(size_t idx) -> void;
#endif
static auto physics_thread(physics_state& state) -> void;
public:
auto add_mass_cmd() -> void;
auto add_spring_cmd(size_t a, size_t b) -> void;
auto clear_cmd() -> void;
auto add_mass_cmd() -> void;
auto add_spring_cmd(size_t a, size_t b) -> void;
auto add_mass_springs_cmd(size_t num_masses, const std::vector<std::pair<size_t, size_t>>& springs) -> void;
};

View File

@ -140,6 +140,13 @@ inline auto ansi_reset() -> std::string
}
// std::println doesn't work with mingw
template <typename... Args>
auto traceln(std::format_string<Args...> fmt, Args&&... args) -> void
{
std::cout << std::format("[{}TRACE{}]: ", ansi_bold_fg(fg_cyan), ansi_reset()) << std::format(
fmt, std::forward<Args>(args)...) << std::endl;
}
template <typename... Args>
auto infoln(std::format_string<Args...> fmt, Args&&... args) -> void
{