Sunday, May 22, 2011

Attempting to solve the rotation problem




Hi Casey,
Thank you so much! You've been a great help. The code example by you is exactly what we want and it helped resolve the legacy rotation problem in our Knot_A_Problem project from last semester.
Now we can have nicer knot strings: the flat one has gone, and the string can be rotated to any direction in 3D world smoothly. Only, it seems to have been rotated too much in some areas. I'm thinking of the reasons of it. If you know what's wrong there, could you please let me know? The over-rotated ones are shown on the screenshots below:



The code used for it is as following:
    rotationAxis = Vector3.Normalize(calVectors[i + 1] - calVectors[i]);
    if (rotationAxis == Vector3.Up)
    {
        rotationAngle = 0f;
    }
    else if (rotationAxis == -Vector3.Up)
    {
        rotationAxis = Vector3.Backward;
        rotationAngle = MathHelper.Pi;
    }
    else
    {
                    //angle between circle's base axis and up vector
        rotationAngle = -(float)Math.Acos(Vector3.Dot(rotationAxis, Vector3.Up));
                    //find a vector perpendicular to both the circle's base axis and the up vector, this will be your rotation axis
        rotationAxis = Vector3.Cross(rotationAxis, Vector3.Up);
        rotationAxis.Normalize();
    }
                    //construct the rotation matrix from the rotation axis and rotation angle
    Matrix rotationMatrix = Matrix.CreateFromAxisAngle(rotationAxis, rotationAngle);
    Matrix magicMatrix = rotationMatrix * Matrix.CreateTranslation(calVectors[i]);
    Vector3[] newCircle = new Vector3[baseCircle.segPointNo];
    Vector3.Transform(baseCircle.vecotrCircle, ref magicMatrix, newCircle);
Do you think it's the Gimbal lock issue or do we need to make more check on the 'rotationAngle = -(float)Math.Acos(Vector3.Dot(rotationAxis, Vector3.Up));' ?
Kind regards,
Jun 

From: Casey Chow [caseyc@uow.edu.au]
Sent: Thursday, April 28, 2011 6:22 AM
To: Jun Ye
Cc: Luke McAven; philip_m_davies@hotmail.com; timothy overton [timothy_overton@hotmail.com]; Xun He; jernej.cesar@gmail.com
Subject: RE: Rotation Issue - CSCI321 - From Jun
Hi Jun,
Following my previous email, depending on what you are trying to do, I don't think Euler angles is what you need. If I understand correctly, for your purposes what you can do is to construct the rotation matrix from an axis and an angle. I've hacked up some XNA code (attached) that shows you how to do this, you should be able to figure it out from there.
Regards,
Casey


Thursday, April 28, 2011 6:22 AM
You replied on 4/28/2011 6:13 PM.
Hi Jun,
Following my previous email, depending on what you are trying to do, I don't think Euler angles is what you need. If I understand correctly, for your purposes what you can do is to construct the rotation matrix from an axis and an angle. I've hacked up some XNA code (attached) that shows you how to do this, you should be able to figure it out from there.
Regards,
Casey


Luke McAven ‎[lukemc@uow.edu.au]‎‎; philip_m_davies@hotmail.com‎; timothy overton [timothy_overton@hotmail.com]‎; Xun He‎; jernej.cesar@gmail.com
Wednesday, April 27, 2011 7:45 PM
Hi Jun,
Apologies for the late reply.
I think you've got the wrong idea about Euler angles, matrices and quaternions. Whether or not you use Euler angles or quaternions, you still perform transformations using matrices (in XNA Matrix.CreateFromQuaternion(quaternion); )
However, if I understand you correctly, what you're asking instead (from "The problem is how we could calculate the Euler angle... ") is how to calculate the Euler angles from Vector.Up to the rotated vector?
If so, you can do this by calculating the projection of the rotated vector onto the xz, yz, and xy planes, then finding the individual angles between the projected vectors and the x, y, and z reference vectors respectively.
Regards,
Casey

No comments:

Post a Comment