Fixed bug where AABB nodes were not updating on collider size change

This commit is contained in:
Diren D Bharwani 2023-03-01 02:58:05 +08:00
parent abdf614083
commit f620ef226e
6 changed files with 39 additions and 12 deletions

View File

@ -1,4 +1,4 @@
Start in Fullscreen: false
Starting Scene ID: 97402985
Starting Scene ID: 92914350
Window Size: {x: 1920, y: 1080}
Window Title: SHADE Engine

View File

@ -1,4 +1,4 @@
Start Maximized: true
Working Scene ID: 97402985
Working Scene ID: 92914350
Window Size: {x: 1920, y: 1080}
Style: 0

View File

@ -21,7 +21,7 @@
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: 0, z: 0}
Translate: {x: 0.5, y: 0, z: 0}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
@ -31,7 +31,7 @@
- Is Trigger: false
Collision Tag: 1
Type: Sphere
Radius: 2
Radius: 3
Friction: 0.400000006
Bounciness: 0
Density: 1

View File

@ -170,20 +170,16 @@ namespace SHADE
insertLeaf(NEW_INDEX);
}
void SHAABBTree::Update(SHCollisionShapeID id, const SHAABB& AABB)
void SHAABBTree::Update(SHCollisionShapeID id, const SHAABB& newAABB)
{
// Get node index
const int32_t INDEX_TO_UPDATE = nodeMap[id];
Node& nodeToUpdate = nodes[INDEX_TO_UPDATE];
// If new AABB has not moved enough, skip.
if (nodeToUpdate.AABB.Contains(AABB))
return;
removeLeaf(INDEX_TO_UPDATE);
nodeToUpdate.AABB = AABB;
// Update the AABB directly
const SHAABB OLD_AABB = nodeToUpdate.AABB;
nodeToUpdate.AABB = newAABB;
// Fatten the AABB
const SHVec3 EXTENSION{ AABB_EXTENSION };
@ -194,6 +190,13 @@ namespace SHADE
nodeToUpdate.AABB.SetMin(newMin);
nodeToUpdate.AABB.SetMax(newMax);
// If new AABB has not moved enough, skip.
// We only modify the position &/ size, but the AABB remains within this space.
if (OLD_AABB.Contains(nodeToUpdate.AABB))
return;
// Re-insert the node to find it's new neighbour
removeLeaf(INDEX_TO_UPDATE);
insertLeaf(INDEX_TO_UPDATE);
}

View File

@ -206,6 +206,10 @@ namespace SHADE
// Recompute Relative radius
relativeExtents = 2.0f * Extents / scale;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
}
void SHBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept
@ -214,6 +218,10 @@ namespace SHADE
// Recompute world radius
Extents = relativeExtents * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
}
void SHBox::SetScale(const SHVec3& newScale) noexcept
@ -222,6 +230,10 @@ namespace SHADE
// Recompute world radius
Extents = relativeExtents * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
}
/*-----------------------------------------------------------------------------------*/

View File

@ -167,6 +167,10 @@ namespace SHADE
// Recompute Relative radius
relativeRadius = 2.0f * Radius / scale;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
}
void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept
@ -175,6 +179,10 @@ namespace SHADE
// Recompute world radius
Radius = relativeRadius * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
}
void SHSphere::SetScale(float maxScale) noexcept
@ -183,6 +191,10 @@ namespace SHADE
// Recompute world radius
Radius = relativeRadius * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
}
/*-----------------------------------------------------------------------------------*/