简体   繁体   中英

3D Math / 2D Rotation Calculation: Split/cut 3D model?

I'm trying to rotate a 3D object on its Z axis (up/down).

public void RotateY(float angle)
{
    foreach (CoordinateVertices cv in this.GetAll<CoordinateVertices>())
    {
        for (int i = 0; i < cv.Coordinates.Length; i++)
        {
            Vector3 old = cv.Coordinates[i];

            float theta = Math.Atan2(old.Y, old.X) + angle;
            float rayon = Math.Sqrt(Math.Pow(old.X, 2) + Math.Pow(old.Y, 2));

            cv.Coordinates[i] = new Vector3(Math.Cos(theta) * rayon, Math.Sin(theta) * rayon, old.Z);
        }
    }
}

The trigonometry is fairly simple, and it seems to work fine, but for some reason, my 3D object gets cut in half.

对照

Does anybody have an idea of what's going on? I would have posted this on the maths StackExchange, but it might be a problem with my programming too, and the trigonometry is really simple.

Edit: The following is an alternative for the doing the same as the above. It took me a few minutes to realize the following solution is identical to the code initially posted.

It should look like this:

double Xnew = X * cos(theta) + Y * sin(theta);
double Ynew = Y * cos(theta) - X * sin(theta);

Or in your code:

public void RotateY(float angle)
{
    foreach (CoordinateVertices cv in this.GetAll<CoordinateVertices>())
    {
        for (int i = 0; i < cv.Coordinates.Length; i++)
        {
            Vector3 old = cv.Coordinates[i];
            float xnew = old.X * Math.Cos(angle) + old.Y * Math.Sin(angle);
            float ynew = old.Y * Math.Cos(angle) - old.X * Math.Sin(angle);

            cv.Coordinates[i] = new Vector3(xnew, ynew, old.Z);
        }
    }
}

The above code assumes you're rotating about the origin. If you're not rotating about the origin, you just need to translate to the origin, rotate, then translate back.

See here for more details: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation

As has been noted, nothing wrong with your code. However, you may also be interested in using the Transform function (which can operate on your entire array of coordinates at once). Vector3.Transform (Vector3[], Matrix) . You can get your rotation with a rotation matrix calculated for a given angle, theta, about any axis. I would expect this to be significantly faster for large numbers of points. (Less trig calculations, and possibly hardware acceleration)

Actually, the bug disappeared, out of nowhere. I went on to test some more values, and they worked. I went back to the same value as before, and it worked. This is ridiculous, it always happens to me.

What's the name for that? Bugs that disappear by themselves.

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