Jump to content
  • Advertisement
Sign in to follow this  

Focus to object

This topic is 2189 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


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(&points.at(i), &points.at(i), &worldMatrix);
		D3DXVec4Transform(&points.at(i), &points.at(i), &viewMatrix);
		D3DXVec4Transform(&points.at(i), &points.at(i), &projectionMatrix);
		points.at(i).x = points.at(i).x / points.at(i).w;
		points.at(i).y = points.at(i).y / points.at(i).w;
		points.at(i).z = points.at(i).z / points.at(i).w;
		points.at(i).w = 1.0f;


	xmin = xmax = points.at(0).x;
	ymin = ymax = points.at(0).y;

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

	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);
Edited by CmasterG

Share this post

Link to post
Share on other sites
This should probably be placed in general programming(or math?), coding horrors isn't exactly about helping programming problems, it's more about the bad(and sometimes amazing) things people have/seen created.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!