绕原点 3d 旋转

发布于 2024-12-22 23:04:19 字数 1085 浏览 0 评论 0原文

我知道有很多关于 3d 旋转的问题已经在这里得到解答,但所有这些问题似乎都处理 OpenGL 中的旋转矩阵和四元数(而且我并不关心我是否得到万向节锁定)。我需要获取一个点的 3d 坐标 EX:(x,y,z),该点距原点的距离必须始终相同,我现在将其称为“d”。我作为输入的唯一信息是鼠标在屏幕上的增量和增量。到目前为止,这是我尝试过的:

第一:

thetaxz+=(omousex-mouseX)/( width ); 
thetaxy+=(omousey-mouseY)/( height);

(thetaxy 是 x,y 轴上的弧度角度,thetaxz 是 x,z 轴上的角度) (我限制两个角度,这样如果它们小于或等于 0,则它们等于 2*PI)

第二:

pointX=cos(thetaxz)*d;
pointY=sin(thetaxy)*d;

(pointX 是点的 x 坐标,pointY 是 y)

第三:

if(thetaxz)<PI){
 pointZ=sqrt(sq(d)-sq(eyeX/d)-sq(eyeY/d));
}else{
  pointZ=-sqrt(abs(sq(d)-sq(eyeX/d)-sq(eyeY/d)));
}

(sq() 是平方和 abs 的函数()为绝对值函数) (pointZ 应该是该点的 z 坐标,除了正 z 半球和负 z 半球之间的交叉点之外。当它接近边缘时,该点会被拉伸得比它在 x 和 y 中始终应该处于的距离更远并且看似随机地在 thetaxz 的 0.1-0.2 弧度附近,z 坐标变为 NAN 或未定义)

我已经考虑了一段时间,说实话,我很难理解四元数的概念和旋转矩阵,但是如果您能向我展示如何使用它们来生成实际坐标,我将很高兴学习。如果我可以在几个轴上使用一些三角函数,我仍然会更喜欢它。预先感谢您提供的任何帮助,如果您需要更多信息,请询问。

提示/最后一刻的想法:我认为这可能与 z 位置影响 x 和 y 位置有关,但我不确定。

编辑:我画了一个图表: 在此处输入图像描述

I know there are plenty of questions about 3d rotation that have been answered here but all of them seem to deal with rotational matrices and quaternions in OpenGL (and I don't really care if I get gimbal lock). I need to get 3d coordinates EX:(x,y,z) of a point that always must be the same distance, I'll call it "d" for now, from the origin. The only information I have as input is the deltax and deltay of the mouse across the screen. So far here is what I have tried:

First:

thetaxz+=(omousex-mouseX)/( width ); 
thetaxy+=(omousey-mouseY)/( height);

(thetaxy is the angle in radians on the x,y axis and thetaxz on the x,z axis)
(I limit both angles so that if they are less than or equal to 0 they equal 2*PI)

Second:

pointX=cos(thetaxz)*d;
pointY=sin(thetaxy)*d;

(pointX is the point's x coordinate and pointY is the y)

Third:

if(thetaxz)<PI){
 pointZ=sqrt(sq(d)-sq(eyeX/d)-sq(eyeY/d));
}else{
  pointZ=-sqrt(abs(sq(d)-sq(eyeX/d)-sq(eyeY/d)));
}

(sq() is a function that squares and abs() is an absolute value function)
(pointZ should be the point's z coordinate and it is except at crossing between the positive z hemisphere and negative z hemisphere. As it approaches the edge the point gets stretched further than the distance that it is always supposed to be at in the x and y and seemingly randomly around 0.1-0.2 radians of thetaxz the z coordinate becomes NAN or undefined)

I have thought about this for awhile, and truthfully I'm having difficulty warping my head around the concept of quaternions and rotational matrices however if you can show me how to use them to generate actual coordinates I would be glad to learn. I would still prefer it if I could just use some trigonometry in a few axis. Thank you in advance for any help and if you need more information please just ask.

Hint/last minute idea: I think it may have something to do with the z position affecting the x and y positions back but I am not sure.

EDIT: I drew a diagram:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2024-12-29 23:04:19

如果你真的想在这方面取得成功,你就必须硬着头皮学习旋转矩阵 和/或四元数旋转。可能还有其他方法可以完成您想要的操作,但使用旋转矩阵和四元数旋转只是因为它们被广泛理解,并且是表达和应用向量旋转的最简单方法之一。人们可以提出的任何其他表示可能都是对其中一个或两个进行更复杂的重新表述。事实上,可以证明旋转是一个线性变换,因此可以表示为矩阵。四元数旋转只是 3D 中旋转向量的简化方法,因此具有等效的矩阵表示形式。

也就是说,听起来您有兴趣通过单击鼠标来抓取场景中的一个对象并以自然的方式旋转。如果是这种情况,您应该查看 ArcBall 方法(您可能需要查看大量示例 超过)。这仍然需要您了解四元数。您还会发现,至少对线性代数的基本方面有一定的了解会很有帮助。

更新:根据您的图表及其包含的注释,看起来您真正想做的就是转换球面坐标笛卡尔坐标。只要我们在符号上达成一致,那就很容易了。令 θ 为您所说的 XY 角度,即绕 Z 轴旋转的 X 轴之间的角度;这称为方位角,范围为 [0, 2π) 弧度或 [0°, 360°)。令 Φ 为 XY 平面与向量之间的角度;这称为仰角,范围为 [-π/2, +π/2] 或 [-90°, +90°],它对应于您称为 XZ 角的角度(在绕 Y 轴的 XZ 平面)。还有其他约定,因此请确保保持一致。无论如何,转换很简单:

x = d∙cos(Φ)∙cos(θ)
y = d∙cos(Φ)∙sin(θ)
z = d∙sin(Φ)

If you truly want any success in this, you're going to have to bite the bullet and learn about rotation matrices and / or quaternion rotations. There may be other ways to do what you want, but rotation matrices and quaternion rotations are used simply because they are widely understood and among the simplest means of expressing and applying rotations to vectors. Any other representation somebody can come up with will probably be a more complex reformulation of one or both of these. In fact it can be shown rotation is a linear transformation and so can be expressed as a matrix. Quaternion rotations are just a simplified means of rotating vectors in 3D, and therefore have equivalent matrix representations.

That said, it sounds like you're interested in grabbing an object in your scene with a mouse click and rotating in a natural sort of way. If that's the case, you should look at the ArcBall method (there are numerous examples you may want to look over). This still requires you know something of quaternions. You will also find that an at least minimal comprehension of the basic aspects of linear algebra will be helpful.

Update: Based on your diagram and the comments it contains, it looks like all you are really trying to do is to convert Spherical Coordinates to Cartesian Coordinates. As long as we agree on the the notation, that's easy. Let θ be the angle you're calling XY, that is, the angle between the X axis rotated about the Z axis; this is called the azimuth angle and will be in the range [0, 2π) radians or [0°, 360°). Let Φ be an angle between the XY plane and your vector; this is called the elevation angle and will be in the range [-π/2, +π/2] or [-90°, +90°] and it corresponds to the angle you're calling the XZ angle (rotation in the XZ plane about the Y axis). There are other conventions, so make sure you're consistent. Anyway, the conversion is simply:

x = d∙cos(Φ)∙cos(θ)
y = d∙cos(Φ)∙sin(θ)
z = d∙sin(Φ)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文