# Panning camera so object is at edge of screen

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

## Recommended Posts

I'm trying to write a formula to pan the camera enough so that an object is just visible at the edge of the screen.
So, in other words if the object is out of view to the right, I would change the x and y position of the camera, so that the object is just at the right edge of the screen (without changing camera angle or z co-ordinate).

Any tips how to do this would be greatly appreciated.

cheers,

Ed

##### Share on other sites

In the example above the right plane of the viewing frustrum should be a supporting plane of the object.

that is the object and the plane must intersect and the object must lie on one side of the plane.

##### Share on other sites

Yeah, but what about the maths?
So far, I have worked out the following:
camPos + camDir*t + tan(fov)*t = p
where t = distance to object and p = postion of object
now t = sqrt((camPos.x-p.x)^2 + (camPos.y-p.y)^2 + (camPos.z-p.z)^2)
but I have no idea how I can solve that because I only have one equation and two variables

##### Share on other sites

Think in terms of frustrum planes, not view rays.

You could try something like this, again using the right frustrum plane for example:

1. get the plane equation

2. cast ray R along x-axis to find object-plane distance

3. move camera along x-axis so that distance is 0

depending on the object or which bounding geometry you use you should

sample the distance at every vertex and use the minimum or something like that.

im not really sure what you are trying to do. after the camera translation the object

should still be outside the camera view, right?

##### Share on other sites

Think in terms of frustrum planes, not view rays.

You could try something like this, again using the right frustrum plane for example:

1. get the plane equation

2. cast ray R along x-axis to find object-plane distance

3. move camera along x-axis so that distance is 0

depending on the object or which bounding geometry you use you should

sample the distance at every vertex and use the minimum or something like that.

im not really sure what you are trying to do. after the camera translation the object

should still be outside the camera view, right?

I'm trying to pan the camera the minimum amount so the object moves just into the view frustrum. The object position p is in the center of the object, so moving it into the edge of the frustrum would make it half visible (which is good enough)

##### Share on other sites

I sort of got it working. This is the code that I used:

	m_plane[RIGHT].Normalize();

Vector3D up(0, 0, 1.0f);
Vector3D xAxis = up.crossProduct(m_dir);
xAxis.normalize();

float distToPlane = m_plane[RIGHT].GetDistance(pos);
xDist = distToPlane / m_plane[RIGHT].DotProduct(xAxis);



It almost positions the object correctly, but it's slightly off. I can't figure out where the error is.

Anyways, thanks for the answer ;)

Edited by Ed Welch

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

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×