# Radius enclosing near plane

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

## Recommended Posts

Hi! I'm trying to make a sphere which has its centre at the camera position and find a radius that encloses the near plane of my view frustum. Using CalculateMaxDistanceToNearPlane, I get an answer that is a little too low, however I get a perfect answer when I find the corners and compare squared distances. But of course that is much slower. Can some clever person see why? I'm sure it's more likely that my new functions are wrong rather than the functions they use :)
public static float CalculateMaxDistanceToNearPlane(Vector3 position, float left, float right,
float top, float bottom, float zn)
{
float w = System.Math.Max(System.Math.Abs(left), System.Math.Abs(right));
float h = System.Math.Max(System.Math.Abs(top), System.Math.Abs(bottom));
return (float)System.Math.Sqrt(0.25 *(h * h + w * w) + zn * zn);       // d = sqrt((0.5h)^2 + (0.5w)^2 + z^2)
}

public static float CalculateMaxDistanceToNearPlane(Vector3 position, Matrix projection)
{
float left, right, top, bottom, zn, zf;
ThreeD.Projection.ExtractParameters(projection, out left, out right, out top, out bottom, out zn, out zf);
return CalculateMaxDistanceToNearPlane(position, left, right, top, bottom, zn);
}

public virtual Sphere BoundingSphere {
get {
//Calculate the camera radius; the sphere should enclose the near plane of the view frustum
Matrix mat = Projection;
mat.Invert();

float maxDist = CalculateMaxDistanceToNearPlane(Position, mat);

Vector3[] pts = new Vector3[8];
ThreeD.Projection.ExtractPoints(mat, ref pts);

float d = System.Math.Max(pts[0].LengthSq(),
System.Math.Max(pts[1].LengthSq(),
System.Math.Max(pts[2].LengthSq(),
pts[3].LengthSq())));
d = (float)System.Math.Sqrt(d); //only do 1 sqrt
return new Sphere(Position, d);
}
}


When I go on preview here on the forums, it looks like GameDev is removing all my carriage-returns so sorry if it's not very readable. Cheers! EDIT: tried to narrow post EDIT 2: or if you don't want to read .NET code then maybe you could just tell me? *smiles sweetly* [Edited by - DrGUI on July 8, 2005 11:43:33 AM]

##### Share on other sites
Funny...in the last post column it says John Bo(u)lton but nothing here!

##### Share on other sites
The distance to the farthest corner of the near plane is
    x = max( abs( left ), abs( right ) )     y = max( abs( top ), abs( bottom ) )    d = sqrt(x2 + y2 + znear2)

##### Share on other sites
Ah thanks! I almost had that already but I still have the divide-by-twos left in from height and width...yeeeess!

Thanks for pointing out my silly mistake! Your rating++

1. 1
2. 2
Rutin
20
3. 3
A4L
15
4. 4
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633739
• Total Posts
3013619
×