Skip to content

Commit

Permalink
fix: freelookcamera inverted controls
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanbaburajan committed Oct 7, 2023
1 parent d53f9cf commit 3057690
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 76 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ set (GLFW_BUILD_DOCS OFF)
set (GLFW_BUILD_TESTS OFF)
set (GLFW_BUILD_EXAMPLES OFF)

option (DT_EXPORT "Build the project for export." OFFz)
option (DT_EXPORT "Build the project for export." OFF)

if (NOT DT_EXPORT)
if (DT_EXPORT)
add_compile_definitions (DT_EXPORT)
else ()
set (BUILD_SHARED_LIBS ON CACHE BOOL "")
add_compile_definitions(DT_EXPORT)
endif ()

set (CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/Extern/assimp/cmake-modules;${CMAKE_MODULE_PATH}")
Expand Down
6 changes: 3 additions & 3 deletions Engine/Components/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ namespace DT

void Camera::Tick(Context &ctx, const float &dt)
{
view = glm::lookAtLH(transform->translation, transform->translation + transform->rotation * glm::vec3(0.f, 0.f, 1.f), glm::vec3(0.f, 1.f, 0.f));
view = glm::lookAtLH(transform->translation, transform->translation + transform->Forward(), transform->Up());

glm::vec2 winSize = window->GetWindowSize();
float aspect = (float)winSize.x/winSize.y;
if (isOrthographic)
projection = glm::ortho(-aspect, aspect, -1.0f, 1.0f, nearPlane, farPlane);
projection = glm::orthoLH(-aspect, aspect, -1.0f, 1.0f, nearPlane, farPlane);
else
projection = glm::perspective(glm::radians(fieldOfView), window->GetWindowSize().x / window->GetWindowSize().y, nearPlane, farPlane);
projection = glm::perspectiveLH(glm::radians(fieldOfView), window->GetWindowSize().x / window->GetWindowSize().y, nearPlane, farPlane);

}
}
8 changes: 6 additions & 2 deletions Engine/Components/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace DT

glm::vec3 Transform::Forward()
{
return glm::rotate(rotation, glm::vec3(0.0, 0.0, -1.0));
return glm::rotate(rotation, glm::vec3(0.0, 0.0, 1.0));
}

glm::vec3 Transform::Up()
Expand All @@ -65,7 +65,11 @@ namespace DT

void Transform::SetEulerRotation(glm::vec3 eulerRotation)
{
eulerRotation = {glm::angNormalize(eulerRotation.x), glm::angNormalize(eulerRotation.y), glm::angNormalize(eulerRotation.z)};
rotation = glm::radians(eulerRotation);
}

void Transform::LookAt(const glm::vec3 &at)
{
// rotation = glm::quatLookAt(glm::normalize(at - translation), {0.f, 1.f, 0.f});
}
}
1 change: 1 addition & 0 deletions Engine/Components/Transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ namespace DT
glm::vec3 Up();
glm::vec3 GetEulerRotation();
void SetEulerRotation(glm::vec3 eulerRotation);
void LookAt(const glm::vec3 &at);
};
}
3 changes: 2 additions & 1 deletion Engine/Core/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <glm/gtx/string_cast.hpp>
#include <Core/InputManager.h>

namespace DT
Expand All @@ -45,7 +46,7 @@ namespace DT
double curPosX, curPosY;
glfwGetCursorPos(window->GetRawWindowPointer(), &curPosX, &curPosY);

mouseDelta = glm::vec2(curPosX - mousePosition.x, curPosY - mousePosition.y);
mouseDelta = glm::vec2(curPosX - mousePosition.x, mousePosition.y - curPosY);
mousePosition = glm::vec2(curPosX, curPosY);
}

Expand Down
19 changes: 12 additions & 7 deletions Engine/Utils/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ SOFTWARE.

namespace glm
{
inline float angNormalize(float angle)
double normalize( const double value, const double start, const double end )
{
angle = std::fmod(angle, 360.0f);
if (angle >= 0.0f)
return angle;
else
return 360.0f + angle;
const double width = end - start ; //
const double offsetValue = value - start ; // value relative to 0

return ( offsetValue - ( floor( offsetValue / width ) * width ) ) + start ;
}

inline float angNormalize(const float &angle)
{
const float offset = angle + 180.f;
return (offset - (floor(offset / 360.f) * 360.f)) - 180.f;
}

inline glm::vec3 angNormalize(glm::vec3 angle)
inline glm::vec3 angNormalize(const glm::vec3 &angle)
{
return glm::vec3(angNormalize(angle.x), angNormalize(angle.y), angNormalize(angle.z));
}
Expand Down
77 changes: 65 additions & 12 deletions Runtime/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ SOFTWARE.
#include <Editor.h>
#include <Panels/WorldOutliner.h>

#include <glm/gtx/string_cast.hpp>

#define DT_EXPORT

using namespace DT;

class FreeLookCamera : public Component
Expand All @@ -48,6 +52,8 @@ class FreeLookCamera : public Component
glm::vec3 orientation;
float speed = 2.5f, sensitivity = 75.f;

float lastx;

void Init(Context &ctx) override
{
transform = scene->Require<Transform>(entity).Fatal("FreeLookCamera::Init()");
Expand Down Expand Up @@ -85,25 +91,56 @@ class FreeLookCamera : public Component
if (input->IsKeyHeld(KEY_D))
transform->translation += speed * dt * transform->Right();
if (input->IsKeyHeld(KEY_SPACE))
transform->translation += speed * dt * transform->Up();
transform->translation += speed * dt * glm::vec3(0.f, 1.f, 0.f);
if (input->IsKeyHeld(KEY_LEFT_SHIFT))
transform->translation -= speed * dt * transform->Up();
transform->translation -= speed * dt * glm::vec3(0.f, 1.f, 0.f);
if (input->IsKeyHeld(KEY_E))
orientation.z += sensitivity * dt;
if (input->IsKeyHeld(KEY_Q))
orientation.z -= sensitivity * dt;

// Look
orientation.y += input->GetMouseDelta().x * sensitivity * dt;
orientation.x += -input->GetMouseDelta().y * sensitivity * dt;

if (orientation.x > 89.0f)
orientation.x = 89.0f;
if (orientation.x < -89.0f)
orientation.x = -89.0f;
if (orientation.x < -90.0f)
orientation.x = -90.0f;
if (orientation.x > 90.0f)
orientation.x = 90.0f;

orientation = glm::angNormalize(orientation);
transform->SetEulerRotation({orientation.x, orientation.y, 0.f});
transform->SetEulerRotation(orientation);
}
}
};

class TransformInspector : public Component
{
public:
std::shared_ptr<Transform> transform;
float speed = 2.5f, sensitivity = 75.f;
glm::vec3 orientation;
char dir;

TransformInspector(char _d) : dir(_d) {}

void Init(Context &ctx) override
{
transform = scene->Require<Transform>(entity).Fatal("FreeLookCamera::Init()");
}

void Tick(Context &ctx, const float &dt) override
{
if (dir == 'x')
orientation.x += sensitivity * dt;
if (dir == 'y')
orientation.y += sensitivity * dt;
if (dir == 'z')
orientation.z += sensitivity * dt;
transform->SetEulerRotation(orientation);
}
};

int main(int argc, char* argv[])
{
// Project
Expand Down Expand Up @@ -140,7 +177,7 @@ int main(int argc, char* argv[])
// Renderer
Renderer renderer(context, projectData.value("renderer", json::object()));
context.AttachService<Renderer>(&renderer).Fatal("main()");
#ifndef DT_EXPORT
#ifdef DT_EXPORT
renderer.SetRenderFrameBuffer(true);
#endif

Expand All @@ -151,27 +188,39 @@ int main(int argc, char* argv[])
// Scene
Entity logo = sceneManager.activeScene.CreateEntity();
sceneManager.activeScene.Require<Tag>(logo, "Logo").Fatal("main()");
sceneManager.activeScene.Require<SpriteRenderer>(logo, &context, projectPath / "Textures" / "logo.png").Fatal("main()");
sceneManager.activeScene.Require<SpriteRenderer>(logo, &context, projectPath / "User" / "Textures" / "logo.png").Fatal("main()");
sceneManager.activeScene.Require<TransformInspector>(logo, 'x').Fatal("main()");

Entity nyan = sceneManager.activeScene.CreateEntity();
sceneManager.activeScene.Require<Tag>(nyan, "Nyan").Fatal("main()");
sceneManager.activeScene.Require<Transform>(nyan).Fatal("main()")->translation += glm::vec3(1.f, 0.f, 0.f);
sceneManager.activeScene.Require<SpriteRenderer>(nyan, &context, projectPath / "Textures" / "nyan.png").Fatal("main()");
sceneManager.activeScene.Require<Transform>(nyan).Fatal("main()")->translation = glm::vec3(1.f, 0.f, 0.f);
sceneManager.activeScene.Require<SpriteRenderer>(nyan, &context, projectPath / "User" / "Textures" / "nyan.png").Fatal("main()");
sceneManager.activeScene.Require<TransformInspector>(nyan, 'y').Fatal("main()");

Entity logo2 = sceneManager.activeScene.CreateEntity();
sceneManager.activeScene.Require<Tag>(logo2, "Logo 2").Fatal("main()");
sceneManager.activeScene.Require<Transform>(logo2).Fatal("main()")->translation = glm::vec3(2.f, 0.f, 0.f);
sceneManager.activeScene.Require<SpriteRenderer>(logo2, &context, projectPath / "User" / "Textures" / "logo.png").Fatal("main()");
sceneManager.activeScene.Require<TransformInspector>(logo2, 'z').Fatal("main()");

Entity camera = sceneManager.activeScene.CreateEntity();
sceneManager.activeScene.Require<Tag>(camera, "Camera").Fatal("main()");
sceneManager.activeScene.Require<Camera>(camera).Fatal("main()");
// sceneManager.activeScene.Require<FreeLookCamera>(camera).Fatal("main()");
sceneManager.activeScene.Require<Transform>(camera).Fatal("main()")->translation = glm::vec3(0.f, 0.f, -3.f);
// sceneManager.activeScene.Get<Transform>(camera).Fatal("main()")->LookAt(glm::vec3(1.f, 0.f, 0.f));
sceneManager.activeScene.Require<FreeLookCamera>(camera).Fatal("main()");

sceneManager.Init(context);
renderer.Init(context);

// Editor
#ifndef DT_EXPORT
Editor editor(context);
WorldOutliner worldOutliner;
editor.AttachPanel(&worldOutliner).Fatal("main()");

editor.Init(context);
#endif

// Game Loop
float lastTime = 0.0f;
Expand All @@ -190,12 +239,16 @@ int main(int argc, char* argv[])
continue;

input.Process();
#ifndef DT_EXPORT
editor.NewFrame();
editor.Tick(context, deltaTime);
#endif
renderer.BeginScene();
sceneManager.Tick(context, deltaTime);
renderer.EndScene();
#ifndef DT_EXPORT
editor.EndFrame();
#endif
window.SwapBuffers();
}

Expand Down
2 changes: 1 addition & 1 deletion Sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.20)

file (GLOB_RECURSE dtp_source_list "${PROJECT_SOURCE_DIR}/Sandbox/Scripts/**/*.cpp" "${PROJECT_SOURCE_DIR}/Sandbox/Scripts/*.cpp")
file (GLOB_RECURSE dtp_source_list "${PROJECT_SOURCE_DIR}/Sandbox/User/Scripts/**/*.cpp" "${PROJECT_SOURCE_DIR}/Sandbox/User/Scripts/*.cpp")
add_library (DucktapeProject SHARED ${dtp_source_list})
set_target_properties (DucktapeProject PROPERTIES PREFIX "")
target_link_libraries(DucktapeProject PRIVATE Ducktape)
47 changes: 0 additions & 47 deletions Sandbox/Scenes/MainScene.json

This file was deleted.

File renamed without changes.
File renamed without changes
File renamed without changes

0 comments on commit 3057690

Please sign in to comment.