相对于另一个对象放置一个对象

时间:2019-07-12 21:55:37

标签: c++ linear-algebra maya

问题: 我有对象A,我知道对象A(x,y,z)的位置。我也知道对象A的欧拉角。我想将对象B定位为面向对象A的-z轴。

我的代码: 我在下面列出的代码将使用对象A并将其旋转,使其-z轴面向对象B的。

现在,我想编写代码,以便将对象B放置在面对对象A的-z轴的位置。

我觉得我本质上是在尝试从欧拉角和物体A的位置中找到“ targetPos”。

// Calculate the x,y,z rotation vectors
    MVector pos = nodeTranslation();
    MVector objectUpVector = {0.0, 1.0, 0.0};
    MVector zaxis = pos - targetPos; // pos = object A's position, targetPos = object B's position
    zaxis.normalize();
    MVector xaxis = objectUpVector^zaxis;
    xaxis.normalize();
    MVector yaxis = zaxis ^ xaxis;

    // from the rotation vectors obtain the euler angles
    double x = atan2(yaxis.z, zaxis.z);
    double s1 = sin(x);
    double c1 = cos(x);
    double c2 = sqrt(xaxis.x * xaxis.x + xaxis.y*xaxis.y);
    double y = atan2(-xaxis.z, c2);
    double z = atan2(s1*zaxis.x - c1*yaxis.x, c1*yaxis.y - s1*zaxis.y);

0 个答案:

没有答案