3D相机缩放并遵循java中的物理特性?
I'm trying to make it so that no matter how far apart two objects are they'll both remain on screen. I'm using JOGL, but that shouldn't matter as I just need help with the math. This is what I have so far:
我试图做到这一点,无论两个物体相隔多远,它们都会留在屏幕上。我正在使用JOGL,但这并不重要,因为我只需要数学方面的帮助。这是我到目前为止:
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 is the math I need so the Z zooms in and out correctly?
我需要什么数学运算才能使Z正确放大和缩小?
4 个解决方案
#1
If both objects have z=0, and your screen viewing angle (from the center of the screen to an edge) is ax and ay for the horizontal and vertical angles, then:
如果两个对象的z = 0,并且您的屏幕视角(从屏幕中心到边缘)对于水平和垂直角度是ax和ay,那么:
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 the objects onto the horizontal and vertical dimensions of the screen, and camz is the distance that satisfies both criteria. Also note that ax and ay are in radians (for example, if you assume your screen is 40 deg wide, then ax is 20 deg, or ax =20*(pi/180)=0.3419 radians).
这里zx和zy是将对象放到屏幕的水平和垂直维度上的距离,而camz是满足两个标准的距离。还要注意,ax和ay是弧度的(例如,如果你假设你的屏幕是40度宽,那么ax是20度,或者ax = 20 *(pi / 180)= 0.3419弧度)。
Your values for camx and camy were correct.
你对camx和camy的值是正确的。