如何计算基于方向向量的旋转矩阵



在我的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个矢量(见下文链接)。对于向前移动,然后将对象的位置加上速度乘以向前矢量,即:位置+=速度*向前

使用下面发布的完整示例代码来了解如何实现您自己的版本。http://cs.lmu.edu/雷/notes/flightsimulator/

最新更新