From 074ec20e1b7b2c36a0290f18855f0f09a92ebe4d Mon Sep 17 00:00:00 2001 From: karl Date: Sat, 3 Oct 2020 18:05:55 +0200 Subject: [PATCH] Add interactive path editing The PathMovementSwitchSystem was repurposed to a general InteractivePathSystem for this. It handles switching and adding points, as both seem like sensible possibilities for such entities to have. --- ECS/Components/MouseLook.h | 2 ++ ECS/Components/PathMove.h | 25 +++++++++++++------ ...SwitchSystem.h => InteractivePathSystem.h} | 10 +++++++- ECS/Systems/MouseLookSystem.h | 6 ++--- ECS/Systems/PathMoveSystem.h | 4 +-- main.cpp | 4 +-- 6 files changed, 35 insertions(+), 16 deletions(-) rename ECS/Systems/{PathMovementSwitchSystem.h => InteractivePathSystem.h} (69%) diff --git a/ECS/Components/MouseLook.h b/ECS/Components/MouseLook.h index 5749413..3b079c8 100644 --- a/ECS/Components/MouseLook.h +++ b/ECS/Components/MouseLook.h @@ -13,6 +13,8 @@ struct MouseLook { double yaw = 0.0; double pitch = 0.0; + glm::quat rotation; + bool is_active; }; diff --git a/ECS/Components/PathMove.h b/ECS/Components/PathMove.h index d8dfa42..aa17150 100644 --- a/ECS/Components/PathMove.h +++ b/ECS/Components/PathMove.h @@ -7,23 +7,33 @@ struct PathMove { struct Path { - Path(std::vector points) : points(points) {} + Path(std::vector points) : points(points) { + // Calculate distances + for (int i = 0; i < points.size() - 1; i++) { + distances.emplace_back(glm::distance(points[i], points[i + 1])); + } + } + + void add_point(glm::vec3 new_point) { + distances.emplace_back(glm::distance(points.back(), new_point)); + points.emplace_back(new_point); + } std::vector points; + std::vector distances; }; struct Views { Views(std::vector views) : views(views) {} + void add_view(glm::quat new_quat) { + views.emplace_back(new_quat); + } + std::vector views; }; - PathMove(double speed, Path path, Views views) : speed(speed), path(path), views(views) { - // Calculate distances - for (int i = 0; i < path.points.size() - 1; i++) { - distances.emplace_back(glm::distance(path.points[i], path.points[i + 1])); - } - } + PathMove(double speed, Path path, Views views) : speed(speed), path(path), views(views) {} bool is_active; double speed; @@ -34,6 +44,5 @@ struct PathMove { Path path; Views views; - std::vector distances; }; #endif // __PATHMOVE_H__ \ No newline at end of file diff --git a/ECS/Systems/PathMovementSwitchSystem.h b/ECS/Systems/InteractivePathSystem.h similarity index 69% rename from ECS/Systems/PathMovementSwitchSystem.h rename to ECS/Systems/InteractivePathSystem.h index e5da081..dd486db 100644 --- a/ECS/Systems/PathMovementSwitchSystem.h +++ b/ECS/Systems/InteractivePathSystem.h @@ -14,7 +14,7 @@ using namespace ECS; -class PathMovementSwitchSystem : public EntitySystem, public EventSubscriber { +class InteractivePathSystem : public EntitySystem, public EventSubscriber { void configure(World *pWorld) override { myWorld = pWorld; @@ -37,6 +37,14 @@ class PathMovementSwitchSystem : public EntitySystem, public EventSubscribereach([&](Entity *ent, ComponentHandle pathmove, ComponentHandle movement, ComponentHandle mouselook, ComponentHandle transform) { + if (event.action == GLFW_PRESS) { + // Add this point to the path + pathmove->path.add_point(transform->origin); + pathmove->views.add_view(mouselook->rotation); + } + }); } } diff --git a/ECS/Systems/MouseLookSystem.h b/ECS/Systems/MouseLookSystem.h index ca4f3fc..94a10b6 100644 --- a/ECS/Systems/MouseLookSystem.h +++ b/ECS/Systems/MouseLookSystem.h @@ -48,10 +48,10 @@ public: if(mouse->pitch < -80.0f) mouse->pitch = -80.0f; - glm::quat rotation = glm::angleAxis(glm::radians((float)mouse->yaw), glm::vec3(0.f, 1.f, 0.f)); - rotation *= glm::angleAxis(glm::radians((float)mouse->pitch), glm::vec3(1.f, 0.f, 0.f)); + mouse->rotation = glm::angleAxis(glm::radians((float)mouse->yaw), glm::vec3(0.f, 1.f, 0.f)); + mouse->rotation *= glm::angleAxis(glm::radians((float)mouse->pitch), glm::vec3(1.f, 0.f, 0.f)); - transform->set_rotation_from_quat(rotation); + transform->set_rotation_from_quat(mouse->rotation); }); } diff --git a/ECS/Systems/PathMoveSystem.h b/ECS/Systems/PathMoveSystem.h index 273bc10..3097ab8 100644 --- a/ECS/Systems/PathMoveSystem.h +++ b/ECS/Systems/PathMoveSystem.h @@ -78,7 +78,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber { pWorld->each( [&](Entity *ent, ComponentHandle transform, ComponentHandle pathmove) { if (!pathmove->is_active) return; - + // Handle change in speed pathmove->speed += pathmove->speed_addition * deltaTime; pathmove->speed = glm::clamp(pathmove->speed, 0.0, 10.0); @@ -88,7 +88,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber { // Add the passed time float desired_distance = deltaTime * pathmove->speed; // TODO - pathmove->time_passed += desired_distance / pathmove->distances[pathmove->current_point_index]; + pathmove->time_passed += desired_distance / path.distances[pathmove->current_point_index]; // Shorthand for number of points in the path int num_points = path.points.size(); diff --git a/main.cpp b/main.cpp index 4086ea4..16344ef 100644 --- a/main.cpp +++ b/main.cpp @@ -19,7 +19,7 @@ #include "ECS/Systems/SineAnimationSystem.h" #include "ECS/Components/DirectionalLight.h" #include "ECS/Components/PathMove.h" -#include "ECS/Systems/PathMovementSwitchSystem.h" +#include "ECS/Systems/InteractivePathSystem.h" using namespace ECS; @@ -83,7 +83,7 @@ int main() { world->registerSystem(new MouseLookSystem(1280, 720)); world->registerSystem(new PathMoveSystem()); world->registerSystem(new SineAnimationSystem()); - world->registerSystem(new PathMovementSwitchSystem()); + world->registerSystem(new InteractivePathSystem()); RenderSystem* renderSystem = new RenderSystem(); world->registerSystem(renderSystem);