implement threaded physics (decoupled from rendering thread) - not yet integrated
This commit is contained in:
10
src/main.cpp
10
src/main.cpp
@ -1,3 +1,7 @@
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <tracy/Tracy.hpp>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "input.hpp"
|
||||
#include "physics.hpp"
|
||||
@ -5,10 +9,6 @@
|
||||
#include "state.hpp"
|
||||
#include "tracy.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <tracy/Tracy.hpp>
|
||||
|
||||
// TODO: Klotski state file loading
|
||||
// - File should contain a single state per line, multiple lines possible
|
||||
// - If a file is loaded, the presets should be replaced with the states
|
||||
@ -18,7 +18,7 @@
|
||||
// TODO: Graph interaction
|
||||
// - Click states to display them in the board
|
||||
// - Find shortest path to any winning state and mark it in the graph
|
||||
// - Also mark the next move along the path on the board
|
||||
// - Also mark the next move along the path on the board
|
||||
|
||||
auto main(int argc, char *argv[]) -> int {
|
||||
// if (argc < 2) {
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include <algorithm>
|
||||
#include <cfloat>
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <tracy/Tracy.hpp>
|
||||
@ -69,8 +70,7 @@ auto Spring::CalculateSpringForce(Mass &_mass_a, Mass &_mass_b) const -> void {
|
||||
_mass_b.force = Vector3Add(_mass_b.force, force_b);
|
||||
}
|
||||
|
||||
auto MassSpringSystem::AddMass(float mass, bool fixed, const State &state)
|
||||
-> void {
|
||||
auto MassSpringSystem::AddMass(const State &state) -> void {
|
||||
if (!state_masses.contains(state)) {
|
||||
masses.emplace_back(Vector3Zero());
|
||||
std::size_t idx = masses.size() - 1;
|
||||
@ -86,9 +86,7 @@ auto MassSpringSystem::GetMass(const State &state) const -> const Mass & {
|
||||
return masses.at(state_masses.at(state));
|
||||
}
|
||||
|
||||
auto MassSpringSystem::AddSpring(const State &state_a, const State &state_b,
|
||||
float spring_constant,
|
||||
float dampening_constant, float rest_length)
|
||||
auto MassSpringSystem::AddSpring(const State &state_a, const State &state_b)
|
||||
-> void {
|
||||
std::pair<State, State> key = std::make_pair(state_a, state_b);
|
||||
if (!state_springs.contains(key)) {
|
||||
@ -340,3 +338,46 @@ auto MassSpringSystem::InvalidateGrid() -> void {
|
||||
last_springs_count = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
|
||||
-> void {
|
||||
MassSpringSystem mass_springs;
|
||||
|
||||
const auto visitor = overloads{
|
||||
[&](const AddMass &am) { mass_springs.AddMass(am.s); },
|
||||
[&](const AddSpring &as) { mass_springs.AddSpring(as.a, as.b); },
|
||||
[&](const ClearGraph &cg) { mass_springs.Clear(); },
|
||||
};
|
||||
|
||||
while (state.running) {
|
||||
// Handle queued commands
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(state.command_mtx);
|
||||
while (!state.pending_commands.empty()) {
|
||||
Command &cmd = state.pending_commands.front();
|
||||
cmd.visit(visitor);
|
||||
state.pending_commands.pop();
|
||||
}
|
||||
}
|
||||
|
||||
if (mass_springs.masses.empty()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Physics update
|
||||
mass_springs.ClearForces();
|
||||
mass_springs.CalculateSpringForces();
|
||||
mass_springs.CalculateRepulsionForces();
|
||||
mass_springs.VerletUpdate(TIMESTEP * SIM_SPEED);
|
||||
|
||||
// Publish the positions for the renderer (copy)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(state.pos_mtx);
|
||||
state.masses = mass_springs.masses;
|
||||
state.state_masses = mass_springs.state_masses;
|
||||
state.springs = mass_springs.springs;
|
||||
state.state_springs = mass_springs.state_springs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user