2d Vector class

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

2d Vector class

Post by zeid »

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.

Code: Select all

/*
*  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.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: 2d Vector class

Post by GroundUpEngine »

This is great, Nice work!
I made a simple Vec3 class with a few operators a while back, but I never took any time to improve it. Thanks!

Edit: I checked out your art thread, amazing stuff man! :worship:
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: 2d Vector class

Post by ismetteren »

Im not that much into licencing and stuff like that, but dosent the copyright statement in the beginning prevent us from copying it?
Image ImageImage Image
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: 2d Vector class

Post by zeid »

@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 - :lol: 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. :(
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: 2d Vector class

Post by Bakkon »

Very nice. I like the template usage. My vector classes usually just get floats dumped into them.

Code: Select all

Vector Vector::LinearInterp(const Vector& v1, const Vector& v2, const float& weight)
{
	Vector vector;
	
	// weight is out of range, return a vector of (0.0, 0.0)
	if(weight < 0 || weight > 1)
		return vector;
		
	vector.X = v1.X * (1.0f - weight) + v2.X * weight;
	vector.Y = v1.Y * (1.0f - weight) + v2.Y * weight;
	
	return vector;
}
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. ;)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: 2d Vector class

Post by K-Bal »

Zeid, you should use init lists in your constructor ;)
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: 2d Vector class

Post by zeid »

why use a linked list in a vector2 'maths' class?
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: 2d Vector class

Post by K-Bal »

K-Bal wrote:init lists
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: 2d Vector class

Post by zeid »

oh,

Code: Select all

List():m_root(NULL){}
as in? :P

I shall make changes to node class
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: 2d Vector class

Post by K-Bal »

Yep.
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: 2d Vector class

Post by zeid »

Changes have now been made, thanks for the suggestion.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
Post Reply