From b84c5f141eefd1d116b2eab5682d10c221a4fbf6 Mon Sep 17 00:00:00 2001 From: Yggdrasil75 Date: Tue, 3 Feb 2026 14:32:37 -0500 Subject: [PATCH] gonna work on this more later. adding a fluid sim, gonna use it to create the planet by spawning in heavy particles and then slowly lighter and smaller over time until a sphere is formed. --- util/sim/fluidsim.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 util/sim/fluidsim.cpp diff --git a/util/sim/fluidsim.cpp b/util/sim/fluidsim.cpp new file mode 100644 index 0000000..d016746 --- /dev/null +++ b/util/sim/fluidsim.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../util/grid/grid3eigen.hpp" +#include "../util/output/bmpwriter.hpp" +#include "../util/output/frame.hpp" +#include "../util/timing_decorator.cpp" +#include "../util/noise/pnoise2.hpp" +#include "../util/noise/pnoise.cpp" +#include "../util/output/aviwriter.hpp" + +#include "../imgui/imgui.h" +#include "../imgui/backends/imgui_impl_glfw.h" +#include "../imgui/backends/imgui_impl_opengl3.h" +#include +#include "../stb/stb_image.h" + + +struct fluidParticle { + Eigen::Matrix pos; + Eigen::Matrix velocity; + Eigen::Matrix acceleration; + Eigen::Matrix forceAccumulator; + float pressure; + float viscosity; + float restitution; + float mass; +}; + +struct gridDefaults { + float gridSizeCube = 1024; +}; + +Eigen::Matrix posGen() { + static std::random_device rd; + static std::mt19937 gen(rd()); + static std::normal_distribution dist(0.0f, 1024.0f); + + return Eigen::Matrix( + dist(gen), + dist(gen), + dist(gen) + ); +} +class fluidSim { + std::unordered_map> idposMap; + Octree grid; + + void spawnParticles(fluidParticle toSpawn, int count) { + for (int i = 0; i < count; i++) { + Eigen::Matrix pos = posGen(); + grid.set(toSpawn, pos, true, {toSpawn.mass / 1000, toSpawn.mass / 1000, toSpawn.mass / 1000}, 1, true, 1, false); + + } + } + + void applyPhysics() { + for (auto& meh : idposMap) { + std::shared_ptr::NodeData> mehdata = grid.find(meh.second); + std::vector::NodeData>> neighbors = grid.findInRadius(meh.second, mehdata->data.mass); + ///TODO: + // apply velocity to have particle move + // use mass of neighboring particles to change direction towards them + // ressure pushes particles away if they are too close + // resitution controls how much pressure builds up based on how close particles are + // have to find a way to have a clump use their combined mass instead of individual mass. + //if a particle leaves the grid, destroy it and spawn a new one + + for (auto& neighbor : neighbors) { + + + } + } + } +}; \ No newline at end of file