I felt like sharing this, it's my 2d vector class, useful for game physic systems and rather versatile (tailored to my needs/style of coding). I'm fairly sure all the maths is in order though I haven't touched it for a while. Feel free to pick it apart and make suggestions, or take ideas from it. It can easily be extended to 3d by adding the z variable and perhaps a function for getting the cross product of two vectors.
/*
* 2D vector class
*
*
* Created by Sam Zeid on 6/11/09.
* Copyright 2009 *. All rights reserved.
*
*/
#ifndef _VECTOR2_H
#define _VECTOR2_H
#include <math.h>
template<typename T>
class Vector2
{
public:
//the cartesian coordinates that are stored for a vector are not declared private.
//should the user want to assign new values to the vector,
//they can do so by accessing its members directly or using
//the operator ()
T x,y;
//if a vector is declared by default its values are 0
Vector2():x(0),y(0){}
//the user can define the vector coordinates upon creation
Vector2(T X, T Y):x(X),y(Y){}
//S P E C I A L F U N C T I O N S A N D O P E R A T O R S
//the () operator sets values of the vector
void operator()(const T X, const T Y)
{
x=X;y=Y;
}
//magnitude returns the vectors length
T magnitude() const
{
return sqrt(pow(x,2)+pow(y,2));
}
//get the dot product of two vectors as a scalar value
T dot(Vector2<T> rightHandSide) const
{
return ((x*rightHandSide.x)+(y*rightHandSide.y));
}
const Vector2<T> normalise()
{
T n=magnitude();
if(n==0)return *this;
x/=n;y/=n;
return Vector2<T>(x, y);
}
//C O M P A R I T I V E O P E R A T O R S
//the == operator tests if a vector is equal to another
bool operator==(const Vector2<T> &rightHandSide)
{
return (x==rightHandSide.x && y==rightHandSide.y);
}
//the != operator tests if a vector is not equal to another
bool operator!=(const Vector2<T> &rightHandSide)
{
return (x!=rightHandSide.x || y!=rightHandSide.y);
}
//A R I T H M E T I C O P E R A T O R S
//the = operator makes a vector equal to another
const Vector2<T> &operator=(const Vector2<T> &rightHandSide)
{
x=rightHandSide.x;y=rightHandSide.y;
return *this; //the resulting vector is returned
}
//the - operator may return the negative of a vector
const Vector2<T> operator-(void)const
{
return Vector2<T>(-x, -y);
}
//the - operator may also return the subtraction of two vectors
const Vector2<T> operator-(Vector2<T> &rightHandSide)const
{
return Vector2<T>(x-rightHandSide.x, y-rightHandSide.y);
}
//the + operator returns the addition of two vectors
const Vector2<T> operator+(Vector2<T> &rightHandSide)const
{
return Vector2<T>(x+rightHandSide.x, y+rightHandSide.y);
}
//the * operator returns the uniform scale of a vector
const Vector2<T> operator*(T s)const
{
return Vector2<T>(x*s, y*s);
}
//the / operator returns the uniform scale of a vector
const Vector2<T> operator/(T s)const
{
return Vector2<T>(x/s, y/s);
}
//the += operator adds one vector to another
const Vector2<T> &operator+=(const Vector2<T> &rightHandSide)
{
x+=rightHandSide.x;y+=rightHandSide.y;
return *this; //the resulting vector is returned
}
//the -= operator subtracts one vector from another
const Vector2<T> &operator-=(const Vector2<T> &rightHandSide)
{
x-=rightHandSide.x;y-=rightHandSide.y;
return *this; //the resulting vector is returned
}
//the *= operator scales a vector by a uniform value
const Vector2<T> &operator*=(T s)
{
x*=s;y*=s;
return *this; //the resulting vector is returned
}
//the /= operator scales a vector by a uniform value
const Vector2<T> &operator/=(T s)
{
x/=s;y/=s;
return *this; //the resulting vector is returned
}
};
#endif
Last edited by zeid on Fri Feb 05, 2010 2:27 pm, edited 1 time in total.
@GroundUpEngine - Thanks, I really should get back into my art... Maybe I will just do art assets for gamejam this year. Yeah this was a follow-up on a 3d vector class I created for a game I did a while ago, when I looked back on the class and how I had hacked a lot of code around it I decided to write something more suited to how I code and my needs for the future.
@ismetteren - oops, thats because I created the class in Xcode (iPhone with c++ programming these days), it creates that text automatically and I didn't take it out as I will undoubtedly use it in my projects. I'm fine with people using this code for learning purposes and even copying parts of it so long as they understand what it's doing. At the same time I'm not going to stop people from just shoving it in their projects, it's just more helpful to them to learn from it, realistically they have to look over it anyway in order to use it.
p.s I should have made this post first, so that my question post in 'Programming Discussion' didn't get overlooked.
Thought I'd throw this method in. Should be pretty easy to gut to use the template. I used this for homing missiles in Ace of Space when I didn't want the homing property to be too strong.