开发者

Rotate matrix so Up vector equals another vector

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开发者_JS百科 the matrix accordingly

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜