From cf19af1142a995eb9e9a7d6265c4b6e5d0ad714c Mon Sep 17 00:00:00 2001 From: karl Date: Sat, 3 Oct 2020 00:56:49 +0200 Subject: [PATCH] Attempt at constant speed Not working because linearly increasing t doesn't imply constant velocity within a segment between two points. Seems like a more complex approach will be necessary --- ECS/Components/PathMove.h | 8 +++++++- ECS/Systems/PathMoveSystem.h | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ECS/Components/PathMove.h b/ECS/Components/PathMove.h index b22fb50..22fab58 100644 --- a/ECS/Components/PathMove.h +++ b/ECS/Components/PathMove.h @@ -18,7 +18,12 @@ struct PathMove { std::vector views; }; - PathMove(double speed, Path path, Views views) : speed(speed), path(path), views(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])); + } + } double speed; float time_passed = 0.0; @@ -28,5 +33,6 @@ struct PathMove { Path path; Views views; + std::vector distances; }; #endif // __PATHMOVE_H__ \ No newline at end of file diff --git a/ECS/Systems/PathMoveSystem.h b/ECS/Systems/PathMoveSystem.h index fa90ade..5a42a8f 100644 --- a/ECS/Systems/PathMoveSystem.h +++ b/ECS/Systems/PathMoveSystem.h @@ -85,7 +85,8 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber { PathMove::Path path = pathmove->path; // Add the passed time - pathmove->time_passed += deltaTime; + float desired_distance = deltaTime * pathmove->speed; // TODO + pathmove->time_passed += desired_distance / pathmove->distances[pathmove->current_point_index]; // Shorthand for number of points in the path int num_points = path.points.size(); @@ -171,6 +172,8 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber { // Apply transform->set_rotation_from_quat(result); + + std::cout << pathmove->time_passed << std::endl; }); }