我已经在互联网上玩了一段时间的一些算法,但我似乎无法让它们工作,所以我在这里抛出这个问题;
我正在尝试从一个点渲染速度矢量线。画线并不困难:只需velocity.length
在图中插入一条带长度的线。这使线以 y 轴方向的点为中心。我们现在需要在适当的旋转和平移中得到它。
平移向量不难计算:它是速度向量的一半。然而,旋转矩阵对我来说非常难以捉摸。给定一个方向向量<x, y, z>
,我需要什么矩阵?
编辑1:看;如果你不明白这个问题,你可能无法给我答案。
这是我目前拥有的:
Vector3f 翻译 = 新 Vector3f(); 平移.scale(1f/2f, body.velocity); Vector3f vec_z = (Vector3f) body.velocity.clone(); vec_z.normalize(); Vector3f vec_y; // 参考向量,稍后会更正 如果(vec_z.x == 0 && vec_z.z == 0){ vec_y = new Vector3f(-vec_z.y, 0f, 0f); // 可以优化 } 别的 { 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 旋转 = 新 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(旋转,平移,1f);
根据这篇文章。是的,我已经尝试过标准旋转矩阵(vec_x.x、vec_y.x 等)但没有奏效。我一直在旋转列和行以查看是否有任何效果。
编辑2:
对我评论的粗鲁措辞表示歉意。
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!). 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();