• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# View Frustrum Cone Culling - how do I get the angle?

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

## Recommended Posts

Hello, yes, my math is bad - sorry. Still trying to relearn things I forgot 10 years ago when I was making my first 3d steps. I am implementing a terrain renderer - with mixed success so far. My next step is culling terrain-patches. I have implemented a sphere/sphere test (against a sphere surrounding the frustrum), and now I want to make a cone/sphere test, so that I can basically cull out objects "on the sides", too. So, my question - I have the formula here for testing whethere a sphere intersects a cone. I need, for this, from the cone: (a) the center (which is identical to my camera position) (b) the vector - which I can basically take as the vector of the view frustrum (c) the ANGLE - which I have a problem with. My Vew Frustrum is constructed from with the nice functions in Direct3d - how the **** do I get the correct angle now? Anyone can help me out? How can I get the angle that I need for the test? Oh - on a side problem: my bounding sphere for the view frustrum is wrong, too. I am basically making an "inner sphere" right now (center between near and far plane, distance to plane). It should be an outer sphere (to a far plane side point) - poit nis, though, how do I get this distance, too? Thanks in advance - and if anyone can recommend a good book on this mathematics, tell me :-) Regards Thomas Tomiczek THONA Consulting Ltd. (Microsoft MVP C#/.NET)

##### Share on other sites
hello,

i couldn''t answer your question though but i could recommend a good book! Mathematics for 3D Game Programming and Computer Graphics by Eric Lengyel

##### Share on other sites
The cone angle is simply the widest field of view.

If you have any aspect ratio factor greater than 1.0, then you''ll need to adjust for that, but otherwise FOV should work.

Remember that you''ll also need to account for the near and far clipping planes which are really just distances along the view vector so a dot product and a couple of comparisons should suffice for testing those.

If you don''t have the field of view (e.g. you use one of the D3DXMatrixPerspectiveLH() type calls rather than the D3DXMatrixPerspectiveFovLH() type) then a bit of trigonometry should do the trick:

 \         |         /  \        |        /   \       |       /    \_____________/ ___     \     |     /   |      \    |    /    |       \   |   /     |        \  |  /      |    near         \ | /       |          \|/        |                    -+-     |-----w-----|

The width parameter passed to D3DXMatrixPerspectiveLH() is "Width of the view volume at the near view-plane"

The near value is as you pass it too.

Now remember high school maths with right angled triangles:

     ______  ___     \     |  |      \    |  |       \   |  |        \  |  |    near         \a|  |          \|  |             -+-     |-w/2-| The "a" I''ve put inside the triangle is half the angle we''re after and is half the field of view. And we know the opposite (width/2) and the adjacent side (znear)...SOHCAHTOAI''ll leave you to remember the rest Incidentally, your sphere<->cone check probably uses tan(angle/2) and cos(angle/2) somewhere so you can actually avoid using any angles at all and simply use what you''ve already got.mathworld.wolfram.com is a good place to fill in the gaps I''ve deliberately left.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
I have to confess the more I read about it, the less I do understand - looks like I really have to pick up this book mentioned.

In the meantime - well - I somehow have to get this test boing.

Let''s see.

I can easily have:
(a) the center of the cone, my camera (a Vector3).
(b) a Vector from there into the view frustrum (to it''s center).
(c) as it looks like the cone angle - as I actually do enter it into the D3DXMatrixPerspectiveFovLH call.

I also have the position of the sphere and it''s radius.

What I understand so far is - I basically have to compute the distance of the line going through the sphere center AND the cone axis, and being 90 degree to the cone axis. Surely.

Shit, I tart to really miss my math days. Totally out of control, my knowledge.

Here is what I found as a formula iun pseudocode - any help really appreciated:
quote:

bool SPhereIntersectCone (Sphere S, Cone K) {
// I assume that U is a Vector3 here. Where the hell do I get the sin from?
D = S.center - U
// No clue.
if (Dot(K.axis,D) bigger (Length(D)*K.cos) {
// Nice - where do I get the cos value from now.
// center is inside K
return true;
} else {
return false;
}
}

Whow - I knew all this stuff 0 yars ago. Hell, I had stellar mathematics in my last year''s physics class. All this Database work seems to have really done something to my brain.

Can anyone step in and help me a little more? I just ordered the book, but I fear it comes too late :-(

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

##### Share on other sites
    vec3 bc = boundCentre;    vec3 u = bc - vFrustumApex;    float t = vec3dot( u, vFrustumAxis );    if ((t+boundRadius) < NEAR_Z_CLIP)    {        return 1;    }    if ((t-boundRadius) > FAR_Z_CLIP)    {        return 2;    }    float coneRadius = t * tanHalfFrustumAngle;    // distance from sphere centre to its projection (t) on the cone axis    float c = sqrtf( u.magnitudeSquared() - (t*t) );    // distance from sphere centre to outside point on cone surface    // (at position where the the sphere centre projects onto the cone axis)    float d = c - coneRadius;    // e = shortest distance from sphere centre to outside of cone surface    float e = d * cosHalfFrustumAngle;    if (e > boundRadius)    {        // culled because the sphere is outside the cone        return 4;    }    // no cull: probably in the frustum    return 0;

Remember that a cone is just like a triangle rotated around the axis of the cone. So all that 2D trig still applies.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
Simon - you are on the verge of saving me - though not there completly. I kjust realised what a chore trigonometry was (shit, I took statistics), and that C# wants me to do most things in double (no float overloads on the Math class for a lot of functions - this will result in a NUMBER of bug reports for this).

Well, does not work yet.

Here is my code - hope my understanding of your''s was correct:
quote:

TerrainTile tile = Tiles[Tr];
Vector3 bc = tile._Patch.CenterPoint;
Vector3 u = bc - Camera.Position;
Vector3 vFrustrumAxis = Camera.ComputeFrustrumCenter () - Camera.Position;
float t = Vector3.Dot (u, vFrustrumAxis);
double coneRadius = t * Math.Tan (Camera.FieldOfFiew / 2);
double c = Math.Sqrt (u.LengthSq () - (t*t));
double d = c - coneRadius;
double e = d * Math.Cos (Camera.FieldOfFiew / 2);

Yes, thats C#, using your''s truly new DirectX version. I am not sure where I actually go wrong. I tested it, and I get NaN on e (yes, that''s "Not a Number"). In particular, I assume things go off very early - t is already more than a million. And the objewct is just around 2000 units far away. I have a 5000 unit far plane, though (thats for large scale terrain rendering).

Do you see any error?

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

##### Share on other sites
Erm, is there a NaN before that? Neither the Cos function nor basic mulitiplucation should be CAPABLE of returning NaN! ... unless they are fed nan ...

Edit: Mulitiplucation!? heheh ...

[edited by - Zaphos on December 3, 2002 3:10:48 PM]

##### Share on other sites
Checking....

Vector3 bc: valid
Vector3 u: valid
Vector3 vFrustrumAxis: valid
float t: valid
ConeRadius: valid (but a LARGE number)
c: NAN.

It blows in the line Math.Sqrt (u.LengthSq () - (t*t));

Pretty obviously, u.LengthSq () is smaller than t*t.

Now, lets see.
u: x:512, y:0 z: 462 (yes, thats a long thing)
t: 1156155.0

Yes, t is definitly too big (or?)

vFrustrumAxis, btw., is 0,0,2502.50 (which makes sense - my view frustrum is 5-5005, so the center is pretty far away.

I hate this :-)

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

##### Share on other sites
Ah yes, forgot to mention:

- Frustum axis should be *normalised*. i.e. the normalised camera view vector or the dot product project won''t work.

- Frustum apex = camera position.

- Frustum axis and sphere centres should be in the same space (e.g. camera/world etc).

This should be useful as a trig revision aid

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
Better :-) Though it is culling SOMETHING, but not exactly my view frustrum cone - bascailly I have objects suddenly disappearing when they are coming nearer (but are still around half of the frustrum in distance).

I will go through my code again :-) But it is definitly working better. I will have a good look at mathworld - actually I ordered the book mentioned.

Thanks.

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 40
• ### Forum Statistics

• Total Topics
634130
• Total Posts
3015705
×