Added collision callback thread safe queue

This commit is contained in:
Antoine Pilote
2024-04-01 13:19:33 -04:00
parent 72e2b6984e
commit 0693ac1ad0
3 changed files with 32 additions and 39 deletions

View File

@@ -124,7 +124,15 @@ namespace Nuake
// An example contact listener
class MyContactListener : public JPH::ContactListener
{
private:
Physics::DynamicWorld* _World;
public:
MyContactListener(Physics::DynamicWorld* world)
: _World(world)
{
}
// See: ContactListener
virtual JPH::ValidateResult OnContactValidate(const JPH::Body& inBody1, const JPH::Body& inBody2, JPH::RVec3Arg inBaseOffset, const JPH::CollideShapeResult& inCollisionResult) override
{
@@ -146,6 +154,9 @@ namespace Nuake
const std::string entity2Name = entity2.GetComponent<NameComponent>().Name;
Logger::Log("Collision detected between " + entity1Name + " and " + entity2Name);
Physics::CollisionCallbackData data;
_World->RegisterCollisionCallback(std::move(data));
}
virtual void OnContactPersisted(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override
@@ -217,7 +228,6 @@ namespace Nuake
}
};
BPLayerInterfaceImpl JoltBroadphaseLayerInterface = BPLayerInterfaceImpl();
ObjectVsBroadPhaseLayerFilterImpl JoltObjectVSBroadphaseLayerFilter = ObjectVsBroadPhaseLayerFilterImpl();
ObjectLayerPairFilterImpl JoltObjectVSObjectLayerFilter;
@@ -246,7 +256,7 @@ namespace Nuake
// A contact listener gets notified when bodies (are about to) collide, and when they separate again.
// Note that this is called from a job so whatever you do here needs to be thread safe.
// Registering one is entirely optional.
_contactListener = CreateScope<MyContactListener>();
_contactListener = CreateScope<MyContactListener>(this);
_JoltPhysicsSystem->SetContactListener(_contactListener.get());
// The main way to interact with the bodies in the physics system is through the body interface. There is a locking and a non-locking
@@ -667,6 +677,14 @@ namespace Nuake
}
}
void DynamicWorld::RegisterCollisionCallback(const CollisionCallbackData& data)
{
// This will be called from multiple threads
std::scoped_lock<std::mutex> lock(_CollisionCallbackMutex);
_CollisionCallbacks.push_back(std::move(data));
}
void DynamicWorld::MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity)
{
const uint32_t entityHandle = entity.GetHandle();

View File

@@ -41,6 +41,14 @@ namespace Nuake
uint32_t Ghost;
};
struct CollisionCallbackData
{
uint32_t Entity1;
uint32_t Entity2;
Vector3 Normal;
Vector3 Position;
};
class DynamicWorld
{
private:
@@ -56,6 +64,8 @@ namespace Nuake
std::vector<uint32_t> _registeredBodies;
std::map<uint32_t, CharacterGhostPair> _registeredCharacters;
std::mutex _CollisionCallbackMutex;
std::vector<CollisionCallbackData> _CollisionCallbacks;
public:
DynamicWorld();
@@ -75,6 +85,8 @@ namespace Nuake
void StepSimulation(Timestep ts);
void Clear();
void RegisterCollisionCallback(const CollisionCallbackData& data);
private:
JPH::Ref<JPH::Shape> GetJoltShape(const Ref<PhysicShape> shape);
void SyncEntitiesTranforms();

View File

@@ -52,12 +52,6 @@ namespace Nuake
{
auto [transform, brush] = brushes.get<TransformComponent, BSPBrushComponent>(e);
for (auto& r : brush.Rigidbody)
{
//r->m_Transform->setOrigin(btVector3(transform.GlobalTranslation.x, transform.GlobalTranslation.y, transform.GlobalTranslation.z));
//r->UpdateTransform(*r->m_Transform);
}
if (!brush.IsFunc)
continue;
@@ -73,37 +67,6 @@ namespace Nuake
}
}
}
//auto bspTriggerView = m_Scene->m_Registry.view<TransformComponent, BSPBrushComponent, TriggerZone>();
//for (auto e : bspTriggerView)
//{
// auto [transform, brush, trigger] = bspTriggerView.get<TransformComponent, BSPBrushComponent, TriggerZone>(e);
// trigger.GhostObject->ScanOverlap();
// brush.Targets.clear();
// auto targetnameView = m_Scene->m_Registry.view<TransformComponent, NameComponent>();
// for (auto e2 : targetnameView)
// {
// auto [ttransform, name] = targetnameView.get<TransformComponent, NameComponent>(e2);
// if (name.Name == brush.target) {
// brush.Targets.push_back(Entity{ e2, m_Scene });
// }
// }
//}
/*auto physicGroup = m_Scene->m_Registry.view<TransformComponent, RigidBodyComponent>();
for (auto e : physicGroup) {
auto [transform, rb] = physicGroup.get<TransformComponent, RigidBodyComponent>(e);
rb.SyncTransformComponent(&m_Scene->m_Registry.get<TransformComponent>(e));
}*/
//auto ccGroup = m_Scene->m_Registry.view<TransformComponent, CharacterControllerComponent>();
//for (auto e : ccGroup) {
// auto [transform, rb] = ccGroup.get<TransformComponent, CharacterControllerComponent>(e);
// rb.SyncWithTransform(m_Scene->m_Registry.get<TransformComponent>(e));
//}
}
void PhysicsSystem::FixedUpdate(Timestep ts)