Guide to Game Development/Theory/Game logic/Creating a Vector3 class

This is an outline of what a Vector3 class could look like, written in C++:


class Vector3
	float x, y, z;


	static const Vector3 Null;
	static const Vector3 Up;
	static const Vector3 Down;
	static const Vector3 Left;
	static const Vector3 Right;
	static const Vector3 Forwards;
	static const Vector3 Backwards;


	Vector3(float x, float y, float z);

	void ToArray(float* coords);           //Fills the passed in array with the vector information. [0] = x, [1] = y, [2] = z
	float Length();                        //Finds the magnitude of the vector
	float LengthSqr();                     //Uses less resources, can be used for comparisons, finds the square of the length

	//Operator overloading

	Vector3 operator+(Vector3 v);
	Vector3 operator-(Vector3 v);
	void operator+=(Vector3 v);
	void operator-=(Vector3 v);
	Vector3 operator*(int s);
	Vector3 operator/(int s);
	void operator*=(int s);
	void operator/=(int s);

	//Dot product, angle between and cross product

	float Dot(Vector3 v);	                //Dot product with self and other vector
	float Dot(Vector3 a, Vector3 b);	//Dot product with two vectors
	float FindAngle(Vector3 v);	        //Find the angle between between self another vector
	float FindAngle(Vector3 a, Vector3 b);	//Find the angle between two vectors
	Vector3 Cross(Vector3 a, Vector3 b);	//Cross product with two vectors
	Vector3 Cross(Vector3 v);	        //Cross product with self and other vector


#include <cmath>
#include "Vector3.h"

const Vector3 Vector3::Null;
const Vector3 Vector3::Up(0,1,0);
const Vector3 Vector3::Down(0,-1,0);
const Vector3 Vector3::Left(1,0,0);
const Vector3 Vector3::Right(-1,0,0);
const Vector3 Vector3::Forwards(0,0,1);
const Vector3 Vector3::Backwards(0,0,-1);



Vector3::Vector3(float x, float y, float z){
	Vector3::x = x;
	Vector3::y = y;
	Vector3::z = z;

void Vector3::ToArray(float* coords){
	coords[0] = x;
	coords[1] = y;
	coords[2] = z;
float Vector3::Length(){
	return sqrtf(pow(x,2)+pow(y,2)+pow(z,2));
float Vector3::LengthSqr(){ 
	return pow(x,2)+pow(y,2)+pow(z,2);

Vector3 Vector3::operator+(Vector3 v){
	Vector3 ret(x + v.x, y + v.y, z + v.z);
	return ret;
Vector3 Vector3::operator-(Vector3 v){
	Vector3 ret(x - v.x, y - v.y, z - v.z);
	return ret;
void Vector3::operator+=(Vector3 v){
	*this = *this + v;  //Calling it's own + overload
void Vector3::operator-=(Vector3 v){
	*this = *this - v;  //Calling it's own - overload
Vector3 Vector3::operator*(int s){
	Vector3 ret(x*s, y*s, z*s);
	return ret;
Vector3 Vector3::operator/(int s){
	Vector3 ret(x/s, y/s, z/s);
	return ret;
void Vector3::operator*=(int s){
	*this = *this * s;  //Calling it's own * overload
void Vector3::operator/=(int s){
	*this = *this / s;  //Calling it's own / overload

float Vector3::Dot(Vector3 v){
	return x*v.x + y+v.y + z*v.z;
float Vector3::Dot(Vector3 a, Vector3 b){
	return a.x*b.x + a.y+b.y + a.z*b.z;
float Vector3::FindAngle(Vector3 v){
	return (this->Dot(v))/(float)(this->Length() * v.Length());
float Vector3::FindAngle(Vector3 a,Vector3 b){
	return (a.Dot(b))/(float)(a.Length() * b.Length());
Vector3 Vector3::Cross(Vector3 a, Vector3 b){
	Vector3 c(
	return c;
Vector3 Vector3::Cross(Vector3 v){
	Vector3 c(
	return c;

To do:

  • Add interactions with matrices