点看点 [英] Point look at Point

查看:16
本文介绍了点看点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 3D 空间中有一个点,并且在 3D 空间中有相机的位置和旋转.

So I have one point in 3D space, and I have location and rotation of the Camera in 3D space.

所以基本上对象上有Vector3.相机 Vector3Quaternion.

So basically there is Vector3 on the object. Camera Vector3 and Quaternion.

我需要知道如何看待这一点.

I need to get how to look at that point.

我想告诉用户如何移动到那个点.用户应该向左、向右还是向后直接拍摄相机?

I want to tell user how to move to that point. Should user direct camera left or right or behind?

推荐答案

一种方法是将相机当前面向的方向计算为偏航角(如罗盘航向),并计算它需要的方向脸才能看重点.

One way to do this is to calculate the direction the camera is currently facing as a yaw angle (like a compass heading), and calculate the direction it needs to face in order to be looking at the point.

从另一个中减去一个并调整结果,使其在 -180 到 180 度(或 -pi 到 pi 弧度)的范围内,然后根据符号告诉用户向左或向右转.如果绝对值大于 120 度(或某个可配置的值),则告诉他们它在他们后面.

Subtract one from the other and adjust the result so that it is in the range of -180 to 180 degrees (or -pi to pi radians) and then tell the user to turn left or right based on the sign. If the absolute value is more than 120 degrees (or some configurable value) then tell them it is behind them.

要找到相机的当前航向,将向量 (0, 0, 1) 通过四元数转换为前向向量,然后使用 atan2(forward.z, forward.x).

To find the camera's current heading, transform the vector (0, 0, 1) by the quaternion to get the forward vector, then calculate the heading using atan2(forward.z, forward.x).

要计算观察该点所需的航向,请从该点减去当前相机位置以获得所需的前向矢量,然后传递给 atan:

To calculate the heading required to look at the point, subtract the current camera position from the point to get a desired forward vector and then pass to atan:

Vector3 desired_forward = point - camera_pos;
float desired_heading = atan2(desired_forward.z, desired_forward.x);

然后找到需要的旋转:

float rotation_needed = desired_heading - heading;
if(rotation_needed > Math.PI)
    rotation_needed -= 2 * Math.PI;
if(rotation_needed < -Math.PI)
    rotation_needed += 2 * Math.PI;

现在告诉用户根据所需旋转的符号向左或向右旋转.

Now tell the user to rotate left or right based on the sign of the rotation needed.

如果要向上/向下查找,可以先计算XZ平面中前向矢量的长度,然后再次使用atan2来计算俯仰角:

If you want to do it for look up/down, you can calculate a pitch angle by first calculating the length of the forward vector in the XZ plane and then using atan2 again:

float xzLength = sqrt(forward.x * forward.x + forward.z * forward.z);
float pitch_angle = atan2(forward.y, xzLength);

对所需的正向向量执行相同的操作,并从所需的向量中减去电流.检查标志告诉用户是向上看还是向下看.

Do the same for the desired forward vector and subtract the current from the desired. Check the sign to tell the user whether to look up or down.

有一些可能的并发症.例如,根据相机四元数是指定从世界空间到相机空间的变换还是反之,您可能需要否定计算出的相机航向.

There's a few likely complications. For example, depending on whether the camera quaternion specifies the transform from world space to camera space or vice versa, you might need to negate the calculated camera heading.

这篇关于点看点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆