Skip to content

Commit

Permalink
begin debug of rope system
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Nov 11, 2024
1 parent 1e36caf commit 5ea8510
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
11 changes: 11 additions & 0 deletions samples/golf/src/golf/MenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ source distribution.
#include "Clubs.hpp"
#include "HoleData.hpp"
#include "League.hpp"
#include "RopeSystem.hpp"
#include "../ErrorCheck.hpp"

#include <Achievements.hpp>
Expand Down Expand Up @@ -1558,6 +1559,7 @@ void MenuState::addSystems()
{
auto& mb = getContext().appInstance.getMessageBus();

m_backgroundScene.addSystem<RopeSystem>(mb);
m_backgroundScene.addSystem<GolfCartSystem>(mb);
m_backgroundScene.addSystem<CloudSystem>(mb)->setWindVector(glm::vec3(0.25f));
m_backgroundScene.addSystem<cro::CallbackSystem>(mb);
Expand Down Expand Up @@ -1758,6 +1760,15 @@ void MenuState::loadAssets()

void MenuState::createScene()
{
auto rope = m_backgroundScene.getSystem<RopeSystem>()->addRope(glm::vec3(-7.f, 3.f, 10.f), glm::vec3(7.f, 3.f, 10.f));
for (auto i = 0; i < 4; ++i)
{
auto entity = m_backgroundScene.createEntity();
entity.addComponent<cro::Transform>();
entity.addComponent<RopeNode>().ropeID = rope;
}


m_backgroundScene.enableSkybox();

cro::AudioMixer::setPrefadeVolume(0.f, MixerChannel::Music);
Expand Down
48 changes: 47 additions & 1 deletion samples/golf/src/golf/RopeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ source distribution.
#include <crogine/ecs/Scene.hpp>
#include <crogine/ecs/components/Transform.hpp>

#include <crogine/gui/Gui.hpp>

namespace
{
constexpr glm::vec3 Gravity(0.f, -9.f, 0.f);
Expand All @@ -58,6 +60,18 @@ std::size_t RopeSystem::addRope(glm::vec3 start, glm::vec3 end, float slack)
auto ret = m_ropes.size();
m_ropes.emplace_back(start, end, slack, *getScene());

registerWindow([&]()
{
ImGui::Begin("sdfsd");
for (auto n : m_ropes.back().m_nodes)
{
const auto& node = n.getComponent<RopeNode>();
ImGui::Text("Pos: %3.2f, %3.2f, %3.2f", node.position.x, node.position.y, node.position.z);
ImGui::Text("Fixed: %s", node.fixed ? "true" : "false");
}
ImGui::End();
});

return ret;
}

Expand All @@ -79,7 +93,7 @@ void RopeSystem::onEntityAdded(cro::Entity e)
void RopeSystem::onEntityRemoved(cro::Entity e)
{
const auto& node = e.getComponent<RopeNode>();
CRO_ASSERT(node.ropeID < m_ropes.size());
CRO_ASSERT(node.ropeID < m_ropes.size(), "");

m_ropes[node.ropeID].removeNode(e);
}
Expand Down Expand Up @@ -189,4 +203,36 @@ void Rope::integrate(float dt)
void Rope::constrain()
{
static constexpr std::size_t MaxIterations = 30;
for (auto i = 0u; i < MaxIterations; ++i)
{
for (auto j = 1u; j < m_nodes.size(); ++j)
{
auto& n1 = m_nodes[j - 1].getComponent<RopeNode>();
auto& n2 = m_nodes[j].getComponent<RopeNode>();

auto dir = n2.position - n1.position;
const float dist = glm::length(dir);
const float error = dist - m_nodeSpacing;

dir /= dist;
dir *= error;

if (n1.fixed && !n2.fixed)
{
n2.position -= dir;
}
else if (n2.fixed && !n1.fixed)
{
n1.position += dir;
}
else
{
//we should never have 2 next to each other fixed as
//we don't call this with fewer than 3 nodes
dir *= 0.5f;
n2.position -= dir;
n1.position += dir;
}
}
}
}
7 changes: 5 additions & 2 deletions samples/golf/src/golf/RopeSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ source distribution.
#pragma once

#include <crogine/ecs/System.hpp>
#include <crogine/gui/GuiClient.hpp>

//component on an entity, used to
//update the entity final position
Expand Down Expand Up @@ -60,7 +61,7 @@ class Rope final

void addNode(cro::Entity);

//TODO it's possible to destroy entities outside
//it's possible to destroy entities outside
//of this system - this func just tidies up loose
//references - I have no idea how it'll behave though...
void removeNode(cro::Entity);
Expand All @@ -76,9 +77,11 @@ class Rope final

void integrate(float dt);
void constrain();

friend class RopeSystem; //TODO remove this when done debugging
};

class RopeSystem final : public cro::System
class RopeSystem final : public cro::System, public cro::GuiClient
{
public:
explicit RopeSystem(cro::MessageBus&);
Expand Down

0 comments on commit 5ea8510

Please sign in to comment.