From a99ffec8c25ebfcf090da3689afa457efe85b480 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Tue, 14 Apr 2015 17:28:20 +0200 Subject: [PATCH] player rotates to view direction fixed move in view direction --- Weave/Graphix/GLM.h | 4 ++++ Weave/Graphix/Scene.cpp | 16 ++++++---------- Weave/Graphix/SceneObject.cpp | 21 ++++++++++++++++++--- Weave/Graphix/SceneObject.h | 3 ++- Weave/Graphix/ViewPort.cpp | 1 - 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Weave/Graphix/GLM.h b/Weave/Graphix/GLM.h index a334115..4cf1186 100644 --- a/Weave/Graphix/GLM.h +++ b/Weave/Graphix/GLM.h @@ -4,6 +4,7 @@ #include "glm\gtx\transform.hpp" #include "glm\gtc\type_ptr.hpp" #include "glm\gtx\rotate_vector.hpp" +#include "glm\gtx\vector_angle.hpp" using glm::vec3; using glm::mat4; @@ -17,6 +18,9 @@ using glm::normalize; using glm::inverse; using glm::dot; +using glm::angle; +using glm::orientedAngle; + using glm::value_ptr; using glm::sign; diff --git a/Weave/Graphix/Scene.cpp b/Weave/Graphix/Scene.cpp index 3166671..92259ff 100644 --- a/Weave/Graphix/Scene.cpp +++ b/Weave/Graphix/Scene.cpp @@ -11,6 +11,7 @@ #include "Shader.h" #define TIME_TILL_MAX_MOVE 2 +#define TIME_TILL_DIRECTION_ROTATE 1 #define SPEED_MOVE_NORMAL 1.5f #define MOVE_FASTER 2.f @@ -89,12 +90,11 @@ void Scene::update(float deltaT) // XYAchse um den Player viewPort->rotateView(0.005f*Events::getViewX(), 0.002f*Events::getViewY()); - //int view_x = Events::getViewX(); - //int view_y = Events::getViewY(); + // MOVE Player if (move_x) lookat->move(SPEED_MOVE_NORMAL *move_x * deltaT * viewPort->rotateDirection(vec3(-1.f, 0.f, 0.f))); - //TODO vorwärts schneller gehen nach einer sekunde + if (move_y){ if (move_y > 0){ if (move_delta < TIME_TILL_MAX_MOVE) @@ -111,14 +111,10 @@ void Scene::update(float deltaT) move_delta = 0; } + //Rotate Play in move direction - - - //if ( view_x || view_y) - //{ - // //TODO Limit Rotation - // view = view * rotate(0.3f, vec3(view_y ,view_x, 0.f)); - //} + if (move_x || move_y) + lookat->turnTo(viewPort->rotateDirection(vec3(0.f, 0.f, -1.f)),3*deltaT/TIME_TILL_DIRECTION_ROTATE); //// Zoom auf Player diff --git a/Weave/Graphix/SceneObject.cpp b/Weave/Graphix/SceneObject.cpp index d8526c8..a0df510 100644 --- a/Weave/Graphix/SceneObject.cpp +++ b/Weave/Graphix/SceneObject.cpp @@ -100,18 +100,33 @@ void SceneObject::setModel(mat4& _model){ model = _model; } -void SceneObject::turn(float degree, vec3& axis){ +void SceneObject::turn(float angle, vec3& axis){ //vec3 pos = getPosition(); //model = rotate(degree, axis)*model; //model = model*translate(pos - getPosition()); - model = model * rotate(degree, axis); + model = model * rotate(angle, axis); +} + +void SceneObject::turnTo(vec3& direction, float speed){ + vec3 pos = getPosition(); + float perc = 1; + model = translate(-pos) * model; + float rot_angle = orientedAngle(normalize((vec3)(model * vec4(0.f, 0.f, 1.f, 1.f))), direction, vec3(0.f, 1.f, 0.f)); + + if (abs(rot_angle) / M_PI > speed) + perc = speed / abs(rot_angle) * M_PI; + + model = rotate(rot_angle *perc , vec3(0.f, 1.f, 0.f)) * model; + model = translate(pos) * model; } void SceneObject::move(vec3& dist){ - model = model * translate(dist); + model = translate(dist) * model; } + + void SceneObject::setCollision(bool _col) { collision_ignore = !_col; diff --git a/Weave/Graphix/SceneObject.h b/Weave/Graphix/SceneObject.h index ea431f3..78e2799 100644 --- a/Weave/Graphix/SceneObject.h +++ b/Weave/Graphix/SceneObject.h @@ -34,7 +34,8 @@ public: virtual vec3 getPosition() const; virtual void setModel(mat4&); - virtual void turn(float degree, vec3& axis); + virtual void turn(float angle, vec3& axis); + virtual void turnTo(vec3& direction, float speed = 1); virtual void move(vec3& dist); //virtual bool operator==(SceneObject); diff --git a/Weave/Graphix/ViewPort.cpp b/Weave/Graphix/ViewPort.cpp index 3aab0ff..70e1ab1 100644 --- a/Weave/Graphix/ViewPort.cpp +++ b/Weave/Graphix/ViewPort.cpp @@ -59,7 +59,6 @@ void ViewPort::rotateView(float angle_x, float angle_y){ if (view_angle_y < VIEW_BOT_LIM) view_angle_y = VIEW_BOT_LIM; - //keine Ahnung warum hier noch durch 50 geteilt werden muss, sonst ists aber irgend wie falsch view = translate(vec3(0.f, 0.f, -view_dist))*rotate(view_angle_y *(float)M_PI_4, vec3(1.f, 0.f, 0.f))*rotate(view_angle_x * (float)M_PI_4, vec3(0.f, 1.f, 0.f)); } -- 2.47.3