将带有面部的物体旋转到另一个物体的问题。 [C ++]

问题描述 投票:0回答:1

我想将一个物体的正面旋转到另一个物体的中心,但是我遇到了一些问题:当我尝试将一个物体旋转到另一个物体并且它位于X轴上时,它正常工作[前两个截图] ,但是当我尝试像截图一样旋转它时,一切都崩溃了[后两个截图]。

Before1:
1 0 0 0 
0 1 0 0 
0 0 1 0 
0 0 0 1 

After1:
0 0 -1 0 
-0 -1 0 0 
1 0 0 0 
0 0 0 1 

Before2:
0 0 -1 0 
-0 -1 0 0 
1 0 0 0 
0 0 0 1 

After2:
0 0 -0.707107 0 
0.5 -0.5 0 0 
0.707107 -0.707107 0 0 
0 0 0 1 

这是我的代码:

void ConcreteObject::faceObjectTo(ConcreteObject otherObject) {
    Vector<double> temp = {0, 1, 0};

    Vector<double> forward = otherObject.getCenter() - this->getCenter();
    forward.normalize();

    Vector<double> right = temp.cross(forward);
    right.normalize();

    Vector<double> up = forward.cross(right);

    Matrix<double> newMatrix = this->getTransformMatrix().getCurrentState();

    newMatrix(0, 0) = right[0];
    newMatrix(0, 1) = right[1];
    newMatrix(0, 2) = right[2];

    newMatrix(1, 0) = up[0];
    newMatrix(1, 1) = up[1];
    newMatrix(1, 2) = up[2];

    newMatrix(2, 0) = forward[0];
    newMatrix(2, 1) = forward[1];
    newMatrix(2, 2) = forward[2];

    TransformMatrix newObjectMatrix(newMatrix);

    this->setTransformMatrix(newObjectMatrix);
}
c++ math 3d rotation
1个回答
0
投票

你需要正确归一化,没有理由使temp和forward成为正交,因此即使它们是单位向量,它们的交叉产物也不必如此。

© www.soinside.com 2019 - 2024. All rights reserved.