旋转围绕轴线矢量时获得错误的值[关闭]

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

我试图旋转功能添加到我的班,围绕X,Y,Z旋转 - 轴,但输出不正是我所期望的

我确信,我的公式是正确的,他们似乎是正确的,但我不知道。我把他们从这个:Rotating a Vector in 3D Space

#include <iostream>

using namespace std;

#include <math.h>

// Vector class, to handle all the vector operations for us
// Thanks to : https://stackoverflow.com/questions/14607640/rotating-a-vector-in-3d-space
class cVector
{
public:
    float x;
    float y;
    float z;
    // Constructor
    cVector();
    cVector(float x1, float y1, float z1);

    // returns the vector's magnitude
    float Magnitude();

    // Normalize ( change length to 1, while keeping the same direction)
    void Normalize();


    // Rotate around the Axis
    void RotateX(float angle);
    void RotateY(float angle);
    void RotateZ(float angle);

    // TODO : Add operators for Addition & Substraction

    // Addition
    cVector operator+(cVector const& v1) const
    {
        return cVector(x + v1.x,
                        y + v1.y,
                        z + v1.z);
    }
    void operator+=(cVector const& v1)
    {
        x += v1.x;
        y += v1.y;
        z += v1.z;
    }
    // Substraction
    cVector operator-(cVector const& v1) const
    {
        return cVector(x - v1.x,
                        y - v1.y,
                        z - v1.z);
    }
    void operator-=(cVector const& v1)
    {
        x -= v1.x;
        y -= v1.y;
        z -= v1.z;
    }

    // Multiplication
    void operator*=(const float scalar)
    {
        x *= scalar;
        y *= scalar;
        z *= scalar;
    }
    cVector operator*(const float scalar) const
    {
        return cVector(x * scalar,
                        y * scalar,
                        z * scalar);
    }

    // Division
    void operator/=(const float scalar)
    {
        x /= scalar;
        y /= scalar;
        z /= scalar;
    }
    cVector operator/(const float scalar) const
    {
        return cVector(x / scalar,
                        y / scalar,
                        z / scalar);
    }
};



// Constructor
cVector::cVector()
{

}

cVector::cVector(float x1, float y1, float z1)
{
    x = x1;
    y = y1;
    z = z1;
}

// returns the vector's magnitude
float cVector::Magnitude()
{
    return sqrt((x * x) + (y * y) + (z * z));
}

// Normalize ( change length to 1, while keeping the same direction)
void cVector::Normalize()
{
    float flMagnitude = Magnitude();

    // We devide the coordinates by the magnitude
    x /= flMagnitude;
    y /= flMagnitude;
    z /= flMagnitude;
}

// Rotate around the Axis
void cVector::RotateX(float angle)
{
    y = y * cos(angle) - z * sin(angle);
    z = y * sin(angle) + z * cos(angle);
}

void cVector::RotateY(float angle)
{
    x = (x * cos(angle)) + (z * sin(angle));
    z = (-x * sin(angle)) + (z * cos(angle));
}

void cVector::RotateZ(float angle)
{
    x = x * cos(angle) - y * sin(angle);
    y = x * sin(angle) + y * cos(angle);
}

void PrintVector(cVector vec)
{
    cout << "X : " << vec.x << " Y : " << vec.y << " Z : " << vec.z << endl;
}

// TODO : Add operators for Addition & Substraction
int main()
{
    cout << "Hello world!" << endl;



    cVector vec(10, 0, 0);

    vec.RotateZ(1.57f);

    PrintVector(vec);

    cin.get();
    return 0;
}

我想到的方法来保持载体的相同幅度,并返回(0,10,0),因为我对PI / 2旋转,但是这不是我得到。显然,如果我通过PI旋转,我得到一个好的结果,但除此之外,这是行不通的。

c++ vector 3d
1个回答
1
投票

首先在你的循环例如在RotateZ你应该保存的X,一些暂时的,因为如果你修改和再尝试使用它对于Y这显然会因为你的错误,即你应该做这样的事情

void cVector::RotateZ(float angle)
{
    float temp = x;
    x = x * cos(angle) - y * sin(angle);
    y = temp * sin(angle) + y * cos(angle);
}

其次您将得到pi的值是太过分圆润因此它们的值都是假的

你可以为你的PI值,这样做

const float Pi = 3.1415926535;
© www.soinside.com 2019 - 2024. All rights reserved.