Saturday, January 21, 2012

Bring on the Vectors...

Hey everybody. Today I finished up building a simple 2d Vector class. I will be using this class not only in this project, but also many other projects in the future. I ran it through several tests, making sure it didn't break. This is the class that I came up with:



/* 
 * File:   Vector2.h
 * Author: Eric
 *
 * Created on January 20, 2012, 8:33 AM
 */

#ifndef VECTOR2_H
#define VECTOR2_H

#include 
#include "math.h"

class Vector2 {
private:
    float x, y;
    
public:
    Vector2():x(0.0f), y(0.0f){};
    Vector2(float x, float y):x(x),y(y){};
        
    float getX(){return x;};
    float getY(){return y;};
    void setX(float a){x=a;};
    void setY(float a){y=a;};
    
    float length(){return sqrt(x*x + y*y);};
    float mag2(){return x*x + y*y;};
    void normalize(){
        float Length = length(); 
        if(Length != 0){
                x = x/Length;
                y = y/Length;
        }  
    };
    
    Vector2 normalized(){
        float length = this->length();
        if(length != 0){
            return Vector2(x/length, y/length);
        } 
        return Vector2();
    };
    
    bool operator==(const Vector2 a){return (x == a.x) && (y == a.y);};
    
    Vector2 operator+(const Vector2 a){return Vector2(x + a.x, y + a.y);};
    Vector2 operator-(const Vector2 a){return Vector2(x - a.x, y - a.y);};
    Vector2 operator-(){return Vector2(-x,-y);};
    
    template Vector2 operator+(const T a){return Vector2(a + x, a + y);};
    template Vector2 operator-(const T a){return Vector2(a - x, a - y);};
    template Vector2 operator*(const T a){return Vector2(a * x, a * y);};
    template Vector2 operator/(const T a){return Vector2(x/a, y/a);};
    
    friend std::ostream& operator<< (std::ostream &out, const Vector2 &c){ return out << "(" << c.x << "," << c.y << ")"; };
    
    float dot(const Vector2 a){return x * a.x + y * a.y;};
    
    static const Vector2 zero;
    static const Vector2 unit_x;
    static const Vector2 unit_y;
};

 const Vector2 Vector2::zero = Vector2();
 const Vector2 Vector2::unit_x = Vector2(1.0f, 0.0f); 
 const Vector2 Vector2::unit_y = Vector2(0.0f, 1.0f);

#endif /* VECTOR2_H */



I am quite content with this vector class. I should do everything I need and more. Here are some useful features:

Mathematical Operators We should be able to do the most common operations on this vectors (add, subtract, negate, multiply by any constant, dot product). Also, there are functions to normalize the current vector, or return a normalized copy of the vector.


Static Unit Vectors included are some pre-defined vectors; the Zero Vector, unit X vector, and unit Y vector. 


Simplicity This vector class is simple and fits into one header file. So including it in a project is as simple as coping the Vector2.h file into your header file directory.

Display included is an override for the stream operator (<<), making printing a vector a snap. This will be very useful in debugging.

Friday, January 20, 2012

Welcome!

Hello World! My name is Eric Hurst. Programming has always been a hobby of mine. I have explored deeply into the worlds of C++, Java, and PHP. Ever since my days of BASIC, I knew that Programming was for me. This development journal will be a record of my trials, journeys, and hopefully success of my newest programming ventures. Development journals from others have always been an inspiration to me, and I have always dreamed of having my own. I hope others will also be able to benefit from my contributions as well.

 Many times have I attempted to create the next-gen game engine, but lost interest or locked myself down in a system that was going nowhere. Every time it would get better, but I never completed a viable game engine. This time around I hope for much better, with more reasonable goals and specific plans. I plan on making a 2D RTS game (name pending) with a different resource/combat system. Instead of a global resource pool, resources will be location specific. Also, game play will be more Macro-management, rather than micromanaging, allowing the player to focus on strategy, rather than administration.

More is on the way. Comments are always welcome, whether they be mistakes in code, or general tips.