BVH fix pairing AABB init and mask checks

Fix bug whereby AABBs were reused from previous items due to use of a pool, resulting in missed collisions.
Also use full mask collision checks for all cases except generic update.
This commit is contained in:
lawnjelly 2021-01-28 13:48:15 +00:00
parent bc90bcb65c
commit df18f72384
2 changed files with 18 additions and 9 deletions

View file

@ -90,10 +90,10 @@ public:
BVHHandle create(T *p_userdata, const AABB &p_aabb = AABB(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t p_pairable_mask = 1) { BVHHandle create(T *p_userdata, const AABB &p_aabb = AABB(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t p_pairable_mask = 1) {
// not completely sure why, but the order of the pairing callbacks seems to be causing problems in // not sure if absolutely necessary to flush collisions here. It will cost performance to, instead
// the render tree unless these are flushed before creating a new object // of waiting for update, so only uncomment this if there are bugs.
if (USE_PAIRS) { if (USE_PAIRS) {
_check_for_collisions(); //_check_for_collisions();
} }
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
@ -107,7 +107,15 @@ public:
BVHHandle h = tree.item_add(p_userdata, p_aabb, p_subindex, p_pairable, p_pairable_type, p_pairable_mask); BVHHandle h = tree.item_add(p_userdata, p_aabb, p_subindex, p_pairable, p_pairable_type, p_pairable_mask);
if (USE_PAIRS) { if (USE_PAIRS) {
_add_changed_item(h, p_aabb); // for safety initialize the expanded AABB
AABB &expanded_aabb = tree._pairs[h.id()].expanded_aabb;
expanded_aabb = p_aabb;
expanded_aabb.grow_by(tree._pairing_expansion);
// force a collision check no matter the AABB
_add_changed_item(h, p_aabb, false);
_check_for_collisions(true);
} }
return h; return h;
@ -170,6 +178,8 @@ public:
} }
tree.item_remove(p_handle); tree.item_remove(p_handle);
_check_for_collisions(true);
} }
// call e.g. once per frame (this does a trickle optimize) // call e.g. once per frame (this does a trickle optimize)
@ -192,11 +202,9 @@ public:
if (USE_PAIRS) { if (USE_PAIRS) {
// not completely sure why, but the order of the pairing callbacks seems to be causing problems in // not sure if absolutely necessary to flush collisions here. It will cost performance to, instead
// the render tree unless these are flushed before creating a new object.. we will do this for set_pairable // of waiting for update, so only uncomment this if there are bugs.
// to just in case. This may not be required, and may slow things down slightly when there are a lot //_check_for_collisions();
// of visibility changes in a frame
_check_for_collisions();
// when the pairable state changes, we need to force a collision check because newly pairable // when the pairable state changes, we need to force a collision check because newly pairable
// items may be in collision, and unpairable items might move out of collision. // items may be in collision, and unpairable items might move out of collision.

View file

@ -14,6 +14,7 @@ struct ItemPairs {
void clear() { void clear() {
num_pairs = 0; num_pairs = 0;
extended_pairs.reset(); extended_pairs.reset();
expanded_aabb = AABB();
} }
AABB expanded_aabb; AABB expanded_aabb;