diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 4f05b26b..4088d927 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -249,7 +249,7 @@ namespace Nuake { DynamicWorld::DynamicWorld() : _stepCount(0) { - _registeredCharacters = std::map(); + _registeredCharacters = std::map>(); // Initialize Jolt Physics const uint32_t MaxBodies = 2048; @@ -339,10 +339,15 @@ namespace Nuake bodySettings.mMassPropertiesOverride.mMass = mass; } + if (rb->GetEntity().GetID() == 0) + { + Logger::Log("Entity with ID 0 detected. Name: " + rb->GetEntity().GetComponent().Name, "DEBUG"); + } + bodySettings.mUserData = rb->GetEntity().GetID(); // Create the actual rigid body JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr - _registeredBodies.push_back((uint32_t)body.GetIndexAndSequenceNumber()); + _registeredBodies.push_back((uint32_t)body.GetIndex()); } void DynamicWorld::AddGhostbody(Ref gb) @@ -364,7 +369,7 @@ namespace Nuake const Quat& bodyRotation = cc->Rotation; const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w); - auto character = new JPH::CharacterVirtual(settings, std::move(joltPosition), std::move(joltRotation), _JoltPhysicsSystem.get()); + auto character = CreateRef(settings, std::move(joltPosition), std::move(joltRotation), _JoltPhysicsSystem.get()); // To get the jolt character control from a scene entity. _registeredCharacters[cc->Owner.GetHandle()] = character; @@ -468,7 +473,7 @@ namespace Nuake { Entity entity { (entt::entity)e.first, Engine::GetCurrentScene().get()}; - JPH::CharacterVirtual* characterController = e.second; + Ref characterController = e.second; JPH::Mat44 joltTransform = characterController->GetWorldTransform(); const auto bodyRotation = characterController->GetRotation(); @@ -584,13 +589,17 @@ namespace Nuake if (!_registeredBodies.empty()) { - _JoltBodyInterface->RemoveBodies(reinterpret_cast(_registeredBodies.data()), _registeredBodies.size()); + for (auto& body : _registeredBodies) + { + _JoltBodyInterface->RemoveBody(static_cast(body)); + } _registeredBodies.clear(); } if (!_registeredCharacters.empty()) { + _registeredCharacters.clear(); } } @@ -664,6 +673,7 @@ namespace Nuake break; case RigidbodyShapes::MESH: { + assert(true); MeshShape* meshShape = (MeshShape*)shape.get(); const auto& mesh = meshShape->GetMesh(); const auto& vertices = mesh->GetVertices(); diff --git a/Nuake/src/Core/Physics/DynamicWorld.h b/Nuake/src/Core/Physics/DynamicWorld.h index a050e768..2f0b6dad 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.h +++ b/Nuake/src/Core/Physics/DynamicWorld.h @@ -42,13 +42,13 @@ namespace Nuake Ref _JoltPhysicsSystem; JPH::JobSystemThreadPool* _JoltJobSystem; - Scope _contactListener; - Scope _bodyActivationListener; + Ref _contactListener; + Ref _bodyActivationListener; JPH::BodyInterface* _JoltBodyInterface; BPLayerInterfaceImpl* _JoltBroadphaseLayerInterface; std::vector _registeredBodies; - std::map _registeredCharacters; + std::map> _registeredCharacters; public: DynamicWorld(); diff --git a/Nuake/src/Core/Physics/PhysicsManager.cpp b/Nuake/src/Core/Physics/PhysicsManager.cpp index c7cc2ea4..620c5b77 100644 --- a/Nuake/src/Core/Physics/PhysicsManager.cpp +++ b/Nuake/src/Core/Physics/PhysicsManager.cpp @@ -55,7 +55,7 @@ namespace Nuake JPH::Factory::sInstance = new JPH::Factory(); JPH::RegisterTypes(); - m_World = new Physics::DynamicWorld(); + m_World = CreateRef(); m_World->SetGravity(Vector3(0, -3, 0)); m_IsRunning = false; diff --git a/Nuake/src/Core/Physics/PhysicsManager.h b/Nuake/src/Core/Physics/PhysicsManager.h index 2946a036..f4074f07 100644 --- a/Nuake/src/Core/Physics/PhysicsManager.h +++ b/Nuake/src/Core/Physics/PhysicsManager.h @@ -11,11 +11,12 @@ namespace Nuake class PhysicsManager { private: - Physics::DynamicWorld* m_World; + Ref m_World; bool m_IsRunning = false; bool m_DrawDebug = false; static PhysicsManager* m_Instance; + public: static PhysicsManager& Get() { @@ -23,11 +24,12 @@ namespace Nuake return instance; } - Physics::DynamicWorld* GetWorld() { return m_World; } + Ref GetWorld() { return m_World; } PhysicsManager() { if (!m_Instance) m_Instance = this; } - void SetDrawDebug(bool value) { + void SetDrawDebug(bool value) + { m_DrawDebug = value; }