使用 Java 3D 旋转矢量

发布于 2024-11-17 09:48:08 字数 1279 浏览 4 评论 0原文

我正在尝试使用 Java3D 来旋转向量。我的目标是创建一个变换,使向量与 y 轴平行。为此,我计算了原始向量和相同向量之间的角度,但它的 z 值为 0(原始 x、原始 y、z 值为 0)。然后我对 y 轴做了同样的事情(原始 x,0 代表 y 值,原始 z)。然后,我使用每个角度创建两个 Transform3D 对象,将它们相乘并应用于向量。我的代码如下:

Transform3D yRotation = new Transform3D();
Transform3D zRotation = new Transform3D();

//create new normal vector
Vector3f normPoint = new Vector3f (normal.getX(), normal.getY(), normal.getZ());

//****Z rotation methods*****
Vector3f newNormPointZ = new Vector3f(normal.getX(), normal.getY(),0.0F);
float zAngle = normPoint.angle(newNormPointZ);
zRotation.rotZ(zAngle);

//****Y rotation methods*****
Vector3f newNormPointY = new Vector3f(normal.getX(),0.0F, normal.getZ());
float yAngle = normPoint.angle(newNormPointY);
yRotation.rotY(yAngle);

//combine the two rotations
yRotation.mul(zRotation);

System.out.println("before trans normal = " +normPoint.x + ", "+normPoint.y+", "+normPoint.z);
//PRINT STATEMENT RETURNS: before trans normal = 0.069842085, 0.99316376, 0.09353002

//perform transform 
yRotation.transform(normPoint);

System.out.println("normal trans = " +normPoint.x + ", "+normPoint.y+", "+normPoint.z);
//PRINT STATEMENT RETURNS: normal trans = 0.09016449, 0.99534255, 0.03411238    

我希望变换会产生 x 和 z 值或非常接近 0。虽然逻辑对我来说很有意义,但我显然错过了一些东西..

I'm attempting to use Java3D to rotate a vector. My goal is create a transform that will make the vector parallel with the y-axis. To do this, I calculated the angle between the original vector and an identical vector except that it has a z value of 0 (original x, original y, 0 for z-value). I then did the same thing for the y-axis (original x, 0 for y-value, original z). I then used each angle to create two Transform3D objects, multiply them together and apply to the vector. My code is as follows:

Transform3D yRotation = new Transform3D();
Transform3D zRotation = new Transform3D();

//create new normal vector
Vector3f normPoint = new Vector3f (normal.getX(), normal.getY(), normal.getZ());

//****Z rotation methods*****
Vector3f newNormPointZ = new Vector3f(normal.getX(), normal.getY(),0.0F);
float zAngle = normPoint.angle(newNormPointZ);
zRotation.rotZ(zAngle);

//****Y rotation methods*****
Vector3f newNormPointY = new Vector3f(normal.getX(),0.0F, normal.getZ());
float yAngle = normPoint.angle(newNormPointY);
yRotation.rotY(yAngle);

//combine the two rotations
yRotation.mul(zRotation);

System.out.println("before trans normal = " +normPoint.x + ", "+normPoint.y+", "+normPoint.z);
//PRINT STATEMENT RETURNS: before trans normal = 0.069842085, 0.99316376, 0.09353002

//perform transform 
yRotation.transform(normPoint);

System.out.println("normal trans = " +normPoint.x + ", "+normPoint.y+", "+normPoint.z);
//PRINT STATEMENT RETURNS: normal trans = 0.09016449, 0.99534255, 0.03411238    

I was hoping the transform would produce x and z values of or very close to 0. While the logic makes sense to me, I'm obviously missing something..

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

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

发布评论

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

评论(1

静谧幽蓝 2024-11-24 09:48:08

如果您的目标是平行于 y 轴旋转向量,为什么不能使用向量的大小手动设置它并将向量设置为 <0, MAGNITUDE, 0> ?

另外,您应该知道,将向量旋转为直接指向 +Y 或 -Y 可能会导致某些旋转实现中断,因为它们根据“世界向上”向量或 <0,1,0> 进行操作。您可以通过构建自己的旋转系统并使用“world out”向量<0,0,1>来解决这个问题。直接向上旋转时。

如果您有其他目的,fastgraph 帮助我构建旋转矩阵。

最好了解正在发生的事情的数学原理,以便您知道将来要做什么。

If your goal is to rotate a vector parallel to the y axis, why can't you just manually set it using the magnitude of the vector and setting your vector to <0, MAGNITUDE, 0>?

Also, you should know that rotating a vector to be directly pointing +Y or -Y can cause some rotation implementations to break, since they operate according to the "world up" vector, or, <0,1,0>. You can solve this by building your own rotation system and using the "world out" vector <0,0,1> when rotating directly up.

If you have some other purpose for this, fastgraph helped me with building rotation matrices.

It's best to understand the math of what's going on so that you know what to do in the future.

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