diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 0cc571e3..c7c2a1c2 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -28,6 +28,10 @@ #include #include #include +#include +#include + +#include namespace Nuake { @@ -211,6 +215,7 @@ namespace Nuake _JoltPhysicsSystem = CreateRef(); _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 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 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 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() diff --git a/Nuake/src/Core/Physics/DynamicWorld.h b/Nuake/src/Core/Physics/DynamicWorld.h index bbb5c05c..2336bdf5 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.h +++ b/Nuake/src/Core/Physics/DynamicWorld.h @@ -1,8 +1,9 @@ #pragma once +#include "src/Core/Core.h" +#include "src/Core/Timestep.h" + #include #include "Rigibody.h" -#include "src/Core/Timestep.h" -#include "src/Core/Core.h" #include #include "RaycastResult.h" @@ -10,7 +11,7 @@ #include "CharacterController.h" #include -#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 _registeredBodies; std::map _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 Raycast(const Vector3& from, const Vector3& to); void StepSimulation(Timestep ts); void Clear(); diff --git a/Nuake/src/Core/Physics/RaycastResult.h b/Nuake/src/Core/Physics/RaycastResult.h index be12f90c..6760ceaa 100644 --- a/Nuake/src/Core/Physics/RaycastResult.h +++ b/Nuake/src/Core/Physics/RaycastResult.h @@ -7,14 +7,11 @@ namespace Nuake { namespace Physics { - + // result object from raycast. + struct RaycastResult + { + Vector3 WorldPosition; + float Fraction; + }; } - - - // result object from raycast. - struct RaycastResult { - Vector3 WorldPoint; - Vector3 LocalPoint; - Vector3 Normal; - }; }