feat: implement contact listeners
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <destrum/Physics/JoltPhysicsWorld.h>
|
||||
#include <destrum/Components/Physics/SphereCollider.h>
|
||||
#include <destrum/Components/Physics/BoxCollider.h>
|
||||
#include <destrum/Components/Physics/Collider.h>
|
||||
#include <destrum/Components/Animator.h>
|
||||
#include <destrum/Components/Physics/Rigidbody.h>
|
||||
#include <destrum/Graphics/Material.h>
|
||||
@@ -483,6 +484,65 @@ namespace {
|
||||
check(!AssetReference::fromCacheKey("CharacterMedium.fbx").has_value(),
|
||||
"plain cache names must not be treated as file-backed assets");
|
||||
}
|
||||
|
||||
class TriggerTracker final : public Component {
|
||||
public:
|
||||
explicit TriggerTracker(GameObject& owner)
|
||||
: Component(owner, "TriggerTracker") {}
|
||||
|
||||
void Update(float) override {}
|
||||
std::string GetTypeName() const override { return "TriggerTracker"; }
|
||||
|
||||
void OnTriggerEnter(GameObject* other) override {
|
||||
++enterCount;
|
||||
lastOther = other;
|
||||
enterObjectName = other ? other->GetName() : "null";
|
||||
}
|
||||
|
||||
void OnTriggerExit(GameObject* other) override {
|
||||
++exitCount;
|
||||
}
|
||||
|
||||
int enterCount{0};
|
||||
int exitCount{0};
|
||||
GameObject* lastOther{nullptr};
|
||||
std::string enterObjectName;
|
||||
};
|
||||
|
||||
void testTriggerZone()
|
||||
{
|
||||
Scene& scene = SceneManager::GetInstance().CreateScene("trigger-test");
|
||||
|
||||
// Trigger box at origin (Static rigidbody + BoxCollider as trigger).
|
||||
GameObject* triggerObj = scene.CreateGameObject("TriggerZone");
|
||||
triggerObj->AddComponent<BoxCollider>(glm::vec3{3.0f, 3.0f, 3.0f});
|
||||
triggerObj->GetComponent<BoxCollider>()->SetTrigger(true);
|
||||
auto* triggerRb = triggerObj->AddComponent<Rigidbody>();
|
||||
triggerRb->SetType(RigidbodyType::Static);
|
||||
auto* tracker = triggerObj->AddComponent<TriggerTracker>();
|
||||
|
||||
// Falling sphere above the trigger.
|
||||
GameObject* sphere = scene.CreateGameObject("FallingSphere");
|
||||
sphere->AddComponent<SphereCollider>(0.5f);
|
||||
sphere->AddComponent<Rigidbody>();
|
||||
sphere->GetTransform().SetWorldPosition({0.0f, 8.0f, 0.0f});
|
||||
|
||||
scene.CommitPendingAdditions();
|
||||
|
||||
// Run physics for enough steps that the sphere falls into the trigger.
|
||||
for (int step = 0; step < 120; ++step) {
|
||||
scene.FixedUpdate(1.0f / 60.0f);
|
||||
}
|
||||
|
||||
check(tracker->enterCount >= 1,
|
||||
"falling sphere must trigger OnTriggerEnter at least once");
|
||||
check(tracker->exitCount >= 1,
|
||||
"falling sphere passing through trigger must exit");
|
||||
check(tracker->enterObjectName == "FallingSphere",
|
||||
"OnTriggerEnter must report the correct entering object");
|
||||
|
||||
SceneManager::GetInstance().Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
@@ -503,6 +563,7 @@ int main()
|
||||
testAnimatorAssetReferences();
|
||||
testSimpleColorMaterial();
|
||||
testAssetReferenceCacheKeys();
|
||||
testTriggerZone();
|
||||
std::cout << "destrum tests passed\n";
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user