Sign in to follow this  

Radius enclosing near plane

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;

		float maxDist = CalculateMaxDistanceToNearPlane(Position, mat);

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

		float d = System.Math.Max(pts[0].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 this post

Link to post
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++

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this