Sign in to follow this  

Focus to object

Recommended Posts


I have the following problem. I want to zoom to my object. I have the camera in sphere coordinates. So the camera position has 2 angles and a length. For zooming I want to change the length of the camera, so that the object has the maximal expansion on the screen. The object is a bridge. So it has 8 corner points, which I transformed into screen space. This is what I tried to get the length, but it is not working correct. Can anyone help me? Can anyone see the problem:

float xmin, xmax, ymin, ymax, zmin, zmax;
	xmin = - m_width / 2;
	xmax = m_width / 2;
	ymin = -(m_height * scale_factor - m_height/2);
	ymax = m_height /2;
	zmin = -m_length / 2;
	zmax = m_length / 2;

	std::vector<D3DXVECTOR4> points;
	D3DXVECTOR4 point;

	point = D3DXVECTOR4(xmin, ymin, zmin,1.0f);

	point = D3DXVECTOR4(xmin, ymin, zmax, 1.0f);

	point = D3DXVECTOR4(xmin, ymax, zmax,1.0f);

	point = D3DXVECTOR4(xmin, ymax, zmin,1.0f);

	point = D3DXVECTOR4(xmax, ymin, zmin, 1.0f);

	point = D3DXVECTOR4(xmax, ymin, zmax, 1.0f);

	point = D3DXVECTOR4(xmax, ymax, zmax, 1.0f);

	point = D3DXVECTOR4(xmax, ymax, zmin,1.0f);

	D3DXMATRIX worldMatrix, projectionMatrix, viewMatrix;

	for(int i = 0; i < points.size(); i++)
		D3DXVec4Transform(&, &, &worldMatrix);
		D3DXVec4Transform(&, &, &viewMatrix);
		D3DXVec4Transform(&, &, &projectionMatrix); = /; = /; = /; = 1.0f;


	xmin = xmax =;
	ymin = ymax =;

	for(int i = 1; i < points.size(); i++)
		if( < xmin)
			xmin =;
		if( > xmax)
			xmax =;
		if( < ymin)
			ymin =;
		if( > ymax)
			ymax =;

	float width, height, scale, length;

	if(abs(xmax) > abs(xmin))
		width = abs(xmax);
		width = abs(xmin);

	if(abs(ymax) > abs(ymin))
		height = abs(ymax);
		height = abs(ymin);

	if(width > height)
		scale = 1/width;
	scale = 1 / height;


	length = m_Camera->getLength();
	m_Camera->setLength(length / scale);

Share this post

Link to post
Share on other sites


What I want is that the model (bridge) will fit to screen. So when I am too close with my camera I want that it zoom out so that the model fits to screen, and when my camera is too far away, I want that it zooms in so that the model fits to screen. I want sort of a focus function like when you press in Autodesk Maya the "F" Key, but with the difference that it should zoom in or out in the direction of the looking vector. Do you know what I mean? 

For the position of the camera I use sphere coordinates instead of cartesian coordinates. So  I have two angles and a length. For zooming I only have to change my length to the object.

What I did with the code was the following: I take my corner points of the model (bounding box) and transformed them from world coordinates in screen coordinates by multiplying the coordinates of the corner points with my world matrix, view matrix and projection matrix. Then I took the ratio between the size of the whole screen (screen_width, screen_height) and the size of the rectangle which is formed by the bridge on the screen ( height = max_y - min_y of the bridge in screen coordinates, width = max_x - min_x) and then I multiplied my camera length by this ratio. The result is that it is jumping back and forth when pressing the key for the fit to screen function.

BTW: the bridge is always in the center of my "world".

Do you have an idea why it is not working correct?

Share this post

Link to post
Share on other sites

Ok. I think the only thing you want to do is keep the camera's distance so that it EXACTLY keeps the bridge in view. There should be a mathematical way of doing this.... Let me think on it.....

Share this post

Link to post
Share on other sites

This assumes you are using FOV of pi/4...... This gives a 900 look perspective, so 450 from center of the screen.


You first need to determine the length measured from center to the farthest point (in camera space) on the bridge. This is tricky. Make a temporary structure of the 8 points you created earlier (remember, you need to keep the points up to date if you move the bridge). "Unrotate" all the temp points using the negative of the rotation values of the camera. This will make these points "relative" to the original world axis. Compare the X values and find the one farthest from 0. Make that value your distance.

(if using pi/4, don't ready any further)







If you are not using pi/4, then you will need to do something else.



Where A is the hypotenuse of the triangle and B&C are its legs..... This is a good place to start.....



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