Collision with a quad terrain
Posted: Sun Jan 13, 2013 3:25 am
Hi,
I have a terrain partitioned into a quadtree and the rendering is fast, but collision detection for physics is slow.
I am trying to test an OBB against the terrain as follows:
Basically, if the node is in the viewing frustum, and we are at the leaf node, I check if the OBB is above that visible node's AABB. This is a fast SAT test that doesn't affect the frame rate.
Now that there are 2192 triangles in the node that the OBB is above, it is inefficient to perform the SAT test on every one of those triangles and it is slowing the frame rate considerably for physics.
Does anyone have any suggestions on how I would go about performing the collision detection. Should I build a smaller quadtree out of those triangles and do the collision tests?
I have a terrain partitioned into a quadtree and the rendering is fast, but collision detection for physics is slow.
I am trying to test an OBB against the terrain as follows:
Basically, if the node is in the viewing frustum, and we are at the leaf node, I check if the OBB is above that visible node's AABB. This is a fast SAT test that doesn't affect the frame rate.
Now that there are 2192 triangles in the node that the OBB is above, it is inefficient to perform the SAT test on every one of those triangles and it is slowing the frame rate considerably for physics.
Does anyone have any suggestions on how I would go about performing the collision detection. Should I build a smaller quadtree out of those triangles and do the collision tests?
Code: Select all
void CollisionQuadtreeNode *pNode, Plane *pFrustumPlanes, const OBB &obb)
{
// Check if node is in the view frustum
if(pNode->aabb.cull(pFrustumPlanes)==CULLED) {
return;
}
int nCountChilds = 0;
for(int i=0; i<4; ++i) {
if(pNode->pNodes[i]!=NULL) {
nCountChilds++;
testOBBIntersect(pNode->pNodes[i], pFrustumPlanes, obb, pContactData);
}
}
// This is not a leaf node as it has children
if(nCountChilds>0)
return;
// Test for overlap on the x-z plane
OBB tmpOBB = obb;
tmpOBB.m_vCenter.y = pNode->aabb.m_vCenter.y;
// Check if OBB is above the terrain node
if(!pNode->aabb.intersects(tmpOBB))
return;
for(int i=0; i<pNode->iNumTriangles; ++i) {
if(!pNode->triangleList[i].m_AABB.intersects(obb))
continue;
// Collision with triangle's AABB occurred
break;
}
return;
}