旋转矩阵,使 Up 向量等于另一个向量

发布于 2024-10-16 01:23:09 字数 269 浏览 1 评论 0原文

我想调整矩阵的方向,使向上向量与另一个向量面向相同的方向。向前和向右向量的方向并不重要。

例如:

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

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

I want to orient my matrix so that the Up vector it facing the same direction as another vector. The orientation of the Forward and Right vectors do not matter.

For example:

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

But I don't know how todo this and I may be wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぽ尐不点ル 2024-10-23 01:23:09

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

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

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

例如:

//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);

之后,将向量插回到矩阵中,瞧:D。

This is one of the rotation problems that matricies are particularly useful for

First, split your matrix into the three component vectors (up, forward, and right).

Set your up vector to what you want it to be. Then, adjust your forward and right vectors so that they are at right angles, an easy way to do this would be through the use of cross products.

For example:

//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);

After that, plug your vectors back into the matrix and voila :D.

梦晓ヶ微光ヅ倾城 2024-10-23 01:23:09

如果我理解正确,只需将矩阵中的轴设置为您选择的向量即可。正如您所说,前向和右向矢量并不重要,请将它们设置为任何值,只要它们与新的上轴正交即可。

If I understand correctly, simply set your up axis in the matrix to your chosen vector. As you say that the forward and right vectors do not matter, set them to anything as long as they are orthonormal to your new up axis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文