From a5ca094c5f297a9296886cc7236455d21e7f7966 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 16:37:06 -0400 Subject: [PATCH] Added trigger rigid bodies --- Nuake/src/Physics/DynamicWorld.cpp | 17 +++++++++++++---- Nuake/src/Physics/Rigibody.h | 10 +++++++--- Nuake/src/Scene/Systems/PhysicsSystem.cpp | 18 +++++++++++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index 2b65b61a..c07116b3 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -170,12 +170,21 @@ namespace Nuake //std::cout << "Contact validate callback" << std::endl; // Allows you to ignore a contact before it is created (using layers to not make objects collide is cheaper!) - return JPH::ValidateResult::AcceptAllContactsForThisBodyPair; + return JPH::ValidateResult::AcceptAllContactsForThisBodyPair; } virtual void OnContactAdded(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override { - //std::cout << "A contact was added" << std::endl; + auto entId1 = static_cast(inBody1.GetUserData()); + Entity entity1 = Engine::GetCurrentScene()->GetEntityByID(entId1); + + auto entId2 = static_cast(inBody2.GetUserData()); + Entity entity2 = Engine::GetCurrentScene()->GetEntityByID(entId2); + + const std::string entity1Name = entity1.GetComponent().Name; + const std::string entity2Name = entity2.GetComponent().Name; + + Logger::Log("Collision detected between " + entity1Name + " and " + entity2Name); } virtual void OnContactPersisted(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override @@ -314,8 +323,9 @@ namespace Nuake const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w); const auto& joltPos = JPH::Vec3(startPos.x, startPos.y, startPos.z); auto joltShape = GetJoltShape(rb->GetShape()); - JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, layer); + JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, layer); + bodySettings.mIsSensor = rb->IsTrigger(); bodySettings.mAllowedDOFs = (JPH::EAllowedDOFs::All); if (rb->GetLockXAxis()) @@ -372,7 +382,6 @@ namespace Nuake settings->mPenetrationRecoverySpeed = 1.0f; settings->mPredictiveContactDistance = 0.01f; settings->mShape = GetJoltShape(cc->Shape); - auto joltPosition = JPH::Vec3(cc->Position.x, cc->Position.y, cc->Position.z); const Quat& bodyRotation = cc->Rotation; diff --git a/Nuake/src/Physics/Rigibody.h b/Nuake/src/Physics/Rigibody.h index 8344a9b0..a6282569 100644 --- a/Nuake/src/Physics/Rigibody.h +++ b/Nuake/src/Physics/Rigibody.h @@ -20,6 +20,7 @@ namespace Nuake Quat _rotation; Entity _entity; + bool _isTrigger = false; bool m_LockXAxis = false; bool m_LockYAxis = false; bool m_LockZAxis = false; @@ -33,13 +34,16 @@ namespace Nuake void UpdateTransform(); + void SetIsTrigger(bool isTrigger) { _isTrigger = isTrigger; } + bool IsTrigger() const { return _isTrigger; } + bool GetLockXAxis() const { return m_LockXAxis; } bool GetLockYAxis() const { return m_LockYAxis; } bool GetLockZAxis() const { return m_LockZAxis; } - void setLockXAxis(bool lock) { m_LockXAxis = lock; } - void setLockYAxis(bool lock) { m_LockYAxis = lock; } - void setLockZAxis(bool lock) { m_LockZAxis = lock; } + void SetLockXAxis(bool lock) { m_LockXAxis = lock; } + void SetLockYAxis(bool lock) { m_LockYAxis = lock; } + void SetLockZAxis(bool lock) { m_LockZAxis = lock; } void SetEntityID(Entity ent); Vector3 GetPosition() const { return _position; } diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index e0bd94c7..bb54d874 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -219,6 +219,8 @@ namespace Nuake Entity ent = Entity({ e, m_Scene }); Ref rigidBody; Ref shape; + + bool isTrigger = false; if (rigidBodyComponent.GetRigidBody()) { continue; @@ -227,6 +229,7 @@ namespace Nuake if (ent.HasComponent()) { BoxColliderComponent& boxComponent = ent.GetComponent(); + isTrigger = boxComponent.IsTrigger; shape = CreateRef(boxComponent.Size); } @@ -235,6 +238,7 @@ namespace Nuake auto& capsuleComponent = ent.GetComponent(); float radius = capsuleComponent.Radius; float height = capsuleComponent.Height; + isTrigger = capsuleComponent.IsTrigger; shape = CreateRef(radius, height); } @@ -243,12 +247,14 @@ namespace Nuake auto& cylinderComponent = ent.GetComponent(); float radius = cylinderComponent.Radius; float height = cylinderComponent.Height; + isTrigger = cylinderComponent.IsTrigger; shape = CreateRef(radius, height); } if (ent.HasComponent()) { const auto& component = ent.GetComponent(); + isTrigger = component.IsTrigger; shape = CreateRef(component.Radius); } @@ -262,6 +268,8 @@ namespace Nuake const auto& modelComponent = ent.GetComponent(); const auto& component = ent.GetComponent(); + isTrigger = component.IsTrigger; + if (modelComponent.ModelResource) { uint32_t subMeshId = component.SubMesh; @@ -282,10 +290,14 @@ namespace Nuake } rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), shape, ent); - rigidBody->setLockXAxis(rigidBodyComponent.LockX); - rigidBody->setLockYAxis(rigidBodyComponent.LockY); - rigidBody->setLockZAxis(rigidBodyComponent.LockZ); + rigidBody->SetLockXAxis(rigidBodyComponent.LockX); + rigidBody->SetLockYAxis(rigidBodyComponent.LockY); + rigidBody->SetLockZAxis(rigidBodyComponent.LockZ); + + rigidBody->SetIsTrigger(isTrigger); + PhysicsManager::Get().RegisterBody(rigidBody); + rigidBodyComponent.Rigidbody = rigidBody; } }