Point look at Point

烂漫一生 提交于 2020-01-05 06:59:07

问题


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

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?


回答1:


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.

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.

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).

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);

Then find the rotation needed:

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.

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.



来源:https://stackoverflow.com/questions/42592198/point-look-at-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!