Started by Oct 21 2012 06:30 AM

,
2 replies to this topic

Posted 21 October 2012 - 06:30 AM

I'm trying to place my game camera so that the resulting view for the player will encompass a selection of points in world space. I have calculated the central averaged position of the points, worked out the unit vector in which I want the camera to travel away from the central point, but I can't seem to quite get my head around the required distance to move the camera.

I can't figure out how to take the FOV of the camera, the list of points, and the facing angle/projection matrix of the camera to determine how far back I need to pull the camera. I'm currently thinking that I might be able to reverse the 'is point on screen' equation using the cameras projection matrix, then get a required distance for each point and use the biggest distance. I'm just not sure how to reverse that equation - or if I'm even on the right track here.

Can someone point me in the right direction please?

I can't figure out how to take the FOV of the camera, the list of points, and the facing angle/projection matrix of the camera to determine how far back I need to pull the camera. I'm currently thinking that I might be able to reverse the 'is point on screen' equation using the cameras projection matrix, then get a required distance for each point and use the biggest distance. I'm just not sure how to reverse that equation - or if I'm even on the right track here.

Can someone point me in the right direction please?

Posted 22 October 2012 - 08:45 AM

I would do it the following way (using OpenGL matrix):

Place the camera at the median point of your point cloud. Transform all points to camera (view, eye) space.

Now translating camera along it's local Z is equivalent to translating all points along global Z in camera space. I.e. you have to find for each point, which is the smallest translation along z that moves it inside view frustum.

Let your point (in camera coordinate system) be P(x,y,z)

In clip coordinate system:

Qx = 2Px/(R-L) - Pz(R+L)/(R-L)

Qy = 2Py/(T-B) - Pz(T+B)/(T-B)

Qz = -(F+N)/(F-N) - 2FN/(F-N)

Qw = -Pz

And after perspective division

Sx = Qx/Qw

Sy = Qy/Qw

Sz = Qz/Qw

You want to ensure that:

-1 <= Sx <= 1

-1 <= Sy <= 1

-1 <= Sz (you do not want to check far plane here, because otherwise your inequalities may become unsolvable)

Now for each point and for each coordinate x, y and z find maximum z value Pz' that fills the conditions of these inequalities.

Required distance to move camera d = Pz - Pz'

While iterating over all points and all coordinates find the maximum d.

Place the camera at the median point of your point cloud. Transform all points to camera (view, eye) space.

Now translating camera along it's local Z is equivalent to translating all points along global Z in camera space. I.e. you have to find for each point, which is the smallest translation along z that moves it inside view frustum.

Let your point (in camera coordinate system) be P(x,y,z)

In clip coordinate system:

Qx = 2Px/(R-L) - Pz(R+L)/(R-L)

Qy = 2Py/(T-B) - Pz(T+B)/(T-B)

Qz = -(F+N)/(F-N) - 2FN/(F-N)

Qw = -Pz

And after perspective division

Sx = Qx/Qw

Sy = Qy/Qw

Sz = Qz/Qw

You want to ensure that:

-1 <= Sx <= 1

-1 <= Sy <= 1

-1 <= Sz (you do not want to check far plane here, because otherwise your inequalities may become unsolvable)

Now for each point and for each coordinate x, y and z find maximum z value Pz' that fills the conditions of these inequalities.

Required distance to move camera d = Pz - Pz'

While iterating over all points and all coordinates find the maximum d.

Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya

Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya

Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

Posted 22 October 2012 - 11:22 AM

Excellent, thanks Lauris. I appreciate the detailed explanation.

------------------------------------------[New Delta Games] | [Sliders]