c - 旋转矩阵,使 Up vector 等于另一个 vector

标签 c opengl matrix rotation

我想调整矩阵的方向,使向上 vector 与另一个 vector 朝向相同的方向。前向和右向 vector 的方向无关紧要。

例如:

matrix4 m; // m.Up = 0, 1, 0
vector3 v = V3(1,0,0);

// Then I think I have to get the rotation from m.Up and v
// And then rotate the matrix accordingly

但我不知道该怎么做,我可能是错的。

最佳答案

这是矩阵特别有用的旋转问题之一

首先,将您的矩阵拆分为三个分量 vector (向上、向前和向右)。

将向上 vector 设置为您想要的样子。然后,调整您的前向和右向 vector ,使它们成直角,一种简单的方法是使用叉积。

例如:

//Gets a perpendicular vector
V3 V3::Perp() {
    V3 perp = v.Cross(NewV3(-1, 0, 0));
    if(perp.Length() == 0) {
        // If v is too close to -x try -y
        perp = v.Cross(NewV3(0, -1, 0));
    }
    return perp.Unit();
}
//up = Whatever you need
forward = up.Perp()
right = cross(up, forward);

在那之后,将你的 vector 插回到矩阵中,瞧 :D。

关于c - 旋转矩阵,使 Up vector 等于另一个 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4898987/

相关文章:

处理器速度的C程序

c++ - 使用中心点和比例因子变换顶点?

C++ - std::array 类型的二维矩阵的定义

c++ - 在满载机器中对应用程序进行基准测试

c - 如何将字符 'G' 转换为字符串 "47"(十六进制)

c++ - 在纹理 OpenGL 中使用 GL_LINEAR 时的外部伪影

c++ - Matrix Marginalization(边际分布)

python:如何扩展具有相邻重复列的 numpy 数组?

c++ - 用C++编译器编译C99文件

c++ - 透视投影和 View 矩阵 : Both depth buffer and triangle face orientation are reversed in OpenGL