计算旋转一下三维点? [英] Calculate rotations to look at a 3D point?

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

问题描述

我需要计算的2角(偏航和变桨)的3D对象面对的任意三维点。这些旋转被称为欧拉旋转仅仅是因为第一次旋转后,(可以说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.

这是在code我使用,但它不工作充分。当在地平面上(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,0)的对象当前朝向(0,0,1)正在转身朝向(X,Y,Z)。
  • 在为了实现这一点,就会有绕x轴,随后由一个绕y轴旋转。最后,有一个围绕z轴以有事情直立旋转。

(术语偏航,俯仰和滚转可能会造成混淆,所以我想避免使用它,但大致说来对应为x =间距,Y =偏转,Z =卷。)

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

我希望这是正确的最多的迹象。我觉得要解决这个标志的最简单的方法是通过试验和错误。事实上,你似乎已经得到了招牌上的 ROTX ROTY 正确的 - 包括与问候到z一个微妙的问题 - 所以你只需要修复的迹象 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!

下面是code,它最终为我工作。

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.

这code校正翻页效果使对象的前面的总是所面临的问题。我遇到它通过试错,所以我真的不知道发生了什么事!

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

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

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