# How can I calculate the center of an object relative to a focal point and a moving observer? [closed]

I'm developing an app that contains a 3D scene which the user can navigate. As the user moves it gives the illusion that you are browsing a real landscape but for the illusion to work I need to know two things:

1. How to calculate the angular sizes of all the objects relative to their distances from the observer.
2. How to calculate the center of all the objects relative to the focal point as the user moves.

The first one is easy but the second one still baffles me and I have no idea where to look for the answer. Let's examine the second one:

As the user navigates towards the focal point, the center of the object moves across the line that is created between the focal point and it's center.

Focal Point = $FP(x_0, y_0)$

Center 1 = $C_1(x_1, y_1)$

Center 2 = $C_2(x_2, y_2)$

The equation of the line is:

$y-y_0 = m(x-x_0)$ where $m=(y_1-y_0)/(x_1-x_0)$

The center 1 $(x_1,y_1)$ is known because it is the starting center.

Let's say the the object moved a distance $d$ from center1 to center 2.

(1) $d^2=y^2+x^2$

(2) $y_2=m(x_2-x_0)+y_0$

(1) (2) = ... you can find $y_2$ and $x_2$ if you know $d$.

Can you find $d$ from the illustration? Is there any other method? • This question appears to be off-topic because it is about programming. Maybe it would be better served in Stack Overflow. – ja72 Aug 25 '14 at 16:50
• I'm not sure about programming per se. This is more about rendering, and the math behind (one particular choice of) artistic technique. gamedev.stackexchange.com seems more appropriate. – user10851 Aug 25 '14 at 21:30

This of course has been solved many years ago in computer graphics. Look up something called perpsective projection.

The simplest and most common way to do this is a point to plane projection. All parts of the scene are projected to a single point, usually called the view point or the eye point. You conceptually suspend the image plane at a fixed location relative to the eye point, and make its color whatever is seen from the eye point thru that point on the plane.

Here is a diagram that shows this. The math should be obvious.

Apparently there is still some confusion about the math to project a 3D object to a 2D plane despite the diagram above.

I usually do this by transforming the scene into a space where the virtual image rectangle is on the XY plane centered at (0, 0, 0) and sized ±1 in X and Y. The eyepoint is on the +Z axis at distance Eyedis from the image plane. In other words, the eyepoint is at (0, 0, Eyedis). Using this system, the Eyedis parameter becomes a dimensionless value that only adjusts the aggressiveness of the perspective. For example, infinite Eyedis is the same as a flat or orthagonal projection. A rough rule of thumb is that Eyedis/15 is about the lens focal length in mm on a 35mm camera to get the same degree of perspective.

Anyway, the math to project a point is trivial. The X and Y coordinate of a point map directly to the image plane except that these are scaled inversely by the distance to the eyepoint. Since we define the plane Z=0 to be where objects are their native size, the scaling factor is simply Eyedis/(Eyedis-Z). To spell this out explicitly, the 2D image plane coordinate that a 3D point projects to is:

(X * Eyedis / (Eyedis - Z), Y * Eyedis / (Eyedis - Z))

Yes, it really is that easy.

Note that this blows up for coordinates at Z=Eyedis, as would be expected.

Astonishingly, the solution to the problem is similar to that of calculating the angular sizes. After focusing on the problem I realized that:

Center = Real Center / Depth.

The real center is the center of the object at distance = 1.

The same way:

Angular Size = Real Size / Depth

The real size is the size of the object at distance = 1.

Center2 = (RealCenter.x / distance, RealCenter.y / distance)

Center2 = (Center1.x / distance2, Center1.y / distance2)

Finally,

Center2 = (x1 / 5, y1 / 5)

• I've tested it and it works. Brilliant! – user3737190 Aug 26 '14 at 11:48