Merge pull request #3746 from sheepandshepherd/octree-aabb

Correct octree's AABB intersect test, fixes #3576 and #3253
This commit is contained in:
Rémi Verschelde 2016-02-17 21:25:08 +01:00
commit c250ee7a04
2 changed files with 24 additions and 6 deletions

View File

@ -66,6 +66,7 @@ public:
bool operator!=(const AABB& p_rval) const; bool operator!=(const AABB& p_rval) const;
_FORCE_INLINE_ bool intersects(const AABB& p_aabb) const; /// Both AABBs overlap _FORCE_INLINE_ bool intersects(const AABB& p_aabb) const; /// Both AABBs overlap
_FORCE_INLINE_ bool intersects_inclusive(const AABB& p_aabb) const; /// Both AABBs (or their faces) overlap
_FORCE_INLINE_ bool encloses(const AABB & p_aabb) const; /// p_aabb is completely inside this _FORCE_INLINE_ bool encloses(const AABB & p_aabb) const; /// p_aabb is completely inside this
AABB merge(const AABB& p_with) const; AABB merge(const AABB& p_with) const;
@ -126,6 +127,23 @@ inline bool AABB::intersects(const AABB& p_aabb) const {
return true; return true;
} }
inline bool AABB::intersects_inclusive(const AABB& p_aabb) const {
if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
return false;
if ( (pos.x+size.x) < p_aabb.pos.x )
return false;
if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
return false;
if ( (pos.y+size.y) < p_aabb.pos.y )
return false;
if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
return false;
if ( (pos.z+size.z) < p_aabb.pos.z )
return false;
return true;
}
inline bool AABB::encloses(const AABB & p_aabb) const { inline bool AABB::encloses(const AABB & p_aabb) const {

View File

@ -201,7 +201,7 @@ private:
_FORCE_INLINE_ void _pair_check(PairData *p_pair) { _FORCE_INLINE_ void _pair_check(PairData *p_pair) {
bool intersect=p_pair->A->aabb.intersects( p_pair->B->aabb ); bool intersect=p_pair->A->aabb.intersects_inclusive( p_pair->B->aabb );
if (intersect!=p_pair->intersect) { if (intersect!=p_pair->intersect) {
@ -480,7 +480,7 @@ void Octree<T,use_pairs,AL>::_insert_element(Element *p_element,Octant *p_octant
if (p_octant->children[i]) { if (p_octant->children[i]) {
/* element exists, go straight to it */ /* element exists, go straight to it */
if (p_octant->children[i]->aabb.intersects( p_element->aabb ) ) { if (p_octant->children[i]->aabb.intersects_inclusive( p_element->aabb ) ) {
_insert_element( p_element, p_octant->children[i] ); _insert_element( p_element, p_octant->children[i] );
splits++; splits++;
} }
@ -497,7 +497,7 @@ void Octree<T,use_pairs,AL>::_insert_element(Element *p_element,Octant *p_octant
if (i&4) if (i&4)
aabb.pos.z+=aabb.size.z; aabb.pos.z+=aabb.size.z;
if (aabb.intersects( p_element->aabb) ) { if (aabb.intersects_inclusive( p_element->aabb) ) {
/* if actually intersects, create the child */ /* if actually intersects, create the child */
Octant *child = memnew_allocator( Octant, AL ); Octant *child = memnew_allocator( Octant, AL );
@ -1146,7 +1146,7 @@ void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const AABB& p_aabb, T**
continue; continue;
e->last_pass=pass; e->last_pass=pass;
if (p_aabb.intersects(e->aabb)) { if (p_aabb.intersects_inclusive(e->aabb)) {
if (*p_result_idx<p_result_max) { if (*p_result_idx<p_result_max) {
@ -1175,7 +1175,7 @@ void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const AABB& p_aabb, T**
continue; continue;
e->last_pass=pass; e->last_pass=pass;
if (p_aabb.intersects(e->aabb)) { if (p_aabb.intersects_inclusive(e->aabb)) {
if (*p_result_idx<p_result_max) { if (*p_result_idx<p_result_max) {
@ -1193,7 +1193,7 @@ void Octree<T,use_pairs,AL>::_cull_AABB(Octant *p_octant,const AABB& p_aabb, T**
for (int i=0;i<8;i++) { for (int i=0;i<8;i++) {
if (p_octant->children[i] && p_octant->children[i]->aabb.intersects(p_aabb)) { if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_inclusive(p_aabb)) {
_cull_AABB(p_octant->children[i],p_aabb, p_result_array,p_result_idx,p_result_max,p_subindex_array,p_mask); _cull_AABB(p_octant->children[i],p_aabb, p_result_array,p_result_idx,p_result_max,p_subindex_array,p_mask);
} }
} }