绕轴旋转矢量时得到错误的值

时间:2019-02-08 00:25:20

标签: c++ vector 3d

我正在尝试向我的班级添加旋转函数,以绕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旋转,我会得到很好的结果,但是除此之外,它不起作用。

1 个答案:

答案 0 :(得分: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值太舍入,因此值为false

您可以为pi值做类似的事情

const float Pi = 3.1415926535;
相关问题