Public Group

# (Fixed)Help Needed with Frustums and AABB's

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, The problem is I can't get the camera's view frustum to detect AABB's that are in world coords, if I set the AABB up in object space the frustum will say all nodes are in but if I move the camera or rotate it away from the centre it will cull them so it looks like it sort of works when the AABB are in object space but the frustum is in world space what am I missing besides a brain help needed please. Heres the lay out. The Frustum planes are in world space sample code
D3DXMATRIX VP = (*mView) *(*m_MatProj);//just the projection is viewspace frustum

D3DXVECTOR4 col0(VP(0,0), VP(1,0), VP(2,0), VP(3,0));
D3DXVECTOR4 col1(VP(0,1), VP(1,1), VP(2,1), VP(3,1));
D3DXVECTOR4 col2(VP(0,2), VP(1,2), VP(2,2), VP(3,2));
D3DXVECTOR4 col3(VP(0,3), VP(1,3), VP(2,3), VP(3,3));

// Planes face inward.
mFrustumPlanes[0] = (D3DXPLANE)(col2);        // near
mFrustumPlanes[1] = (D3DXPLANE)(col3 - col2); // far
mFrustumPlanes[2] = (D3DXPLANE)(col3 + col0); // left
mFrustumPlanes[3] = (D3DXPLANE)(col3 - col0); // right
mFrustumPlanes[4] = (D3DXPLANE)(col3 - col1); // top
mFrustumPlanes[5] = (D3DXPLANE)(col3 + col1); // bottom

for(int i = 0; i < 6; i++)
D3DXPlaneNormalize(&mFrustumPlanes, &mFrustumPlanes);


The AABB has these bounds Min[-5120, -500, -5120] Max[5120, 500, 5120] is this Object or world space ????? I'm thinking worldspace The test function

//------------------------------------------------------------------------------
//this returns true if we can see this bit of the terrain
//-------------------------------------------------------------------------------
bool Camera::isVisible(AABB *box)
{
// Test assumes frustum planes face inward.

D3DXVECTOR3 P;
D3DXVECTOR3 Q;

//      N  *Q                    *P
//      | /                     /
//      |/                     /
// -----/----- Plane     -----/----- Plane
//     /                     / |
//    /                     /  |
//   *P                    *Q  N
//
// PQ forms diagonal most closely aligned with plane normal.

// For each frustum plane, find the box diagonal (there are four main
// diagonals that intersect the box center point) that points in the
// same direction as the normal along each axis (i.e., the diagonal
// that is most aligned with the plane normal).  Then test if the box
// is in front of the plane or not.
for(int i = 0; i < 6; ++i)
{
// For each coordinate axis x, y, z...
for(int j = 0; j < 3; ++j)
{
// Make PQ point in the same direction as the plane normal on this axis.
if( mFrustumPlanes[j] > 0.0f )
{
P[j] = box->minPt[j];
Q[j] = box->maxPt[j];
}
else
{
P[j] = box->maxPt[j];
Q[j] = box->minPt[j];
}
}

// If box is in negative half space, it is behind the plane, and thus, completely
// outside the frustum.  Note that because PQ points roughly in the direction of the
// plane normal, we can deduce that if Q is outside then P is also outside--thus we
// only need to test Q.
if( D3DXPlaneDotCoord(&mFrustumPlanes, &Q) < 0.0f  ) // outside
return false;
}
return true;
}//end isVisible


Image: you can see the mesh of the AABBS they look like there in the right location but none are added to the render list. if you need more code Ill post what ever you need if I have it that is thanks inadvance. [Edited by - ankhd on February 23, 2008 12:52:15 AM]

##### Share on other sites
Hi, well I think I found the error after 4 days(long days)

I was making a cube like this
ID3DXMesh *CreateCube(LPDIRECT3DDEVICE9 Device, AABB *aabb,//we also create its bounding box and return it here					  D3DXVECTOR3 *centre,//world of this cube					  RECT *rhs, float height)// D3DCOLOR colour){	ID3DXMesh *box = NULL;	//new stuff	D3DXCreateBox(Device,				abs(rhs->right - rhs->left), // width				height,  // height				 abs(rhs->bottom - rhs->top),//depth				&box,				0);	if(box == NULL)		return NULL;	D3DXMATRIX T;	//ok the box is created in object space but we dont want to translate the grid all the time so we will	//set each vertex to its world position and we dont need to trans form it again	VertexPN	*vb = 0;	(box->LockVertexBuffer(0, (void**)&vb));	for(UINT i = 0; i < box->GetNumVertices(); i++)	{				// Translate so that the center of the cube is at the		// specified 'center' parameter.						D3DXMatrixTranslation(&T, centre->x, centre->y, -centre->z);		D3DXVec3TransformCoord(&vb.pos, &vb.pos, &T);					}	HRESULT hr = D3DXComputeBoundingBox((D3DXVECTOR3*)vb, box->GetNumVertices(), 									box->GetNumBytesPerVertex(), &aabb->minPt, &aabb->maxPt);		(box->UnlockVertexBuffer());	if(FAILED(hr))		MessageBox(NULL, "Failed to Create AABB","Hello", MB_OK);		return box;//all ok}//end CreateCube////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

this function job is to make a cube so I could render it and then make the AABB
int world space. In the above code there is a line with this in it

D3DXMatrixTranslation(&T, centre->x, centre->y, centre->z);
D3DXVec3TransformCoord(&vb.pos, &vb.pos, &T);

I fixed it by setting the z value to -centre->z I found a post some one had years ago and it sort of hinted at a invers some where I gues it was here any way could someone please explane why that works..
Thanks.......

• 13
• 12
• 23
• 10
• 19