Added wren binding to raycast function

This commit is contained in:
Antoine Pilote
2023-07-24 00:56:27 -04:00
parent fc8628fb4f
commit 5103101a0d
7 changed files with 92 additions and 51 deletions

View File

@@ -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)

View File

@@ -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;;
}
}
}

View File

@@ -37,7 +37,7 @@ namespace Nuake
m_World->Clear();
}
RaycastResult PhysicsManager::Raycast(const Vector3& from, const Vector3& to)
std::vector<RaycastResult> PhysicsManager::Raycast(const Vector3& from, const Vector3& to)
{
return m_World->Raycast(from, to);
}

View File

@@ -45,7 +45,7 @@ namespace Nuake
void Reset();
RaycastResult Raycast(const Vector3& from, const Vector3& to);
std::vector<RaycastResult> Raycast(const Vector3& from, const Vector3& to);
void RegisterBody(Ref<Physics::RigidBody> rb);
void RegisterGhostBody(Ref<GhostObject> rb);

View File

@@ -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;
};
}

View File

@@ -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);
}
};
}

View File

@@ -23,8 +23,10 @@
#include <src/Scene/Components/BSPBrushComponent.h>
#include <src/Scene/Components/PrefabComponent.h>
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<double>(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);