diff --git a/ECS/Components/PathMove.h b/ECS/Components/PathMove.h index dc0a1dc..fec3f79 100644 --- a/ECS/Components/PathMove.h +++ b/ECS/Components/PathMove.h @@ -2,23 +2,29 @@ #define __PATHMOVE_H__ #include +#include #include struct PathMove { struct Path { Path(std::vector points) : points(points) {} - + std::vector points; }; - PathMove(double speed, Path path) : speed(speed), path(path) {} + struct Views { + Views(std::vector views) : views(views) {} + + std::vector views; + }; + + PathMove(double speed, Path path, Views views) : speed(speed), path(path), views(views) {} double speed; - - Path path; - + float time_passed = 0.0; int current_point_index = 0; - float time_passed = 0.0; + Path path; + Views views; }; #endif // __PATHMOVE_H__ \ No newline at end of file diff --git a/ECS/Components/Transform.h b/ECS/Components/Transform.h index a643027..71a0902 100644 --- a/ECS/Components/Transform.h +++ b/ECS/Components/Transform.h @@ -9,6 +9,8 @@ #include #include +#include "glm/gtx/string_cast.hpp" + struct Transform { Transform() = default; @@ -35,6 +37,16 @@ struct Transform { translate(-difference); } + void set_rotation_from_quat(glm::quat quaternion) { + // Remember translation + glm::vec4 save = matrix[3]; + + matrix = glm::mat4_cast(quaternion); + matrix[3] = save; + + std::cout << glm::to_string(matrix) << std::endl; + } + glm::vec3 getPosition() const { return matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); } diff --git a/ECS/Systems/PathMoveSystem.h b/ECS/Systems/PathMoveSystem.h index c856e5b..8f66bf6 100644 --- a/ECS/Systems/PathMoveSystem.h +++ b/ECS/Systems/PathMoveSystem.h @@ -137,6 +137,11 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber { // Apply transform->set_position(point); + // Rotation + // https://www.3dgep.com/understanding-quaternions/#SQUAD + PathMove::Views views = pathmove->views; + transform->set_rotation_from_quat(views.views[pathmove->current_point_index]); + // FIXME: Debugging output std::cout << point.x << ", " << point.y << ", " << point.z << std::endl; }); diff --git a/main.cpp b/main.cpp index a260283..f58d332 100644 --- a/main.cpp +++ b/main.cpp @@ -93,13 +93,23 @@ int main() { player->assign(70.0f, 1280, 720, 0.1f, 100.0f); player->assign(10.0, PathMove::Path(std::vector{ glm::vec3(0.0, 2.0, 0.0), - glm::vec3(0.0, 2.0, 1.0), - glm::vec3(2.0, 2.0, 2.0), - glm::vec3(1.0, 3.0, 3.0), - glm::vec3(-2.0, 2.0, 4.0), - glm::vec3(2.0, 2.0, 4.0), - glm::vec3(0.0, 2.0, 10.0) - })); + glm::vec3(0.0, 2.0, -1.0), + glm::vec3(2.0, 2.0, -2.0), + glm::vec3(1.0, 3.0, -3.0), + glm::vec3(-2.0, 2.0, -4.0), + glm::vec3(2.0, 2.0, -4.0), + glm::vec3(0.0, 2.0, -10.0) + }), + PathMove::Views(std::vector{ + glm::quat(0.0, 0.0, 0.0, 0.0), + glm::angleAxis(glm::radians(10.f), glm::vec3(0.f, 1.f, 0.f)), + glm::angleAxis(glm::radians(30.f), glm::vec3(0.f, 1.f, 0.f)), + glm::angleAxis(glm::radians(40.f), glm::vec3(0.f, 1.f, 0.f)), + glm::angleAxis(glm::radians(50.f), glm::vec3(0.f, 1.f, 0.f)), + glm::angleAxis(glm::radians(70.f), glm::vec3(0.f, 1.f, 0.f)), + glm::angleAxis(glm::radians(80.f), glm::vec3(0.f, 1.f, 0.f)) + }) + ); player->get()->translate(glm::vec3(0.0f, 1.0f, 2.0f)); Entity *monkey = world->create();