| 1 |
public: |
| 2 |
struct ItemRef { |
| 3 |
uint32_t tnode_id; // -1 is invalid |
| 4 |
uint32_t item_id; // in the leaf |
| 5 |
|
| 6 |
bool is_active() const { return tnode_id != BVHCommon::INACTIVE; } |
| 7 |
void set_inactive() { |
| 8 |
tnode_id = BVHCommon::INACTIVE; |
| 9 |
item_id = BVHCommon::INACTIVE; |
| 10 |
} |
| 11 |
}; |
| 12 |
|
| 13 |
// extra info kept in separate parallel list to the references, |
| 14 |
// as this is less used as keeps cache better |
| 15 |
struct ItemExtra { |
| 16 |
// Before doing user defined pairing checks (especially in the find_leavers function), |
| 17 |
// we may want to check that two items have compatible tree ids and tree masks, |
| 18 |
// as if they are incompatible they should not pair / collide. |
| 19 |
bool are_item_trees_compatible(const ItemExtra &p_other) const { |
| 20 |
uint32_t other_type = 1 << p_other.tree_id; |
| 21 |
if (tree_collision_mask & other_type) { |
| 22 |
return true; |
| 23 |
} |
| 24 |
uint32_t our_type = 1 << tree_id; |
| 25 |
if (p_other.tree_collision_mask & our_type) { |
| 26 |
return true; |
| 27 |
} |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
// There can be multiple user defined trees |
| 32 |
uint32_t tree_id; |
| 33 |
|
| 34 |
// Defines which trees this item should collision check against. |
| 35 |
// 1 << tree_id, and normally items would collide against there own |
| 36 |
// tree (but not always). |
| 37 |
uint32_t tree_collision_mask; |
| 38 |
|
| 39 |
uint32_t last_updated_tick; |
| 40 |
int32_t subindex; |
| 41 |
|
| 42 |
T *userdata; |
| 43 |
|
| 44 |
// the active reference is a separate list of which references |
| 45 |
// are active so that we can slowly iterate through it over many frames for |
| 46 |
// slow optimize. |
| 47 |
uint32_t active_ref_id; |
| 48 |
}; |
| 49 |
|
| 50 |
// tree leaf |
| 51 |
struct TLeaf { |
| 52 |
uint16_t num_items; |
| 53 |
|
| 54 |
private: |
| 55 |
uint16_t dirty; |
| 56 |
// separate data orientated lists for faster SIMD traversal |
| 57 |
uint32_t item_ref_ids[MAX_ITEMS]; |
| 58 |
BVHABB_CLASS aabbs[MAX_ITEMS]; |
| 59 |
|
| 60 |
public: |
| 61 |
// accessors |
| 62 |
BVHABB_CLASS &get_aabb(uint32_t p_id) { |
| 63 |
BVH_ASSERT(p_id < MAX_ITEMS); |
| 64 |
return aabbs[p_id]; |
| 65 |
} |
| 66 |
const BVHABB_CLASS &get_aabb(uint32_t p_id) const { |
| 67 |
BVH_ASSERT(p_id < MAX_ITEMS); |
| 68 |
return aabbs[p_id]; |
| 69 |
} |
| 70 |
|
| 71 |
uint32_t &get_item_ref_id(uint32_t p_id) { |
| 72 |
BVH_ASSERT(p_id < MAX_ITEMS); |
| 73 |
return item_ref_ids[p_id]; |
| 74 |
} |
| 75 |
const uint32_t &get_item_ref_id(uint32_t p_id) const { |
| 76 |
BVH_ASSERT(p_id < MAX_ITEMS); |
| 77 |
return item_ref_ids[p_id]; |
| 78 |
} |
| 79 |
|
| 80 |
bool is_dirty() const { return dirty; } |
| 81 |
void set_dirty(bool p) { dirty = p; } |
| 82 |
|
| 83 |
void clear() { |
| 84 |
num_items = 0; |
| 85 |
set_dirty(false); |
| 86 |
} |
| 87 |
bool is_full() const { return num_items >= MAX_ITEMS; } |
| 88 |
|
| 89 |
void remove_item_unordered(uint32_t p_id) { |
| 90 |
BVH_ASSERT(p_id < num_items); |
| 91 |
num_items--; |
| 92 |
aabbs[p_id] = aabbs[num_items]; |
| 93 |
item_ref_ids[p_id] = item_ref_ids[num_items]; |
| 94 |
} |
| 95 |
|
| 96 |
uint32_t request_item() { |
| 97 |
if (num_items < MAX_ITEMS) { |
| 98 |
uint32_t id = num_items; |
| 99 |
num_items++; |
| 100 |
return id; |
| 101 |
} |
| 102 |
#ifdef DEV_ENABLED |
| 103 |
return -1; |
| 104 |
#else |
| 105 |
ERR_FAIL_V_MSG(0, "BVH request_item error."); |
| 106 |
#endif |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
// tree node |
| 111 |
struct TNode { |
| 112 |
BVHABB_CLASS aabb; |
| 113 |
// either number of children if positive |
| 114 |
// or leaf id if negative (leaf id 0 is disallowed) |
| 115 |
union { |
| 116 |
int32_t num_children; |
| 117 |
int32_t neg_leaf_id; |
| 118 |
}; |
| 119 |
uint32_t parent_id; // or -1 |
| 120 |
uint16_t children[MAX_CHILDREN]; |
| 121 |
|
| 122 |
// height in the tree, where leaves are 0, and all above are 1+ |
| 123 |
// (or the highest where there is a tie off) |
| 124 |
int32_t height; |
| 125 |
|
| 126 |
bool is_leaf() const { return num_children < 0; } |
| 127 |
void set_leaf_id(int id) { neg_leaf_id = -id; } |
| 128 |
int get_leaf_id() const { return -neg_leaf_id; } |
| 129 |
|
| 130 |
void clear() { |
| 131 |
num_children = 0; |
| 132 |
parent_id = BVHCommon::INVALID; |
| 133 |
height = 0; // or -1 for testing |
| 134 |
|
| 135 |
// for safety set to improbable value |
| 136 |
aabb.set_to_max_opposite_extents(); |
| 137 |
|
| 138 |
// other members are not blanked for speed .. they may be uninitialized |
| 139 |
} |
| 140 |
|
| 141 |
bool is_full_of_children() const { return num_children >= MAX_CHILDREN; } |
| 142 |
|
| 143 |
void remove_child_internal(uint32_t child_num) { |
| 144 |
children[child_num] = children[num_children - 1]; |
| 145 |
num_children--; |
| 146 |
} |
| 147 |
|
| 148 |
int find_child(uint32_t p_child_node_id) { |
| 149 |
BVH_ASSERT(!is_leaf()); |
| 150 |
|
| 151 |
for (int n = 0; n < num_children; n++) { |
| 152 |
if (children[n] == p_child_node_id) { |
| 153 |
return n; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// not found |
| 158 |
return -1; |
| 159 |
} |
| 160 |
}; |
| 161 |
|
| 162 |
// instead of using linked list we maintain |
| 163 |
// item references (for quick lookup) |
| 164 |
PooledList<ItemRef, uint32_t, true> _refs; |
| 165 |
PooledList<ItemExtra, uint32_t, true> _extra; |
| 166 |
PooledList<ItemPairs> _pairs; |
| 167 |
|
| 168 |
// these 2 are not in sync .. nodes != leaves! |
| 169 |
PooledList<TNode, uint32_t, true> _nodes; |
| 170 |
PooledList<TLeaf, uint32_t, true> _leaves; |
| 171 |
|
| 172 |
// we can maintain an un-ordered list of which references are active, |
| 173 |
// in order to do a slow incremental optimize of the tree over each frame. |
| 174 |
// This will work best if dynamic objects and static objects are in a different tree. |
| 175 |
LocalVector<uint32_t> _active_refs; |
| 176 |
uint32_t _current_active_ref = 0; |
| 177 |
|
| 178 |
// instead of translating directly to the userdata output, |
| 179 |
// we keep an intermediate list of hits as reference IDs, which can be used |
| 180 |
// for pairing collision detection |
| 181 |
LocalVector<uint32_t> _cull_hits; |
| 182 |
|
| 183 |
// We can now have a user definable number of trees. |
| 184 |
// This allows using e.g. a non-pairable and pairable tree, |
| 185 |
// which can be more efficient for example, if we only need check non pairable against the pairable tree. |
| 186 |
// It also may be more efficient in terms of separating static from dynamic objects, by reducing housekeeping. |
| 187 |
// However this is a trade off, as there is a cost of traversing two trees. |
| 188 |
uint32_t _root_node_id[NUM_TREES]; |
| 189 |
|
| 190 |
// these values may need tweaking according to the project |
| 191 |
// the bound of the world, and the average velocities of the objects |
| 192 |
|
| 193 |
// node expansion is important in the rendering tree |
| 194 |
// larger values give less re-insertion as items move... |
| 195 |
// but on the other hand over estimates the bounding box of nodes. |
| 196 |
// we can either use auto mode, where the expansion is based on the root node size, or specify manually |
| 197 |
real_t _node_expansion = 0.5; |
| 198 |
bool _auto_node_expansion = true; |
| 199 |
|
| 200 |
// pairing expansion important for physics pairing |
| 201 |
// larger values gives more 'sticky' pairing, and is less likely to exhibit tunneling |
| 202 |
// we can either use auto mode, where the expansion is based on the root node size, or specify manually |
| 203 |
real_t _pairing_expansion = 0.1; |
| 204 |
|
| 205 |
#ifdef BVH_ALLOW_AUTO_EXPANSION |
| 206 |
bool _auto_pairing_expansion = true; |
| 207 |
#endif |
| 208 |
|
| 209 |
// when using an expanded bound, we must detect the condition where a new AABB |
| 210 |
// is significantly smaller than the expanded bound, as this is a special case where we |
| 211 |
// should override the optimization and create a new expanded bound. |
| 212 |
// This threshold is derived from the _pairing_expansion, and should be recalculated |
| 213 |
// if _pairing_expansion is changed. |
| 214 |
real_t _aabb_shrinkage_threshold = 0.0; |