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.
This commit is contained in:
karl 2020-10-03 18:05:55 +02:00
parent 3fb23200fb
commit 074ec20e1b
6 changed files with 35 additions and 16 deletions

View File

@ -13,6 +13,8 @@ struct MouseLook {
double yaw = 0.0;
double pitch = 0.0;
glm::quat rotation;
bool is_active;
};

View File

@ -7,23 +7,33 @@
struct PathMove {
struct Path {
Path(std::vector<glm::vec3> points) : points(points) {}
Path(std::vector<glm::vec3> 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<glm::vec3> points;
std::vector<float> distances;
};
struct Views {
Views(std::vector<glm::quat> views) : views(views) {}
void add_view(glm::quat new_quat) {
views.emplace_back(new_quat);
}
std::vector<glm::quat> 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<float> distances;
};
#endif // __PATHMOVE_H__

View File

@ -14,7 +14,7 @@
using namespace ECS;
class PathMovementSwitchSystem : public EntitySystem, public EventSubscriber<InputEvent> {
class InteractivePathSystem : public EntitySystem, public EventSubscriber<InputEvent> {
void configure(World *pWorld) override {
myWorld = pWorld;
@ -37,6 +37,14 @@ class PathMovementSwitchSystem : public EntitySystem, public EventSubscriber<Inp
}
}
});
} else if (event.key == GLFW_KEY_R) {
myWorld->each<PathMove, Movement, MouseLook, Transform>([&](Entity *ent, ComponentHandle<PathMove> pathmove, ComponentHandle<Movement> movement, ComponentHandle<MouseLook> mouselook, ComponentHandle<Transform> transform) {
if (event.action == GLFW_PRESS) {
// Add this point to the path
pathmove->path.add_point(transform->origin);
pathmove->views.add_view(mouselook->rotation);
}
});
}
}

View File

@ -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);
});
}

View File

@ -78,7 +78,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
pWorld->each<Transform, PathMove>(
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<PathMove> 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<InputEvent> {
// 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();

View File

@ -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);