From 35de0dd164f21e81ec3f2d7d8ee354823526e63a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Wed, 14 Jun 2023 16:16:50 +0200
Subject: [PATCH] obj kb movement axis based, shortened lines in sim

---
 src/gfx/include/gfx/obj_control.hpp |  9 ++++++--
 src/gfx/src/obj_control.cpp         | 33 ++++++++++++-----------------
 src/studio/src/simulator.cpp        | 33 ++++++++++++++++++++---------
 3 files changed, 43 insertions(+), 32 deletions(-)

diff --git a/src/gfx/include/gfx/obj_control.hpp b/src/gfx/include/gfx/obj_control.hpp
index 5134a34..dfdc42c 100644
--- a/src/gfx/include/gfx/obj_control.hpp
+++ b/src/gfx/include/gfx/obj_control.hpp
@@ -17,9 +17,14 @@ class ObjectController : public Controller
 {
 public:
     using Controller::Controller;
+
+    glm::vec3 getFrontVec() const override { return glm::vec3(0.0f, 0.0f, -1.0f); }
+    glm::vec3 getUpVec() const override { return glm::vec3(0.0f, 1.0f, 0.0f); }
+
     void mouse_rotate(const osi::Mouse &mouse);
-    void mouse_horizontal_move(const glm::vec3 &rayPosition, const glm::vec3 &rayDirection, glm::vec3 &prev_plane_intersect, bool & object_moving);
-    void mouse_elevation_move(const glm::vec3 &rayPosition, const glm::vec3 &rayDirection, const glm::quat &cameraRotation, glm::vec3 &prev_plane_intersect, bool & object_moving);
+    glm::vec3 make_path(glm::vec3 &new_pos, glm::vec3 &prev_plane_intersect, bool &object_moving);
+    void mouse_horizontal_move(const glm::vec3 &rayPosition, const glm::vec3 &rayDirection, glm::vec3 &prev_plane_intersect, bool &object_moving);
+    void mouse_elevation_move(const glm::vec3 &rayPosition, const glm::vec3 &rayDirection, const glm::quat &cameraRotation, glm::vec3 &prev_plane_intersect, bool &object_moving);
 };
 
 
diff --git a/src/gfx/src/obj_control.cpp b/src/gfx/src/obj_control.cpp
index ca9b4a4..bd9c548 100644
--- a/src/gfx/src/obj_control.cpp
+++ b/src/gfx/src/obj_control.cpp
@@ -17,6 +17,16 @@ void ObjectController::mouse_rotate(const osi::Mouse &mouse)
     bound_frame->rotateRotationQuat(glm::angleAxis(xoffset * step, glm::vec3(0.0f, 1.0f, 0.0f)));
 }
 
+glm::vec3 ObjectController::make_path(glm::vec3 &new_pos, glm::vec3 &prev_plane_intersect, bool &object_moving)
+{
+    if (object_moving)
+        return new_pos - prev_plane_intersect;
+    
+    prev_plane_intersect = new_pos;
+    object_moving = true;
+    return glm::vec3(0.0f, 0.0f, 0.0f);
+}
+
 void ObjectController::mouse_horizontal_move(const glm::vec3 &rayPosition, const glm::vec3 &rayDirection, glm::vec3 &prev_plane_intersect, bool &object_moving)
 {
     auto selection_y_pos = bound_frame->getPosition().y;
@@ -34,16 +44,7 @@ void ObjectController::mouse_horizontal_move(const glm::vec3 &rayPosition, const
     
     glm::vec3 new_pos = rayPosition + rayDirection * t;
 
-    glm::vec3 path;
-    if (!object_moving)
-    {
-        prev_plane_intersect = new_pos;
-        object_moving = true;
-        path = glm::vec3(0.0f, 0.0f, 0.0f);
-    }
-    else
-        path = new_pos - prev_plane_intersect;
-
+    auto path = make_path(new_pos, prev_plane_intersect, object_moving);
     path.y = 0.0f;
 
     bound_frame->translatePosVec(path);
@@ -54,6 +55,7 @@ void ObjectController::mouse_horizontal_move(const glm::vec3 &rayPosition, const
 void ObjectController::mouse_elevation_move(const glm::vec3 &rayPosition, const glm::vec3 &rayDirection, const glm::quat &cameraRotation, glm::vec3 &prev_plane_intersect, bool &object_moving)
 {
     Frame atObjectVerticalPlane(bound_frame->getPosition(), cameraRotation);
+
     auto normal = glm::normalize(glm::cross(atObjectVerticalPlane.getRotationAxisX(), glm::vec3(0.0f, 1.0f, 0.0f)));
     auto d = glm::dot(normal, glm::vec3(0, 0, 0) - bound_frame->getPosition()) / glm::length(normal);
 
@@ -66,16 +68,7 @@ void ObjectController::mouse_elevation_move(const glm::vec3 &rayPosition, const
     
     glm::vec3 new_pos = rayPosition + rayDirection * t;
 
-    glm::vec3 path;
-    if (!object_moving)
-    {
-        prev_plane_intersect = new_pos;
-        object_moving = true;
-        path = glm::vec3(0.0f, 0.0f, 0.0f);
-    }
-    else
-        path = new_pos - prev_plane_intersect;
-
+    auto path = make_path(new_pos, prev_plane_intersect, object_moving);
     path.x = 0.0f;
     path.z = 0.0f;
 
diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp
index 56aeac6..4ee105f 100644
--- a/src/studio/src/simulator.cpp
+++ b/src/studio/src/simulator.cpp
@@ -201,7 +201,9 @@ void Simulator::showUI()
 {
     ImGui::SetNextWindowPos(ImVec2(0, 0));
     ImGui::SetNextWindowSize(ImVec2(ImGui::CalcTextSize("Select Move Rotate +").x * 1.5f, 80));
-    ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
+    ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoMove 
+                                      | ImGuiWindowFlags_NoResize 
+                                      | ImGuiWindowFlags_NoCollapse);
 	    ImGui::Text("Operation: %s", state_to_str().c_str());
 
         if (ImGui::Button("Select"))
@@ -338,7 +340,8 @@ void Simulator::process_input()
 
 void Simulator::add_ray(glm::vec3 rayPosition, glm::vec3 rayDirection)
 {
-    object_ptr ray = std::make_shared<Object>(Object{{0, 0, 0, rayDirection.x, rayDirection.y, rayDirection.z}, {0, 1}, {}, glm::vec3(0.5f, 1, 0.31f), "basic"});
+    object_ptr ray = std::make_shared<Object>(Object{{0, 0, 0, rayDirection.x, rayDirection.y, rayDirection.z},
+                                                     {0, 1}, {}, glm::vec3(0.5f, 1, 0.31f), "basic"});
     ray->setDrawMode(GL_LINES);
     ray->setSelectability(false);
 
@@ -357,7 +360,8 @@ void Simulator::add_selection_AABB(node_ptr node)
         if (!obj)
             continue;
         
-        Object obj_bbox(obj->getAABB().getVertices(), obj->getAABB().getIndices(), std::vector<float>{}, glm::vec3(0.5f, 1, 0.31f), "basic");
+        Object obj_bbox(obj->getAABB().getVertices(), obj->getAABB().getIndices(), 
+                        std::vector<float>{}, glm::vec3(0.5f, 1, 0.31f), "basic");
         obj_bbox.setSelectability(false);
         obj_bbox.setDrawMode(GL_LINES);
         scene.back()->addObject(std::make_shared<Object>(std::move(obj_bbox)));
@@ -374,7 +378,8 @@ void Simulator::add_pre_add_AABB(node_ptr node, Frame position)
         if (!obj)
             continue;
             
-        Object obj_bbox(obj->getAABB().getVertices(), obj->getAABB().getIndices(), std::vector<float>{}, glm::vec3(0.5f, 1, 0.31f), "basic");
+        Object obj_bbox(obj->getAABB().getVertices(), obj->getAABB().getIndices(), 
+                        std::vector<float>{}, glm::vec3(0.5f, 1, 0.31f), "basic");
         obj_bbox.setSelectability(false);
         obj_bbox.setDrawMode(GL_LINES);
         scene.back()->addObject(std::make_shared<Object>(std::move(obj_bbox)));
@@ -382,7 +387,8 @@ void Simulator::add_pre_add_AABB(node_ptr node, Frame position)
     scene.erase(scene.end() - 2);
 }
 
-std::pair<float, float> Simulator::intersect_axis(float rayDirection_a, float rayPosition_a, float LH_first_a, float LH_second_a)
+std::pair<float, float> Simulator::intersect_axis(float rayDirection_a, float rayPosition_a, 
+                                                  float LH_first_a, float LH_second_a)
 {
     float t_low;
     float t_high;
@@ -393,14 +399,17 @@ std::pair<float, float> Simulator::intersect_axis(float rayDirection_a, float ra
     }
     else
     {
-        t_low = glm::min((LH_first_a - rayPosition_a) / rayDirection_a, (LH_second_a - rayPosition_a) / rayDirection_a);
-        t_high = glm::max((LH_first_a - rayPosition_a) / rayDirection_a, (LH_second_a - rayPosition_a) / rayDirection_a);
+        t_low = glm::min((LH_first_a - rayPosition_a) / rayDirection_a, 
+                         (LH_second_a - rayPosition_a) / rayDirection_a);
+        t_high = glm::max((LH_first_a - rayPosition_a) / rayDirection_a,
+                          (LH_second_a - rayPosition_a) / rayDirection_a);
     }
 
     return { t_low, t_high };
 }
 
-std::pair<float, float> Simulator::intersect(glm::vec3 rayPosition, glm::vec3 rayDirection, std::pair<glm::vec3, glm::vec3> LH)
+std::pair<float, float> Simulator::intersect(glm::vec3 rayPosition, glm::vec3 rayDirection, 
+                                             std::pair<glm::vec3, glm::vec3> LH)
 {
     auto [ tx, tX ] = intersect_axis(rayDirection.x, rayPosition.x, LH.first.x, LH.second.x);
     auto [ ty, tY ] = intersect_axis(rayDirection.y, rayPosition.y, LH.first.y, LH.second.y);
@@ -477,7 +486,9 @@ std::pair<glm::vec3, glm::vec3> Simulator::get_pick_ray()
 
     //calculate the ray position and direction
     glm::vec3 rayPosition = glm::vec3(worldSpaceNear.x, worldSpaceNear.y, worldSpaceNear.z);
-    glm::vec3 rayDirection = -glm::vec3(worldSpaceFar.x - worldSpaceNear.x, worldSpaceFar.y - worldSpaceNear.y, worldSpaceFar.z - worldSpaceNear.z);
+    glm::vec3 rayDirection = -glm::vec3(worldSpaceFar.x - worldSpaceNear.x, 
+                                        worldSpaceFar.y - worldSpaceNear.y, 
+                                        worldSpaceFar.z - worldSpaceNear.z);
 
     rayDirection = camera->getFrame()->getRotation() * rayDirection;
 
@@ -562,7 +573,9 @@ void Simulator::move_object()
     controller->setup(timer().dt(), selection->getFrame());
 
     if (keyboard().down().contains("LeftShift"))
-        controller->mouse_elevation_move(rayPosition, rayDirection, active_cameras[0]->getFrame()->getRotation(), prev_plane_intersect, object_moving);
+        controller->mouse_elevation_move(rayPosition, rayDirection, 
+                                         active_cameras[0]->getFrame()->getRotation(), 
+                                         prev_plane_intersect, object_moving);
     else
         controller->mouse_horizontal_move(rayPosition, rayDirection, prev_plane_intersect, object_moving);
 }
-- 
GitLab