如何根据方向向量计算旋转矩阵?
How to compute the Rotation-Matrix based on a Direction-Vector?
在我的 3D 世界实现中,我使用方向向量(单位向量)来决定我的 3D 对象的方向。
每个 3D 对象都有自己的方向矢量,默认情况下方向为 V3(1, 0, 0),原点为 V3(0,0,0)。
这就是我应用定向旋转矩阵 "D" 的方式(矩阵 "A" 用于将 3D 对象围绕其方向矢量作为轴旋转,这似乎工作正常):
Model3D model = actor.model;
// Loops through all the edges in the model
for (int i = 0; i < model.edges.length; i++) {
M3 D = directionMatrix(actor);
M3 R = rotationMatrix(actor);
// Draws a line based on each edge in the model.
// Each line consists of two points a and b.
// The matrix R rotates the points around a given axis.
// The D matrix rotates the points towards a given axis - not around it.
S.drawLine(g,
D.mul(R.mul(model.points[model.edges[i].a])).scale(actor.scale),
D.mul(R.mul(model.points[model.edges[i].b])).scale(actor.scale)
);
}
这就是我计算当前定向旋转矩阵的方式 "D":
public M3 directionalRotationMatrix(c_Actor3D actor) {
double x = Math.atan2(actor.direction.z, actor.direction.y);
double y = Math.atan2(actor.direction.x, actor.direction.z);
double z = Math.atan2(actor.direction.y, actor.direction.x);
double sin_x = Math.sin(x), sin_y = Math.sin(y), sin_z = Math.sin(z);
double cos_x = Math.cos(x), cos_y = Math.cos(y), cos_z = Math.cos(z);
return new M3(
cos_x * cos_y, (cos_x * sin_y * sin_z) - (sin_x * cos_z),
(cos_x * sin_y * cos_z) + (sin_x * sin_z), sin_x * cos_y, (sin_x * sin_y * sin_z) + (cos_x * cos_z),
(sin_x * sin_y * cos_z) - (cos_x * sin_z), -sin_y, cos_y * sin_z, cos_y * cos_z);
}
我的问题 是创建正确的方向旋转矩阵,使 3D 对象沿其各自方向向量的方向旋转。
我完全不确定我做错了什么...我的想法是先将立方体朝一个方向旋转,然后围绕该方向的轴旋转立方体。毕竟位置变换等等
谢谢大家的帮助!
听起来您正试图将 3D 对象沿其前向矢量的方向移动。为此,您需要对象的位置 (x,y,z) 和 3 个向量(向前、向上和向右)。您可以使用基于俯仰偏航和滚动的矢量数学来旋转 3 个矢量(见下文 link)。对于向前运动,然后添加对象的位置加上速度乘以向前矢量,即:position += speed * forward
使用此处发布的以下完整示例代码了解如何实现您自己的版本。 http://cs.lmu.edu/~ray/notes/flightsimulator/
在我的 3D 世界实现中,我使用方向向量(单位向量)来决定我的 3D 对象的方向。
每个 3D 对象都有自己的方向矢量,默认情况下方向为 V3(1, 0, 0),原点为 V3(0,0,0)。
这就是我应用定向旋转矩阵 "D" 的方式(矩阵 "A" 用于将 3D 对象围绕其方向矢量作为轴旋转,这似乎工作正常):
Model3D model = actor.model;
// Loops through all the edges in the model
for (int i = 0; i < model.edges.length; i++) {
M3 D = directionMatrix(actor);
M3 R = rotationMatrix(actor);
// Draws a line based on each edge in the model.
// Each line consists of two points a and b.
// The matrix R rotates the points around a given axis.
// The D matrix rotates the points towards a given axis - not around it.
S.drawLine(g,
D.mul(R.mul(model.points[model.edges[i].a])).scale(actor.scale),
D.mul(R.mul(model.points[model.edges[i].b])).scale(actor.scale)
);
}
这就是我计算当前定向旋转矩阵的方式 "D":
public M3 directionalRotationMatrix(c_Actor3D actor) {
double x = Math.atan2(actor.direction.z, actor.direction.y);
double y = Math.atan2(actor.direction.x, actor.direction.z);
double z = Math.atan2(actor.direction.y, actor.direction.x);
double sin_x = Math.sin(x), sin_y = Math.sin(y), sin_z = Math.sin(z);
double cos_x = Math.cos(x), cos_y = Math.cos(y), cos_z = Math.cos(z);
return new M3(
cos_x * cos_y, (cos_x * sin_y * sin_z) - (sin_x * cos_z),
(cos_x * sin_y * cos_z) + (sin_x * sin_z), sin_x * cos_y, (sin_x * sin_y * sin_z) + (cos_x * cos_z),
(sin_x * sin_y * cos_z) - (cos_x * sin_z), -sin_y, cos_y * sin_z, cos_y * cos_z);
}
我的问题 是创建正确的方向旋转矩阵,使 3D 对象沿其各自方向向量的方向旋转。
我完全不确定我做错了什么...我的想法是先将立方体朝一个方向旋转,然后围绕该方向的轴旋转立方体。毕竟位置变换等等
谢谢大家的帮助!
听起来您正试图将 3D 对象沿其前向矢量的方向移动。为此,您需要对象的位置 (x,y,z) 和 3 个向量(向前、向上和向右)。您可以使用基于俯仰偏航和滚动的矢量数学来旋转 3 个矢量(见下文 link)。对于向前运动,然后添加对象的位置加上速度乘以向前矢量,即:position += speed * forward
使用此处发布的以下完整示例代码了解如何实现您自己的版本。 http://cs.lmu.edu/~ray/notes/flightsimulator/