简体   繁体   English

方向矢量的旋转矩阵

[英]Rotation matrix for direction vector

I've been playing with some algorithms on the internet for a while and I can't seem to get them to work, so I'm tossing the question out here; 我已经在互联网上玩了一些算法一段时间,我似乎无法让它们工作,所以我在这里抛出问题;

I am attempting to render a velocity vector line from a point. 我试图从一个点渲染速度矢量线。 Drawing the line isn't difficult: just insert a line with length velocity.length into the graph. 绘制直线并不困难:只需将长度为velocity.length的直线插入图形中即可。 This puts the line centered at the point in the y-axis direction. 这使得线以y轴方向上的点为中心。 We need to get this now in the proper rotation and translation. 我们现在需要在适当的轮换和翻译中得到这个。

The translational vector is not difficult to calculate: it is half the velocity vector. 平移向量不难计算:它是速度向量的一半。 The rotational matrix, however, is being exceedingly elusive to me. 然而,旋转矩阵对我来说是非常难以捉摸的。 Given a directional vector <x, y, z> , what's the matrix I need? 给定方向向量<x, y, z> ,我需要什么矩阵?

Edit 1: Look; 编辑1:看; if you don't understand the question, you probably won't be able to give me an answer. 如果你不明白这个问题,你可能无法给我一个答案。

Here is what I currently have: 这是我目前拥有的:

Vector3f translation = new Vector3f();
                    translation.scale(1f/2f, body.velocity);

                    Vector3f vec_z = (Vector3f) body.velocity.clone();
                    vec_z.normalize();

                    Vector3f vec_y; // reference vector, will correct later
                    if (vec_z.x == 0 && vec_z.z == 0) {
                        vec_y = new Vector3f(-vec_z.y, 0f, 0f); // could be optimized
                    } else {
                        vec_y = new Vector3f(0f, 1f, 0f);
                    }
                    Vector3f vec_x = new Vector3f();
                    vec_x.cross(vec_y, vec_z);
                    vec_z.normalize();

                    vec_y.cross(vec_x, vec_z);
                    vec_y.normalize();
                    vec_y.negate();

                    Matrix3f rotation = new Matrix3f(
                        vec_z.z, vec_z.x, vec_z.y,
                        vec_x.z, vec_x.x, vec_x.y,
                        vec_y.z, vec_y.x, vec_y.y
                    );

                    arrowTransform3D.set(rotation, translation, 1f);

based off of this article . 基于这篇文章 And yes, I've tried the standard rotation matrix (vec_x.x, vec_y.x, etc) and it didn't work. 是的,我已经尝试过标准旋转矩阵(vec_x.x,vec_y.x等)并且它不起作用。 I've been rotating the columns and rows to see if there's any effect. 我一直在旋转列和行,看看是否有任何影响。

Edit 2: 编辑2:

Apologies about the rude wording of my comments. 抱歉我的评论粗鲁的措辞。

So it looks like there were a combination of two errors; 所以看起来有两个错误的组合; one of which House MD pointed out (really bad naming of variables: vec_z was actually vec_y , and so on), and the other was that I needed to invert the matrix before passing it off to the rendering engine (transposing was close!). 其中一个House MD指出(变量的命名非常糟糕: vec_z实际上是vec_y ,依此类推),另一个是我需要在将矩阵传递给渲染引擎之前将其反转(转置接近!)。 So the modified code is: 所以修改后的代码是:

Vector3f vec_y = (Vector3f) body.velocity.clone();
                    vec_y.normalize();

                    Vector3f vec_x; // reference vector, will correct later
                    if (vec_y.x == 0 && vec_y.z == 0) {
                        vec_x = new Vector3f(-vec_y.y, 0f, 0f); // could be optimized
                    } else {
                        vec_x = new Vector3f(0f, 1f, 0f);
                    }

                    Vector3f vec_z = new Vector3f();
                    vec_z.cross(vec_x, vec_y);
                    vec_z.normalize();

                    vec_x.cross(vec_z, vec_y);
                    vec_x.normalize();
                    vec_x.negate();

                    Matrix3f rotation = new Matrix3f(
                        vec_x.x, vec_x.y, vec_x.z,
                        vec_y.x, vec_y.y, vec_y.z,
                        vec_z.x, vec_z.y, vec_z.z
                    );
                    rotation.invert();

应该是你

Dupe. 欺骗。

The question there involves getting a rotation to a certain axis, whereas I'm concerned with getting a rotation matrix. 那里的问题涉及到某个轴的旋转,而我关心的是获得旋转矩阵。

Gee, I wonder if you could turn convert one to the other ? 哎呀,我想知道你是否可以将一个转换为另一个

BTW, your current solution of picking an arbitrary y axis and then reorthogonalising should work fine; 顺便说一句,你当前选择任意y轴然后重新正交化的解决方案应该可以正常工作; it looks bugged though, or at least badly written. 它虽然看起来很麻烦,或者至少写得不好。 ' z_vec ' is not a good variable-name for the y-axis. ' z_vec '不是y轴的良好变量名。 What's with the 'z,x,y' ordering, anyway? 无论如何,'z,x,y'的排序是什么?

If it still doesn't work, try making random changes until it does - transpose the matrix, negate vectors until you have an even number of sign errors, that kind of thing. 如果它仍然不起作用,尝试进行随机更改,直到它完成 - 转置矩阵,否定向量,直到你有偶数个符号错误,这种事情。

Also your tone of voice comes across as sort-of rude, given that you're asking strangers to spend their time helping you. 考虑到你要求陌生人花时间帮助你,你的语气也会变得粗鲁无礼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM