This commit is contained in:
Antoine Pilote
2023-07-31 11:09:23 -04:00
9 changed files with 108 additions and 69 deletions

View File

@@ -75,6 +75,28 @@ class Scene {
// RigidBody
foreign static AddForce_(e, x, y, z)
// Physics
static RayCast(from, to) {
// Fetch results from physics manager, returns a list of floats
// that we need to unpack into vector3
var results = Scene.RayCast_(from.x, from.y, from.z, to.x, to.y, to.z)
var points = [] // Result array of vec3
var size = results.count / 3
var i = 0
while(i < size) {
// size is base 1 while array is base 0, offset by 1
var listIndex = i - 1
points.add(Vector3.new(results[listIndex], results[listIndex + 1], results[listIndex + 2]))
i = i + 1
}
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

@@ -28,6 +28,10 @@
#include <Jolt/Physics/Body/BodyCreationSettings.h>
#include <Jolt/Physics/Body/BodyActivationListener.h>
#include <Jolt/Physics/Character/Character.h>
#include <Jolt/Physics/Collision/RayCast.h>
#include <Jolt/Physics/Collision/CastResult.h>
#include <dependencies/JoltPhysics/Jolt/Physics/Collision/CollisionCollectorImpl.h>
namespace Nuake
{
@@ -211,6 +215,7 @@ namespace Nuake
_JoltPhysicsSystem = CreateRef<JPH::PhysicsSystem>();
_JoltPhysicsSystem->Init(MaxBodies, NumBodyMutexes, MaxBodyPairs, MaxContactConstraints, JoltBroadphaseLayerInterface, MyBroadPhaseCanCollide, MyObjectCanCollide);
// A body activation listener gets notified when bodies activate and go to sleep
// Note that this is called from a job so whatever you do here needs to be thread safe.
// Registering one is entirely optional.
@@ -322,20 +327,36 @@ namespace Nuake
return false;
}
RaycastResult DynamicWorld::Raycast(const Vector3& from, const Vector3& to)
std::vector<RaycastResult> DynamicWorld::Raycast(const Vector3& from, const Vector3& to)
{
Vector3 localNorm = Vector3(0,0,0);
// Create jolt ray
const auto& fromJolt = JPH::Vec3(from.x, from.y, from.z);
const auto& toJolt = JPH::Vec3(to.x, to.y, to.z);
JPH::RayCast ray { fromJolt, toJolt - fromJolt };
JPH::AllHitCollisionCollector<JPH::RayCastBodyCollector> collector;
_JoltPhysicsSystem->GetBroadPhaseQuery().CastRay(ray, collector);
//Logger::Log("normal: x:" + std::to_string(localNorm.x) + " y:" + std::to_string(localNorm.y )+ "z: " + std::to_string(localNorm.z));
// Map bullet result to dto.
RaycastResult result{
Vector3(0,0,0),
Vector3(0,0,0),
localNorm
};
// Fetch results
int num_hits = (int)collector.mHits.size();
JPH::BroadPhaseCastResult* results = collector.mHits.data();
return result;
// Format result
std::vector<RaycastResult> raycastResults;
for (int i = 0; i < num_hits; ++i)
{
const float hitFraction = results[i].mFraction;
const JPH::Vec3& hitPosition = ray.GetPointOnRay(results[i].mFraction);
RaycastResult result
{
Vector3(hitPosition.GetX(), hitPosition.GetY(), hitPosition.GetZ()),
hitFraction
};
raycastResults.push_back(std::move(result));
}
return raycastResults;
}
void DynamicWorld::SyncEntitiesTranforms()

View File

@@ -1,8 +1,9 @@
#pragma once
#include "src/Core/Core.h"
#include "src/Core/Timestep.h"
#include <glm/ext/vector_float3.hpp>
#include "Rigibody.h"
#include "src/Core/Timestep.h"
#include "src/Core/Core.h"
#include <map>
#include "RaycastResult.h"
@@ -10,7 +11,7 @@
#include "CharacterController.h"
#include <Jolt/Jolt.h>
#include "src/Core/Core.h"
namespace JPH
{
class PhysicsSystem;
@@ -32,7 +33,8 @@ namespace Nuake
class MyContactListener;
class MyBodyActivationListener;
namespace Physics {
namespace Physics
{
class DynamicWorld
{
private:
@@ -47,6 +49,7 @@ namespace Nuake
std::vector<uint32_t> _registeredBodies;
std::map<uint32_t, JPH::Character*> _registeredCharacters;
public:
DynamicWorld();
@@ -62,7 +65,7 @@ namespace Nuake
void MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity);
void AddForceToRigidBody(const Entity& entity, const Vector3& force);
RaycastResult Raycast(const Vector3& from, const Vector3& to);
std::vector<RaycastResult> Raycast(const Vector3& from, const Vector3& to);
void StepSimulation(Timestep ts);
void Clear();

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,16 +5,10 @@
namespace Nuake
{
namespace Physics
{
}
// result object from raycast.
struct RaycastResult {
Vector3 WorldPoint;
Vector3 LocalPoint;
Vector3 Normal;
struct RaycastResult
{
Vector3 WorldPosition;
float Fraction;
};
}

View File

@@ -28,39 +28,7 @@ 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);
}
};
}

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);
@@ -191,6 +194,7 @@ namespace Nuake {
double x = wrenGetSlotDouble(vm, 2);
double y = wrenGetSlotDouble(vm, 3);
double z = wrenGetSlotDouble(vm, 4);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
cam.CameraInstance->SetDirection(Vector3(x, y, z));
@@ -249,6 +253,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);