56 lines
1.7 KiB
C++
56 lines
1.7 KiB
C++
#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)
|
|
: m_World(std::move(world)) {
|
|
}
|
|
|
|
void PhysicsSceneBridge::RegisterGameObject(GameObject& object) {
|
|
if (!m_World) return;
|
|
if (auto* rb = object.GetComponent<Rigidbody>()) {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|