简体   繁体   中英

What is wrong with my rotation matrix?

//---------------------------------------------------------------------------
//  arbitraryRotate - rotates v theta degrees around n
//---------------------------------------------------------------------------
private static Vector3D arbitraryRotate(Vector3D v, Vector3D n, double theta)
{
    theta = Math.toRadians(theta);
    double cosTheta = Math.cos(theta);
    double sinTheta = Math.sin(theta);

    double M11 = n.x * n.x * (1 - cosTheta) + cosTheta;
    double M12 = n.x * n.y * (1 - cosTheta) + (n.z * sinTheta);
    double M13 = n.x * n.z * (1 - cosTheta) - (n.y * sinTheta);

    double M21 = n.x * n.y * (1 - cosTheta) - (n.z * sinTheta);
    double M22 = n.y * n.y * (1 - cosTheta) + cosTheta;
    double M23 = n.y * n.z * (1 - cosTheta) + (n.x * sinTheta);

    double M31 = n.x * n.z * (1 - cosTheta) + (n.y * sinTheta);
    double M32 = n.y * n.z * (1 - cosTheta) - (n.x * sinTheta);
    double M33 = n.z * n.z * (1 - cosTheta) + cosTheta;

    return new Vector3D(v.x * M11 + v.y * M21 + v.z * M31,
                        v.x * M12 + v.y * M22 + v.z * M32,
                        v.x * M13 + v.y * M23 + v.z * M33);
}

Could anyone tell me what is wrong with my matrix please? Rotation around the Z-Axis works perfectly however X and Y axis rotations result in distortions.

Your matrix is set up improperly. Here is how you rotate properly

  1. Create a rotation matrix that rotates your axis to lie along the XZ plane
  2. Create a rotation matrix that rotates that new axis to lie along the Z axis
  3. Use the Z-axis rotation matrix to rotate by theta degrees about the Z axis.
  4. Multiply by the inverse of 2
  5. Multiply by the inverse of 1.

You get

new vector = (Rx^-1)(Ry^-1)RzRyRx(original vector)

Here's a link that spells it out well. You don't have to extend your vectors and matrices to 4 dimensions - that's only for the purpose of translation if you're rotating about an arbitrary line in space, not a vector stemming from the origin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM