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的值是正确的。

更多相关文章

  1. Android Asyntask:对上下文使用弱引用以避免设备旋转屏幕
  2. 自定义视图无法工作,直到我触摸屏幕

随机推荐

  1. android异步发网络请求
  2. Android UI 设计准则
  3. Android头部视差效果的实现方式
  4. CookieManager
  5. android 基本工程配置
  6. android按钮按下的效果
  7. Android prelink-linux-arm.map, LOCAL_P
  8. android给View设置边框 填充颜色 弧度
  9. 编译Android源码:Exception in thread "ma
  10. Intent.FLAG 属性大全