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 Start in Fullscreen: false
Starting Scene ID: 97402985 Starting Scene ID: 92914350
Window Size: {x: 1920, y: 1080} Window Size: {x: 1920, y: 1080}
Window Title: SHADE Engine Window Title: SHADE Engine

View File

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

View File

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

View File

@ -170,20 +170,16 @@ namespace SHADE
insertLeaf(NEW_INDEX); insertLeaf(NEW_INDEX);
} }
void SHAABBTree::Update(SHCollisionShapeID id, const SHAABB& AABB) void SHAABBTree::Update(SHCollisionShapeID id, const SHAABB& newAABB)
{ {
// Get node index // Get node index
const int32_t INDEX_TO_UPDATE = nodeMap[id]; const int32_t INDEX_TO_UPDATE = nodeMap[id];
Node& nodeToUpdate = nodes[INDEX_TO_UPDATE]; Node& nodeToUpdate = nodes[INDEX_TO_UPDATE];
// If new AABB has not moved enough, skip. // Update the AABB directly
if (nodeToUpdate.AABB.Contains(AABB)) const SHAABB OLD_AABB = nodeToUpdate.AABB;
return; nodeToUpdate.AABB = newAABB;
removeLeaf(INDEX_TO_UPDATE);
nodeToUpdate.AABB = AABB;
// Fatten the AABB // Fatten the AABB
const SHVec3 EXTENSION{ AABB_EXTENSION }; const SHVec3 EXTENSION{ AABB_EXTENSION };
@ -194,6 +190,13 @@ namespace SHADE
nodeToUpdate.AABB.SetMin(newMin); nodeToUpdate.AABB.SetMin(newMin);
nodeToUpdate.AABB.SetMax(newMax); 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); insertLeaf(INDEX_TO_UPDATE);
} }

View File

@ -206,6 +206,10 @@ namespace SHADE
// Recompute Relative radius // Recompute Relative radius
relativeExtents = 2.0f * Extents / scale; 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 void SHBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept
@ -214,6 +218,10 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Extents = relativeExtents * scale * 0.5f; 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 void SHBox::SetScale(const SHVec3& newScale) noexcept
@ -222,6 +230,10 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Extents = relativeExtents * scale * 0.5f; 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 // Recompute Relative radius
relativeRadius = 2.0f * Radius / scale; 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 void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept
@ -175,6 +179,10 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Radius = relativeRadius * scale * 0.5f; 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 void SHSphere::SetScale(float maxScale) noexcept
@ -183,6 +191,10 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Radius = relativeRadius * scale * 0.5f; Radius = relativeRadius * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/