feat: implement contact listeners
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <destrum/Physics/PhysicsSceneBridge.h>
|
||||
|
||||
#include <destrum/Components/Physics/Rigidbody.h>
|
||||
#include <destrum/ObjectModel/Component.h>
|
||||
#include <destrum/ObjectModel/GameObject.h>
|
||||
|
||||
PhysicsSceneBridge::PhysicsSceneBridge(std::unique_ptr<PhysicsWorld> world)
|
||||
@@ -8,14 +9,16 @@ PhysicsSceneBridge::PhysicsSceneBridge(std::unique_ptr<PhysicsWorld> world)
|
||||
}
|
||||
|
||||
void PhysicsSceneBridge::RegisterGameObject(GameObject& object) {
|
||||
if (!m_World) return;
|
||||
if (auto* rb = object.GetComponent<Rigidbody>()) {
|
||||
if (!rb->HasPhysicsBody() || rb->GetPhysicsWorld() != &GetWorld()) {
|
||||
if (!rb->HasPhysicsBody() || rb->GetPhysicsWorld() != m_World.get()) {
|
||||
m_World->RegisterRigidbody(*rb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsSceneBridge::UnregisterGameObject(GameObject& object) {
|
||||
if (!m_World) return;
|
||||
if (auto* rb = object.GetComponent<Rigidbody>()) {
|
||||
if (rb->GetPhysicsWorld() == m_World.get()) {
|
||||
m_World->UnregisterRigidbody(*rb);
|
||||
@@ -24,13 +27,29 @@ void PhysicsSceneBridge::UnregisterGameObject(GameObject& object) {
|
||||
}
|
||||
|
||||
void PhysicsSceneBridge::RefreshGameObject(GameObject& object) {
|
||||
if (!m_World) return;
|
||||
if (auto* rb = object.GetComponent<Rigidbody>()) {
|
||||
m_World->RefreshRigidbody(*rb);
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsSceneBridge::FixedUpdate(float fixedDt) {
|
||||
if (!m_World) return;
|
||||
m_World->SyncKinematicBodiesToPhysics();
|
||||
m_World->Step(fixedDt);
|
||||
|
||||
for (const auto& event : m_World->ConsumeTriggerEvents()) {
|
||||
if (event.owner == nullptr) continue;
|
||||
for (const auto& component : event.owner->GetComponents()) {
|
||||
if (component && !component->IsBeingDestroyed() && component->isEnabled()) {
|
||||
if (event.entered) {
|
||||
component->OnTriggerEnter(event.other);
|
||||
} else {
|
||||
component->OnTriggerExit(event.other);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_World->SyncDynamicBodiesToTransforms();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user