Yes, I think I understand it quite well now, and it's working good. Here's the whole class now.
Code:
#ifndef VEC3F_H_INCLUDED
#define VEC3F_H_INCLUDED
#include <iostream>
#include <math.h>
class Vec3f {
public:
Vec3f();
Vec3f(float xi, float yi, float zi);
~Vec3f() {}
float x,y,z;
Vec3f operator=(const Vec3f& rhs);
Vec3f operator+(const Vec3f& rhs);
Vec3f operator-(const Vec3f& rhs);
Vec3f operator*(const float& rhs);
Vec3f operator/(const float& rhs);
Vec3f operator+=(const Vec3f& rhs);
Vec3f operator-=(const Vec3f& rhs);
Vec3f operator*=(const float& rhs);
Vec3f operator/=(const float& rhs);
float getMag();
void normalize();
float dot(Vec3f rhs);
Vec3f cross(Vec3f rhs);
float getXAngle();
float getYAngle();
void setAngles(float xa, float ya);
friend std::ostream& operator<< (std::ostream& stream, const Vec3f& v);
protected:
float xAngle_;
float yAngle_;
};
#endif //VEC3F_H_INCLUDED
Code:
#include "vec3f.h"
Vec3f::Vec3f()
{
x = y = z = 0.0;
}
Vec3f::Vec3f(float xi, float yi, float zi)
{
x = xi;
y = yi;
z = zi;
}
Vec3f Vec3f::operator=(const Vec3f& rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
return *this;
}
Vec3f Vec3f::operator+(const Vec3f& rhs)
{
return Vec3f(x + rhs.x, y + rhs.y, z + rhs.z);
}
Vec3f Vec3f::operator-(const Vec3f& rhs)
{
return Vec3f(x - rhs.x, y - rhs.y, z - rhs.z);
}
Vec3f Vec3f::operator*(const float& rhs)
{
return Vec3f(x * rhs, y * rhs, z * rhs);
}
Vec3f Vec3f::operator/(const float& rhs)
{
return Vec3f(x / rhs, y / rhs, z / rhs);
}
Vec3f Vec3f::operator+=(const Vec3f& rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Vec3f Vec3f::operator-=(const Vec3f& rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
Vec3f Vec3f::operator*=(const float& rhs)
{
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
Vec3f Vec3f::operator/=(const float& rhs)
{
x /= rhs;
y /= rhs;
z /= rhs;
return *this;
}
float Vec3f::getMag()
{
return sqrtf(x*x + y*y + z*z);
}
void Vec3f::normalize()
{
*this /= this->getMag();
}
float Vec3f::dot(Vec3f rhs)
{
float r = 0.0;
r = x * rhs.x;
r += y * rhs.y;
r += z * rhs.z;
return r;
}
Vec3f Vec3f::cross(Vec3f rhs)
{
Vec3f r;
r.x = (this->y * rhs.z) - (this->z * rhs.y);
r.y =-((this->x * rhs.z) - (this->z * rhs.x));
r.z = (this->x * rhs.y) - (this->y * rhs.x);
return r;
}
float Vec3f::getXAngle()
{
/*code here to calculate the angle*/
return xAngle_;
}
float Vec3f::getYAngle()
{
/*code here to calculate the angle*/
return yAngle_;
}
void Vec3f::setAngles(float xa, float ya)
{
/*See notes about this function*/
xAngle_ = xa;
yAngle_ = ya;
x = -sinf(xa);
y = tanf(ya);
z = -cosf(xa);
this->normalize();
}
std::ostream& operator<<(std::ostream& stream, const Vec3f& v)
{
std::cout << "x = " << v.x << ", y = " << v.y << ", z = " << v.z;
return stream;
}
Let me know if you have any suggestions.