我想调整矩阵的方向,使向上 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/