计算旋转以查看 3D 点? [英] Calculate rotations to look at a 3D point?

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

问题描述

我需要计算 3D 对象面对任意 3D 点的 2 个角度(偏航角和俯仰角).这些旋转被称为欧拉"旋转,因为在第一次旋转之后(假设 Z,基于下图)Y 轴也随着对象旋转.

I need to calculate the 2 angles (yaw and pitch) for a 3D object to face an arbitrary 3D point. These rotations are known as "Euler" rotations simply because after the first rotation, (lets say Z, based on the picture below) the Y axis also rotates with the object.

这是我正在使用的代码,但它不能完全工作.当在地平面 (Y = 0) 上时,对象正确旋转以面向该点,但是一旦我在 Y 中向上移动该点,旋转看起来就不正确.

This is the code I'm using but its not working fully. When on the ground plane (Y = 0) the object correctly rotates to face the point, but as soon as I move the point upwards in Y, the rotations don't look correct.

// x, y, z represent a fractional value between -[1] and [1]
// a "unit vector" of the point I need to rotate towards

yaw = Math.atan2( y, x )
pitch = Math.atan2( z, Math.sqrt( x * x + y * y ) )

你知道如何计算给定一个点的 2 个欧拉角吗?

Do you know how to calculate the 2 Euler angles given a point?

下图显示了我旋转的方式.这些是我需要计算的角度.(唯一的区别是我按照 X、Y、Z 而非 Z、Y、X 的顺序旋转对象)

The picture below shows the way I rotate. These are the angles I need to calculate. (The only difference is I'm rotating the object in the order X,Y,Z and not Z,Y,X)

这是我的系统.

  • 坐标系 x = 向右,y = 向下,z = 更靠后
  • 默认情况下,对象位于 (0,0,1) 处,面向后
  • 旋转的顺序是 X、Y、Z,其中 X 上的旋转是俯仰,Y 是偏航,Z 是滚转

推荐答案

以下是我的工作假设:

  • 坐标系 (x,y,z) 是这样的,正 x 向右,正 y 向下,z 为剩余方向.特别是,y=0 是地平面.
  • 当前面向 (0,0,1) 的 (0,0,0) 对象正在转向面向 (x,y,z).
  • 为了实现这一点,将围绕 x 轴旋转,然后围绕 y 轴旋转.最后,绕 z 轴旋转以使物体竖直.

(yaw、pitch 和 roll 的术语可能会引起混淆,所以我想避免使用它,但粗略地说对应关系是 x=pitch, y=yaw, z=roll.)

(The terminology yaw, pitch, and roll can be confusing, so I'd like to avoid using it, but roughly speaking the correspondence is x=pitch, y=yaw, z=roll.)

鉴于此设置,我尝试解决您的问题:

Here is my attempt to solve your problem given this setup:

rotx = Math.atan2( y, z )
roty = Math.atan2( x * Math.cos(rotx), z )
rotz = Math.atan2( Math.cos(rotx), Math.sin(rotx) * Math.sin(roty) )

希望这是正确的迹象.我认为修复这些标志的最简单方法是反复试验.事实上,您似乎已经在 rotxroty 上获得了正确的标志——包括一个关于 z 的微妙问题——所以你只需要修复 rotxroty 上的标志代码>rotz.

Hopefully this is correct up to signs. I think the easiest way to fix the signs is by trial and error. Indeed, you appear to have gotten the signs on rotx and roty correct -- including a subtle issue with regards to z -- so you only need to fix the sign on rotz.

我希望这不是微不足道的(可能取决于您所在的八分圆),但请在说它错误之前尝试几种可能性.祝你好运!

I expect this to be nontrivial (possibly depending on which octant you're in), but please try a few possibilities before saying it's wrong. Good luck!

这是最终对我有用的代码.

Here is the code that finally worked for me.

我注意到当对象从任何前象限(正 Z)移动到任何后象限时会发生翻转"效果.在前象限中,对象的正面将始终面向该点.在后象限中,物体的总是面向点.

I noticed a "flip" effect that occurred when the object moved from any front quadrant (positive Z) to any back quadrant. In the front quadrants the front of the object would always face the point. In the back quadrants the back of the object always faces the point.

此代码纠正了翻转效果,因此对象的正面始终面向该点.我通过反复试验遇到了它,所以我真的不知道发生了什么!

This code corrects the flip effect so the front of the object always faces the point. I encountered it through trial-and-error so I don't really know what's happening!

 rotx = Math.atan2( y, z );
 if (z >= 0) {
    roty = -Math.atan2( x * Math.cos(rotx), z );
 }else{
    roty = Math.atan2( x * Math.cos(rotx), -z );
 }

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

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