Skip to content

Commit

Permalink
feat(bvh): feat hitbox bvh tree is now updated automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Silverlan committed Feb 17, 2024
1 parent d23dfdb commit 40f5e25
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ namespace pragma {
virtual void OnEntitySpawn() override;
void InitializeBvh();
bool IntersectionTest(const Vector3 &origin, const Vector3 &dir, float minDist, float maxDist, pragma::bvh::HitInfo &outHitInfo, const bvh::DebugDrawInfo *debugDrawInfo = nullptr);
void UpdateHitboxBvh();
void DebugDrawHitboxMeshes(BoneId boneId, float duration = 12.f) const;
bvh::HitboxBvhCache &GetGlobalBvhCache() const;
private:
Expand All @@ -92,6 +91,7 @@ namespace pragma {
void InitializeHitboxBvh();
bool InitializeModel();
void InitializeHitboxMeshBvhs();
void UpdateHitboxBvh();
std::shared_future<void> m_hitboxMeshCacheTask;
std::unordered_map<BoneId, std::vector<std::shared_ptr<bvh::MeshHitboxBvhCache>>> m_hitboxMeshBvhCaches;
std::shared_ptr<ObbBvhTree> m_hitboxBvh;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ void CHitboxBvhComponent::InitializeLuaObject(lua_State *l) { return BaseEntityC
void CHitboxBvhComponent::Initialize()
{
BaseEntityComponent::Initialize();
auto animC = GetEntity().AddComponent<CAnimatedComponent>();
if(animC.valid())
animC->SetSkeletonUpdateCallbacksEnabled(true);
BindEventUnhandled(BaseModelComponent::EVENT_ON_MODEL_CHANGED, [this](std::reference_wrapper<pragma::ComponentEvent> evData) { OnModelChanged(); });
BindEventUnhandled(CModelComponent::EVENT_ON_RENDER_MESHES_UPDATED, [this](std::reference_wrapper<pragma::ComponentEvent> evData) { InitializeHitboxMeshBvhs(); });
BindEventUnhandled(CAnimatedComponent::EVENT_ON_SKELETON_UPDATED, [this](std::reference_wrapper<pragma::ComponentEvent> evData) { UpdateHitboxBvh(); });
}

void CHitboxBvhComponent::InitializeBvh()
Expand Down Expand Up @@ -202,6 +206,9 @@ void CHitboxBvhComponent::UpdateHitboxBvh()
if(!animC)
return;

// Note: This is called on the animation thread (which is executed during rendering), but we don't need a mutex
// as raycasts must not occur during rendering anyway.

auto &bonePoses = animC->GetProcessedBones();
auto &hitboxBvh = m_hitboxBvh;
auto &bvh = m_hitboxBvh->bvh;
Expand Down

0 comments on commit 40f5e25

Please sign in to comment.