3D camera Zoom in and follow physics in java?
I am trying to make sure that no matter how far apart the two objects are, they both stay on the screen. I am using JOGL but it doesn't matter as I just need some help with the math. This is what I have so far:
float distance = (float) ((Math.sqrt((p1.x - p2.x) + (p1.y - p2.y))));
float camx = (float)((p1.x + p2.x) * 0.5);
float camy = (float)((p1.y + p2.y) * 0.5);
float camz = (float) (distance * 5);
What math do I need to get Z to scale properly?
a source to share
If both objects have z = 0 and the viewing angle of the screen (from the center of the screen to the edge) is ax and ay for the horizontal and vertical angles, then:
zx = abs((p1.x-p2.x)*0.5)/tan(ax)
zy = abs((p1.y-p2.y)*0.5)/tan(ay)
and
camz = max(zx, zy)
Here zx and zy are the distances to get objects in the horizontal and vertical screen dimensions, and camz is the distance that meets both criteria. Also note that ax and ay are in radians (for example, if you assume your screen is 40 degrees wide, that is, 20 degrees, or ax = 20 * (pi / 180) = 0.3419 radians).
Your values โโfor camx and camy were correct.
a source to share