Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Prevent degenerate case in DynamicBVH

Mike Precup662b2a56d174
diff --git a/core/math/dynamic_bvh.cpp b/core/math/dynamic_bvh.cpp
index a49f8ddb375..10753146d5e 100644
--- a/core/math/dynamic_bvh.cpp
+++ b/core/math/dynamic_bvh.cpp
@@ -67,7 +67,8 @@ void DynamicBVH::_insert_leaf(Node *p_root, Node *p_leaf) {
do {
p_root = p_root->children[p_leaf->volume.select_by_proximity(
p_root->children[0]->volume,
- p_root->children[1]->volume)];
+ p_root->children[1]->volume,
+ tiebreaker)];
} while (!p_root->is_leaf());
}
Node *prev = p_root->parent;
diff --git a/core/math/dynamic_bvh.h b/core/math/dynamic_bvh.h
index 085bbbd5e61..4838a895bf7 100644
--- a/core/math/dynamic_bvh.h
+++ b/core/math/dynamic_bvh.h
@@ -120,8 +120,14 @@ private:
return (Math::abs(d.x) + Math::abs(d.y) + Math::abs(d.z));
}
- _FORCE_INLINE_ int select_by_proximity(const Volume &p_a, const Volume &p_b) const {
- return (get_proximity_to(p_a) < get_proximity_to(p_b) ? 0 : 1);
+ _FORCE_INLINE_ int select_by_proximity(const Volume &p_a, const Volume &p_b, uint32_t &tiebreaker) const {
+ real_t prox_a = get_proximity_to(p_a);
+ real_t prox_b = get_proximity_to(p_b);
+ if (prox_a == prox_b) {
+ tiebreaker *= 1664525U;
+ return (tiebreaker % 48271U) & 1;
+ }
+ return prox_a < prox_b ? 0 : 1;
}
//
@@ -227,6 +233,7 @@ private:
int total_leaves = 0;
uint32_t opath = 0;
uint32_t index = 0;
+ uint32_t tiebreaker = 134775813U;
enum {
ALLOCA_STACK_SIZE = 128