Simple and easy to use Vector3 class, by Zuka
Tested, it does work!
Please report any errors you discover to me.
Features include:
Quote: "
- Addition,
- Subtraction,
- Scalar multiplication,
- Multiplication,
- Division,
- Scalar division,
- Unit,
- Dot product,
- Cross product,
- Full Intellisense support.
"
Note that if you do #define Vector3_DEBUG (before #include "Vector3.h"), you will be able to use OutputVector3(Vector3) to print the x,y,z values of the Vector3 using std::cout.
Vector3.h
//Written by Kyle Emmerich, or "Zuka".
//I guess you can have the zlib license or whatever.
#ifndef Vector3_h
#define Vector3_h
class Vector3
{
private:
float tx, ty, tz;
public:
///Constructor
Vector3(float fx = 0.0f, float fy = 0.0f, float fz = 0.0f);
///Add two vectors together
Vector3 & operator+(Vector3 & item);
///Subtract the right vector from the left.
Vector3 & operator-(Vector3 & item);
///Increment left vector by the right.
Vector3 & operator+=(Vector3 & item);
///Decrement left vector by the right.
Vector3 & operator-=(Vector3 & item);
///Scale the left vector by the right float.
Vector3 & operator*=(float & scalar);
///Scale the left vector by the right float.
Vector3 & operator*(float & scalar);
///Scale the left vector by the right vector.
Vector3 & operator*=(Vector3 & scalar);
///Scale the left vector by the right vector.
Vector3 & operator*(Vector3 & scalar);
///Divide the left vector by the right vector.
Vector3 & operator/=(Vector3 & scalar);
///Divide the left vector by the right vector.
Vector3 & operator/(Vector3 & scalar);
///Divide the left vector by the right float.
Vector3 & operator/=(float & scalar);
///Divide the left vector by the right float.
Vector3 & operator/(float & scalar);
///Get the length of the current vector.
float length();
///Get the distance between vectors ((Vector3_1 - Vector3_2).length()).
float distance(Vector3 item);
///Get the unit vector of the current vector.
Vector3 unit();
///Get the direction (in radians) between two vectors.
Vector3 direction(Vector3 item);
///Returns the dot product of the two vectors.
float dot(Vector3 item);
///Returns the cross product of the two vectors.
Vector3 cross(Vector3 item);
///Get the current x value.
float x() { return tx; }
///Get the current y value.
float y() { return ty; }
///Get the current z value.
float z() { return tz; }
///Set the current x value.
void x(float newx) { tx = newx; }
///Set the current y value.
void y(float newy) { ty = newy; }
///Set the current z value.
void z(float newz) { tz = newz; }
};
#ifdef Vector3_DEBUG
void OutputVector3(Vector3 vec)
{
std::cout<<"Vector3; x: "<<vec.x()<<"; y: "<<vec.y()<<"; z: "<<vec.z();
}
#endif
#endif//Vector3_h
Vector3.cpp
//Written by Kyle Emmerich, or "Zuka".
//I guess you can have the zlib license or whatever.
#include "Vector3.h"
#include <cmath>
Vector3::Vector3(float fx, float fy, float fz)
{
this->tx = fx;
this->ty = fy;
this->tz = fz;
}
Vector3 & Vector3::operator+=(Vector3 & item)
{
this->tx = this->tx + item.x();
this->ty = this->ty + item.y();
this->tz = this->tz + item.z();
return *this;
}
Vector3 & Vector3::operator-=(Vector3 & item)
{
this->tx = this->tx - item.x();
this->ty = this->ty - item.y();
this->tz = this->tz - item.z();
return *this;
}
Vector3 & Vector3::operator*=(Vector3 & scalar)
{
tx = tx * scalar.x();
ty = ty * scalar.y();
tz = tz * scalar.z();
return *this;
}
Vector3 & Vector3::operator*=(float & scalar)
{
tx = tx * scalar;
ty = ty * scalar;
tz = tz * scalar;
return *this;
}
Vector3 & Vector3::operator/=(Vector3 & scalar)
{
tx = tx / scalar.x();
ty = ty / scalar.y();
tz = tz / scalar.z();
return *this;
}
Vector3 & Vector3::operator/=(float & scalar)
{
tx = tx / scalar;
ty = ty / scalar;
tz = tz / scalar;
return *this;
}
Vector3 & Vector3::operator*(Vector3 & scalar)
{
return Vector3(*this) *= scalar;
}
Vector3 & Vector3::operator*(float & scalar)
{
return Vector3(*this) *= scalar;
}
Vector3 & Vector3::operator/(Vector3 & scalar)
{
return Vector3(*this) /= scalar;
}
Vector3 & Vector3::operator/(float & scalar)
{
return Vector3(*this) /= scalar;
}
Vector3 & Vector3::operator+(Vector3 & item)
{
return Vector3(*this) += item;
}
Vector3 & Vector3::operator-(Vector3 & item)
{
return Vector3(*this) += item;
}
float Vector3::length()
{
return std::abs(std::sqrt(tx * tx + ty * ty + tz * tz));
}
float Vector3::distance(Vector3 item)
{
return Vector3(*this - item).length();
}
Vector3 Vector3::unit()
{
Vector3 result = *this;
float length = result.length();
result = result / length;
return result;
}
Vector3 Vector3::direction(Vector3 item)
{
return Vector3(*this - item).unit();
}
float Vector3::dot(Vector3 item)
{
return (tx * item.x() + ty * item.y() + tz * item.z());
}
Vector3 Vector3::cross(Vector3 item)
{
return Vector3(ty * item.z() - tz * item.y(), tz * item.x - tx * item.z(), tx * item.y() - ty * item.x());
}
I don't really care what you do with it as long as you leave my name on it.
I'm currently using it in my new particle system. I really don't like the GDK's, it doesn't really suit my purposes well, and it doesn't collide with 3D objects, does it?
I'll then, later, build a full-scale physics engine off of it. Full Newtonian physics, with point gravity sources and ragdolls and everything. I think I might even use Hugo Elias's inverse-kinematics article, so I can make the ragdolls walk on their own.