Calculate rotations to look at a 3D point?

余生颓废 提交于 2019-11-28 03:52:15

Here are my working assumptions:

  • The coordinate system (x,y,z) is such that positive x is to the right, positive y is down, and z is the remaining direction. In particular, y=0 is the ground plane.
  • An object at (0,0,0) currently facing towards (0,0,1) is being turned to face towards (x,y,z).
  • In order to accomplish this, there will be a rotation about the x-axis followed by one around the y-axis. Finally, there is a rotation about the z-axis in order to have things upright.

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

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.

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

Rich Seller's answer shows you how to rotate a point from one 3-D coordinate system to another system, given a set of Euler angles describing the rotation between the two coordinate systems.

But it sounds like you're asking for something different:

You have: 3-D coordinates of a single point

You want: a set of Euler angles

If that's what you're asking for, you don't have enough information. To find the Euler angles, you'd need coordinates of at least two points, in both coordinate systems, to determine the rotation from one coordinate system into the other.

You should also be aware that Euler angles can be ambiguous: Rich's answer assumes the rotations are applied to Z, then X', then Z', but that's not standardized. If you have to interoperate with some other code using Euler angles, you need to make sure you're using the same convention.

You might want to consider using rotation matrices or quaternions instead of Euler angles.

This series of rotations will give you what you're asking for:

  1. About X: 0
  2. About Y: atan2(z, x)
  3. About Z: atan2(y, sqrt(x*x + z*z))

I cannot tell you what these are in terms of "roll", "pitch" and "yaw" unless you first define how you are using these terms. You are not using them in the standard way.

EDIT:
All right, then try this:

  1. About X: -atan2(y, z)
  2. About Y: atan2(x, sqrt(y*y + z*z))
  3. About Z: 0

Talking about the rotation of axes, I think step 3 should have been the rotation of X'-, Y''-, and Z'-axes about the Y''-axis.

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