Here is the code for moving an object through 3D space in my program. Whats wrong with it is that it moves, but not in the right direction. Could anybody tell me what part of this is wrong? Thanks!
Code:
void MATH::UpdatePos(VEC *vPos, float speed)
{
vPos->view.x = vPos->pos.x + sin(vPos->yaw) * sin(vPos->pitch+90);
vPos->view.y = vPos->pos.y + cos(vPos->yaw);
vPos->view.z = vPos->pos.z + cos(vPos->yaw) * -sin(vPos->pitch+90);
// Get the current view vector (the direction we are looking)
CVector3 vVector = vPos->view - vPos->pos;
vVector = Normalize(vVector);
vPos->pos.x += vVector.x * speed; // Add our acceleration to our position's X
vPos->pos.y += vVector.y * speed; // Add our acceleration to our position's Y
vPos->pos.z += vVector.z * speed; // Add our acceleration to our position's Z
vPos->view.x += vVector.x * speed; // Add our acceleration to our view's X
vPos->view.y += vVector.y * speed; // Add our acceleration to our view's Y
vPos->view.z += vVector.z * speed; // Add our acceleration to our view's Z
}
Here are the class defininions if you need them:
Code:
struct CVector3
{
public:
// A default constructor
CVector3() {}
// This is our constructor that allows us to initialize our data upon creating an instance
CVector3(float X, float Y, float Z)
{
x = X; y = Y; z = Z;
}
// Here we overload the + operator so we can add vectors together
CVector3 operator+(CVector3 vVector)
{
// Return the added vectors result.
return CVector3(vVector.x + x, vVector.y + y, vVector.z + z);
}
// Here we overload the - operator so we can subtract vectors
CVector3 operator-(CVector3 vVector)
{
// Return the subtracted vectors result
return CVector3(x - vVector.x, y - vVector.y, z - vVector.z);
}
// Here we overload the * operator so we can multiply by scalars
CVector3 operator*(float num)
{
// Return the scaled vector
return CVector3(x * num, y * num, z * num);
}
// Here we overload the / operator so we can divide by a scalar
CVector3 operator/(float num)
{
// Return the scale vector
return CVector3(x / num, y / num, z / num);
}
float x, y, z;
};
struct VEC
{
CVector3 pos; //The position
float pitch, yaw, roll; //The pitch, yaw, and roll gotten from key input
CVector3 view; //The view vector calculated
};