Sphere in Frustum

Started by
12 comments, last by steg 22 years, 2 months ago
Hi, I've implemented some View frustum culling (thanks to Mark Morleys tutorial), but in my SphereInFrustum method I pass in the objects world x,y and z position as opposed to the centre of the spheres x,y,z coordinate as he suggests but it still works perfectly, in fact, passing in the sphere's x,y,z coordinate gives strange results ?! Here is my method : BOOL CD3DManager::SphereInFrustum( float x, float y, float z, float radius ) { int p; for( p = 0; p < 6; p++ ) { if( frustum[p][0] * x + frustum[p][1] * y + frustum[p][2] * z + frustum[p][3] <= -radius ) return false; } return true; } This is how I precompute the objects sphere : j = 0; while(j!=OBJECTAMOUNT) { DWORD dwBufStart = g_Objects[j].nVertexBufferStart; d3dManager.SetBoundingSphere(&MyVertices[dwBufStart], g_Objects[j].dwVertexAmount, CUSTOM_VERTEX, g_Objects[j].pCenter,&g_Objects[j].radius); j++; } Then my rendering code which checks if object is visible (this shows I'm passing the objects world x,y,z position): j = 0; while(j!=OBJECTAMOUNT) { if(bCulling || d3dManager.SphereInFrustum(g_Objects[j].XPos, g_Objects[j].YPos, g_Objects[j].ZPos, g_Objects[j].radius )) { MakeWorldMatrix(&WorldMatrix, g_Objects[j]); g_pDevice->SetTransform( D3DTS_WORLD, &WorldMatrix ); g_pDevice->DrawPrimitive(g_Objects[j].nType, g_Objects[j].nVertexBufferStart, g_Objects[j].nVertexBufferEnd); } j++; } Just to re-iterate, I'm passing in the objects world x,y and z position - can anyone explain why this works ? If this is the case I don't need the sphere's x,y,z ?! Kind regards, sTeVe Edited by - steg on February 3, 2002 4:56:55 PM Edited by - steg on February 3, 2002 4:58:49 PM Edited by - steg on February 3, 2002 5:00:58 PM

If it isn't working, take a bath, have a think and try again...

Advertisement
Anyone ?!

If it isn't working, take a bath, have a think and try again...

The sphere may not in 0,0,0 in the object''s local coordinate frame, so you need to add them together.
Void,

What do you mean ?

Reg''ds,
sTeVe

If it isn't working, take a bath, have a think and try again...

I think void means that if your object isn''t centred at 0,0,0 you need to add that to your x,y,z parameters.

Neil


WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!
WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!
Sorry - I wasn''t as clear as i meant to be.

Try this

BOOL CD3DManager::SphereInFrustum( Object )
{
int p;

for( p = 0; p < 6; p++ )
{
if( frustum[p][0] * (Object.x+Object.pCenter.x) + frustum[p][1] * (Object.y+Object.pCenter.y) + frustum[p][2] * (Object.z+Object.pCenter.z) + frustum[p][3] <= -radius )
return false;
}
return true;
}

Neil

(not sure but you might have to subtract th centre pos - just done quickly for you)

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!
WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!
Thanks Thedo,

I''ll try it tonight when I get home from work if I have time!

Reg''ds,
sTeVe

If it isn't working, take a bath, have a think and try again...

Just had time to try it out, method now is :

BOOL CD3DManager::SphereInFrustum( float x, float y, float z, float radius, float rx, float ry, float rz )
{
int p;

for( p = 0; p < 6; p++ )
{
if( frustum[p][0] * (x + rx) + frustum[p][1]
* (y + ry) + frustum[p][2]
* (z + rz) + frustum[p][3] <= -radius )
return false;
}
return true;
}

where x,y,z are objects world coordinates and rx,ry,rz are the spheres centre coordinates, this works fine but am still confused why the other way also worked (i.e, not having the spheres centre coordinates) ?

Thanks,
sTeVe

If it isn't working, take a bath, have a think and try again...

Possibly your object was already centred at 0,0,0. That way the transforming by the centre coord (0,0,0) would produce exactly the same result. That would explain it. If not then I really don''t know.

Neil

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!
WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!
I hope you also didn''t calculate the frustum planes like mark morley because he''s using it for opengl. You need to take the column instead for directx.

This topic is closed to new replies.

Advertisement