From 8a8db47f6c1be032cffb4f623cc197945894467f Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Wed, 12 Jul 2023 16:10:20 -0400 Subject: [PATCH] Added lookat --- Editor/resources/Scripts/Scene.wren | 4 ++ Nuake/src/Core/Maths.cpp | 48 ++++++++++++++++++++++- Nuake/src/Core/Maths.h | 2 + Nuake/src/Scripting/Modules/SceneModule.h | 16 ++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Editor/resources/Scripts/Scene.wren b/Editor/resources/Scripts/Scene.wren index 4b870cbd..b6bfd84a 100644 --- a/Editor/resources/Scripts/Scene.wren +++ b/Editor/resources/Scripts/Scene.wren @@ -44,6 +44,7 @@ class Scene { foreign static GetTranslation_(e) foreign static SetTranslation_(e, x, y, z) foreign static SetRotation_(e, x, y, z) + foreign static SetLookAt_(e, x, y, z) foreign static GetRotation_(e) //foreign static SetScale_(e, x, y, z) @@ -111,6 +112,9 @@ class TransformComponent { Scene.SetRotation_(_entityId, t.x, t.y, t.z) } + SetLookAt(t) { + Scene.SetLookAt_(_entityId, t.x, t.y, t.z) + } } class Light { diff --git a/Nuake/src/Core/Maths.cpp b/Nuake/src/Core/Maths.cpp index f77709c5..971339eb 100644 --- a/Nuake/src/Core/Maths.cpp +++ b/Nuake/src/Core/Maths.cpp @@ -2,9 +2,55 @@ using namespace Nuake; +Quat Nuake::LookAt(Vector3 sourcePoint, Vector3 destPoint) +{ + Vector3 forwardVector = glm::normalize(destPoint - sourcePoint); + + float dot = glm::dot(Vector3(0, 0, 1), forwardVector); + if (glm::abs(dot - (-1.0f)) < 0.000001f) + { + return Quat(0, 1, 0, 3.1415926535897932f); + } + if (glm::abs(dot - (1.0f)) < 0.000001f) + { + return Quat(); // identity + } + + float rotAngle = acos(dot); + Vector3 rotAxis = glm::cross(Vector3(0, 0, 1), forwardVector); + rotAxis = glm::normalize(rotAxis); + return CreateFromAxisAngle(rotAxis, rotAngle); +} + +// just in case you need that function also +Quat Nuake::CreateFromAxisAngle(Vector3 axis, float angle) +{ + float halfAngle = angle * .5f; + float s = sin(halfAngle); + Quat q; + q.x = axis.x * s; + q.y = axis.y * s; + q.z = axis.z * s; + q.w = cos(halfAngle); + return q; +} + Quat Nuake::QuatFromEuler(float x, float y, float z) { - return Quat(Vector3(Rad(z), Rad(y), Rad(x))); + Quat q; + + float cr = cos(x * 0.5); + float sr = sin(x * 0.5); + float cp = cos(y * 0.5); + float sp = sin(y * 0.5); + float cy = cos(z * 0.5); + float sy = sin(z * 0.5); + + q.w = cr * cp * cy + sr * sp * sy; + q.x = sr * cp * cy - cr * sp * sy; + q.y = cr * sp * cy + sr * cp * sy; + q.z = cr * cp * sy - sr * sp * cy; + return q; } Vector3 Nuake::QuatToDirection(const Quat& quat) diff --git a/Nuake/src/Core/Maths.h b/Nuake/src/Core/Maths.h index c16fba0e..13a91871 100644 --- a/Nuake/src/Core/Maths.h +++ b/Nuake/src/Core/Maths.h @@ -20,6 +20,8 @@ namespace Nuake using Matrix3 = glm::mat3; #define Rad(degrees) glm::radians(degrees) + Quat LookAt(Vector3 sourcePoint, Vector3 destPoint); + Quat CreateFromAxisAngle(Vector3 axis, float angle); Quat QuatFromEuler(float x, float y, float z); Vector3 QuatToDirection(const Quat& quat); } diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index 2e0087d7..fbaa0ac5 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -40,6 +40,8 @@ namespace Nuake { RegisterMethod("SetTranslation_(_,_,_,_)", (void*)SetTranslation); RegisterMethod("GetRotation_(_)", (void*)GetRotation); RegisterMethod("SetRotation_(_,_,_,_)", (void*)SetRotation); + RegisterMethod("SetLookAt_(_,_,_,_)", (void*)SetLookAt); + RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity); RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity); @@ -263,6 +265,20 @@ namespace Nuake { transform.SetLocalRotation(QuatFromEuler(x, y, z)); } + static void SetLookAt(WrenVM* vm) + { + double handle = wrenGetSlotDouble(vm, 1); + Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get()); + auto& transform = ent.GetComponent(); + // set the slots + float x = (float)wrenGetSlotDouble(vm, 2); + float y = (float)wrenGetSlotDouble(vm, 3); + float z = (float)wrenGetSlotDouble(vm, 4); + Vector3 targetPosition = Vector3(x, y, z); + Quat rotation = LookAt(transform.GetGlobalPosition(), transform.GetGlobalPosition() + targetPosition); + transform.SetLocalRotation(rotation); + } + static void SetTranslation(WrenVM* vm) { double handle = wrenGetSlotDouble(vm, 1);