Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Vector3.cpp by Vassillen M. Chizhov
- #include "Vector3.h"
- namespace myGameMathLib
- {
- Vector3 Vector3::Zero(FP(0), FP(0), FP(0));
- //ctors
- Vector3::Vector3()
- :x(FP(0)), y(FP(0))
- {
- }
- Vector3::Vector3(FP value)
- : x(value), y(value)
- {
- }
- Vector3::Vector3(FP x, FP y, FP z)
- : x(x), y(y), z(z)
- {
- }
- Vector3::Vector3(const Vector3& another)
- : x(another.x), y(another.y), z(another.z)
- {
- }
- //member operators
- Vector3& Vector3::operator=(const Vector3& another)
- {
- x = another.x;
- y = another.y;
- z = another.z;
- return *this;
- }
- Vector3& Vector3::operator+=(const Vector3& another)
- {
- x += another.x;
- y += another.y;
- z += another.z;
- return *this;
- }
- Vector3& Vector3::operator-=(const Vector3& another)
- {
- x -= another.x;
- y -= another.y;
- z -= another.z;
- return *this;
- }
- //componentwise multiplication
- Vector3& Vector3::operator*=(const Vector3& another)
- {
- x *= another.x;
- y *= another.y;
- z *= another.z;
- return *this;
- }
- //componentwise division
- Vector3& Vector3::operator/=(const Vector3& another)
- {
- x /= another.x;
- y /= another.y;
- z /= another.z;
- return *this;
- }
- Vector3& Vector3::operator*=(FP scalar)
- {
- x *= scalar;
- y *= scalar;
- z *= scalar;
- return *this;
- }
- Vector3& Vector3::operator/=(FP scalar)
- {
- x /= scalar;
- y /= scalar;
- z /= scalar;
- return *this;
- }
- Vector3 Vector3::operator+()
- {
- return *this;
- }
- Vector3 Vector3::operator-()
- {
- return Vector3(-x, -y, -z);
- }
- FP Vector3::lengthSquared() const
- {
- return (x*x + y*y + z*z);
- }
- FP Vector3::length() const
- {
- return sqrtf(x*x + y*y + z*z);
- }
- Vector3& Vector3::normalize()
- {
- FP length = sqrt(x*x + y*y + z*z);
- x /= length;
- y /= length;
- z /= length;
- return *this;
- }
- Vector3& Vector3::reflect(const Vector3& another)
- {
- Vector3 temp(another);
- temp.normalize();
- return (dotProduct(*this, temp)*temp - *this);
- }
- Vector3& Vector3::reflectNormalized(const Vector3& another)
- {
- return (dotProduct(*this, another)*another - *this);
- }
- Vector3& Vector3::clearTo(FP value)
- {
- x = value;
- y = value;
- z = value;
- return *this;
- }
- //non-member operators/functions
- //compare
- bool operator==(const Vector3& vec1, const Vector3& vec2)
- {
- return (zero(vec1.x - vec2.x) && zero(vec1.y - vec2.y) && zero(vec1.z-vec2.z));
- }
- bool operator!=(const Vector3& vec1, const Vector3& vec2)
- {
- return (!zero(vec1.x - vec2.x) || !zero(vec1.y - vec2.y) || !zero(vec1.z-vec2.z));
- }
- //arithmetic
- Vector3 operator+(const Vector3& vec1, const Vector3& vec2)
- {
- return Vector3(vec1.x + vec2.x, vec1.y + vec2.y, vec1.z+vec2.z);
- }
- Vector3 operator-(const Vector3& vec1, const Vector3& vec2)
- {
- return Vector3(vec1.x - vec2.x, vec1.y - vec2.y, vec1.z-vec2.z);
- }
- //componentwise multiplication
- Vector3 operator*(const Vector3& vec1, const Vector3& vec2)
- {
- return Vector3(vec1.x * vec2.x, vec1.y * vec2.y, vec1.z*vec2.z);
- }
- //componentwise division
- Vector3 operator/(const Vector3& vec1, const Vector3& vec2)
- {
- return Vector3(vec1.x / vec2.x, vec1.y / vec2.y, vec1.z/vec2.z);
- }
- //scalar multiplication and division
- Vector3 operator*(const Vector3& vec, FP scalar)
- {
- return Vector3(vec.x * scalar, vec.y * scalar, vec.z*scalar);
- }
- Vector3 operator*(FP scalar, const Vector3& vec)
- {
- return Vector3(scalar * vec.x, scalar*vec.y, scalar*vec.z);
- }
- Vector3 operator/(const Vector3& vec, FP scalar)
- {
- return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar);
- }
- Vector3 operator/(FP scalar, const Vector3& vec)
- {
- return Vector3(scalar / vec.x, scalar / vec.y, scalar / vec.z);
- }
- //dot product
- FP dotProduct(const Vector3& vec1, const Vector3& vec2)
- {
- return (vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z);
- }
- //cross Product
- Vector3 crossProduct(const Vector3& vec1, const Vector3& vec2)
- {
- return Vector3(vec1.y*vec2.z - vec1.z*vec2.y, vec1.z*vec2.x - vec1.x*vec2.z, vec1.x*vec2.y - vec1.y*vec2.x);
- }
- //length squared and length
- FP lengthSquared(const Vector3& vec)
- {
- return vec.lengthSquared();
- }
- FP length(const Vector3& vec)
- {
- //precision? - sqrtf?
- return vec.length();
- }
- Vector3& normalize(Vector3& vec)
- {
- return vec.normalize();
- }
- Vector3& reflect(Vector3& vec, const Vector3& around)
- {
- return vec.reflect(around);
- }
- Vector3& reflectNormalized(Vector3& vec, const Vector3& around)
- {
- return vec.reflectNormalized(around);
- }
- Vector3& clearTo(Vector3& vec, FP value)
- {
- vec.clearTo(value);
- return vec;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement