From 5103101a0d09edb01872f03a11b3244d7ba214fc Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 24 Jul 2023 00:56:27 -0400 Subject: [PATCH] Added wren binding to raycast function --- Editor/resources/Scripts/Scene.wren | 14 +++++ Editor/src/Windows/WelcomeWindow.cpp | 8 +-- Nuake/src/Core/Physics/PhysicsManager.cpp | 2 +- Nuake/src/Core/Physics/PhysicsManager.h | 2 +- Nuake/src/Core/Physics/RaycastResult.h | 13 ++-- Nuake/src/Scripting/Modules/PhysicsModule.h | 66 ++++++++++----------- Nuake/src/Scripting/Modules/SceneModule.h | 38 +++++++++++- 7 files changed, 92 insertions(+), 51 deletions(-) diff --git a/Editor/resources/Scripts/Scene.wren b/Editor/resources/Scripts/Scene.wren index 64aa3c93..3a4d0a3f 100644 --- a/Editor/resources/Scripts/Scene.wren +++ b/Editor/resources/Scripts/Scene.wren @@ -75,6 +75,20 @@ class Scene { // RigidBody foreign static AddForce_(e, x, y, z) + // Physics + static RayCast(from, to) { + var results = Scene.RayCast_(from.x, from.y, from.z, to.x, to.y, to.z) + + var points = [] + for(r in results.count / 3) { + points.add(Vector3.new(points[r], points[r + 1], points[r + 2])) + } + + return points + } + + foreign static RayCast_(fromX, fromY, fromZ, toX, toY, toZ) + foreign static TriggerGetOverlappingBodyCount_(e) foreign static TriggerGetOverlappingBodies_(e) diff --git a/Editor/src/Windows/WelcomeWindow.cpp b/Editor/src/Windows/WelcomeWindow.cpp index 19cd126d..927a4728 100644 --- a/Editor/src/Windows/WelcomeWindow.cpp +++ b/Editor/src/Windows/WelcomeWindow.cpp @@ -84,6 +84,7 @@ namespace Nuake void WelcomeWindow::LoadQueuedProject() { + FileSystem::SetRootDirectory(FileSystem::GetParentPath(queuedProjectPath)); auto project = Project::New(); auto projectFileData = FileSystem::ReadFile(queuedProjectPath, true); try @@ -274,14 +275,9 @@ namespace Nuake { assert(SelectedProject < std::size(_Projects)); - using namespace Nuake; - SaveRecentFile(); - std::string projectPath = _Projects[SelectedProject].Path; - FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); - - queuedProjectPath = projectPath; + queuedProjectPath = _Projects[SelectedProject].Path;; } } } diff --git a/Nuake/src/Core/Physics/PhysicsManager.cpp b/Nuake/src/Core/Physics/PhysicsManager.cpp index ae3e9ce2..c7cc2ea4 100644 --- a/Nuake/src/Core/Physics/PhysicsManager.cpp +++ b/Nuake/src/Core/Physics/PhysicsManager.cpp @@ -37,7 +37,7 @@ namespace Nuake m_World->Clear(); } - RaycastResult PhysicsManager::Raycast(const Vector3& from, const Vector3& to) + std::vector PhysicsManager::Raycast(const Vector3& from, const Vector3& to) { return m_World->Raycast(from, to); } diff --git a/Nuake/src/Core/Physics/PhysicsManager.h b/Nuake/src/Core/Physics/PhysicsManager.h index 3934a07c..2946a036 100644 --- a/Nuake/src/Core/Physics/PhysicsManager.h +++ b/Nuake/src/Core/Physics/PhysicsManager.h @@ -45,7 +45,7 @@ namespace Nuake void Reset(); - RaycastResult Raycast(const Vector3& from, const Vector3& to); + std::vector Raycast(const Vector3& from, const Vector3& to); void RegisterBody(Ref rb); void RegisterGhostBody(Ref rb); diff --git a/Nuake/src/Core/Physics/RaycastResult.h b/Nuake/src/Core/Physics/RaycastResult.h index 6760ceaa..7a77e470 100644 --- a/Nuake/src/Core/Physics/RaycastResult.h +++ b/Nuake/src/Core/Physics/RaycastResult.h @@ -5,13 +5,10 @@ namespace Nuake { - namespace Physics + // result object from raycast. + struct RaycastResult { - // result object from raycast. - struct RaycastResult - { - Vector3 WorldPosition; - float Fraction; - }; - } + Vector3 WorldPosition; + float Fraction; + }; } diff --git a/Nuake/src/Scripting/Modules/PhysicsModule.h b/Nuake/src/Scripting/Modules/PhysicsModule.h index 621750bb..5fb11514 100644 --- a/Nuake/src/Scripting/Modules/PhysicsModule.h +++ b/Nuake/src/Scripting/Modules/PhysicsModule.h @@ -28,39 +28,39 @@ namespace Nuake static void Raycast(WrenVM* vm) { - Vector3 v1 = Vector3(wrenGetSlotDouble(vm, 1), - wrenGetSlotDouble(vm, 2), - wrenGetSlotDouble(vm, 3)); - Vector3 v2 = Vector3(wrenGetSlotDouble(vm, 4), - wrenGetSlotDouble(vm, 5), - wrenGetSlotDouble(vm, 6)); - - RaycastResult result = PhysicsManager::Get().Raycast(v1, v2); - wrenSetSlotNewList(vm, 0); - - // Returns a list with 3 vectors placed sequentially - // should be reconstructed in the wren side. - wrenSetSlotDouble(vm, 1, result.LocalPoint.x); - wrenSetSlotDouble(vm, 2, result.LocalPoint.y); - wrenSetSlotDouble(vm, 3, result.LocalPoint.z); - - wrenSetSlotDouble(vm, 4, result.WorldPoint.x); - wrenSetSlotDouble(vm, 5, result.WorldPoint.y); - wrenSetSlotDouble(vm, 6, result.WorldPoint.z); - - wrenSetSlotDouble(vm, 7, result.Normal.x); - wrenSetSlotDouble(vm, 8, result.Normal.y); - wrenSetSlotDouble(vm, 9, result.Normal.z); - - wrenInsertInList(vm, 0, -1, 1); - wrenInsertInList(vm, 0, -1, 2); - wrenInsertInList(vm, 0, -1, 3); - wrenInsertInList(vm, 0, -1, 4); - wrenInsertInList(vm, 0, -1, 5); - wrenInsertInList(vm, 0, -1, 6); - wrenInsertInList(vm, 0, -1, 7); - wrenInsertInList(vm, 0, -1, 8); - wrenInsertInList(vm, 0, -1, 9); + //Vector3 v1 = Vector3(wrenGetSlotDouble(vm, 1), + // wrenGetSlotDouble(vm, 2), + // wrenGetSlotDouble(vm, 3)); + //Vector3 v2 = Vector3(wrenGetSlotDouble(vm, 4), + // wrenGetSlotDouble(vm, 5), + // wrenGetSlotDouble(vm, 6)); + // + //RaycastResult result = PhysicsManager::Get().Raycast(v1, v2); + //wrenSetSlotNewList(vm, 0); + // + //// Returns a list with 3 vectors placed sequentially + //// should be reconstructed in the wren side. + //wrenSetSlotDouble(vm, 1, result.LocalPoint.x); + //wrenSetSlotDouble(vm, 2, result.LocalPoint.y); + //wrenSetSlotDouble(vm, 3, result.LocalPoint.z); + // + //wrenSetSlotDouble(vm, 4, result.WorldPoint.x); + //wrenSetSlotDouble(vm, 5, result.WorldPoint.y); + //wrenSetSlotDouble(vm, 6, result.WorldPoint.z); + // + //wrenSetSlotDouble(vm, 7, result.Normal.x); + //wrenSetSlotDouble(vm, 8, result.Normal.y); + //wrenSetSlotDouble(vm, 9, result.Normal.z); + // + //wrenInsertInList(vm, 0, -1, 1); + //wrenInsertInList(vm, 0, -1, 2); + //wrenInsertInList(vm, 0, -1, 3); + //wrenInsertInList(vm, 0, -1, 4); + //wrenInsertInList(vm, 0, -1, 5); + //wrenInsertInList(vm, 0, -1, 6); + //wrenInsertInList(vm, 0, -1, 7); + //wrenInsertInList(vm, 0, -1, 8); + //wrenInsertInList(vm, 0, -1, 9); } }; } diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index c15f109f..a3e17a8b 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -23,8 +23,10 @@ #include #include -namespace Nuake { - namespace ScriptAPI { +namespace Nuake +{ + namespace ScriptAPI + { class SceneModule : public ScriptModule { std::string GetModuleName() override @@ -56,6 +58,7 @@ namespace Nuake { RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide); RegisterMethod("IsCharacterControllerOnGround_(_)", (void*)IsCharacterControllerOnGround); + RegisterMethod("RayCast_(_,_,_,_,_,_)", (void*)Raycast); RegisterMethod("AddForce_(_,_,_,_)", (void*)AddForce); @@ -249,6 +252,37 @@ namespace Nuake { wrenSetSlotBool(vm, 0, result); } + static void Raycast(WrenVM* vm) + { + const double& fromX = wrenGetSlotDouble(vm, 1); + const double& fromY = wrenGetSlotDouble(vm, 2); + const double& fromZ = wrenGetSlotDouble(vm, 3); + const auto& from = Vector3(fromX, fromY, fromZ); + + const double& toX = wrenGetSlotDouble(vm, 4); + const double& toY = wrenGetSlotDouble(vm, 5); + const double& toZ = wrenGetSlotDouble(vm, 6); + const auto& to = Vector3(toX, toY, toZ); + + const auto& result = PhysicsManager::Get().GetWorld()->Raycast(from, to); + + // We multiply by 3 since we have 3 floats per hit result: vector3. + wrenEnsureSlots(vm, static_cast(result.size() * 3)); + wrenSetSlotNewList(vm, 0); + + uint32_t index = 1; + for (auto& r : result) + { + wrenSetSlotDouble(vm, index, r.WorldPosition.x); + wrenSetSlotDouble(vm, index + 1, r.WorldPosition.y); + wrenSetSlotDouble(vm, index + 2, r.WorldPosition.z); + wrenInsertInList(vm, 0, -1, index); + wrenInsertInList(vm, 0, -1, index + 1); + wrenInsertInList(vm, 0, -1, index + 2); + index += 3; + } + } + static void MoveAndSlide(WrenVM* vm) { double handle = wrenGetSlotDouble(vm, 1);