Jump to content
  • Advertisement
Sign in to follow this  
DrGUI

Radius enclosing near plane

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!