20100104

Vectors in Game Design

                When we first start creating 2D video games, we most often manage the variable X and Y for the object’s location on their own.  While all physics can still be applied this way, it makes it more difficult, and usually harder to enhance your game over time.  The modern world uses Vectors and for good reason.


                A Vector is simple, it holds 2 or more numbers.  For 2D sake, (since 2D has less to explain) we will use a Vector2.  A Vector2 is made up of 2 variables, X and Y.  The X and Y are used in more than just location; they are used for movement, dimensions and ranges.  For instance, perhaps your game needs to know the screen size.  At the beginning you create a Vector2 called Screen.  At any time, your program can ask for Screen.X, which is the width of your screen or Screen.Y which is the height.


//Settings
Vector2 Screen;
Screen.X = 800; Screen.Y = 600;


                Each of the objects in your game also carry a location vector, which defines where they are.  At the drawing cycle of each game loop, it will find each object, and draw it based for its location. 


//GameObject
{
                Vector2 Location;
}


                However, each of those are just setting a variable to identify a location.  Screen defines the Bottom Right Corner of the screen (0,0 being the Top Left Corner) and Location giving exact coordinates for anything that needs to be drawn.  Another perspective is Movement.  Movement, or momentum is a highly valuable feature.  My previous article discusses the use of momentum when applied to a single variable. 


                Imagine that if you pressed UP, it simply subtracted your Force variable (engine torque) to your Momentum.Y.  (By subtracting from the Location.Y, you move the object closer to the top of the screen)  If you pressed down, it added your Force to the Momentum.Y.    This takes care of your momentum for speeding up and slowing down, but now I’ll throw another variable in, Handling.   Handling will define how affective your steering is.  When you move left or right, it will subtract or add your Handling to your Momentum.X. 


(NOTE: “A += 5” is the same as saying “A = A + 5” it is a short cut in most languages used to reduce the typing for this common use.)


                OK, now given past knowledge, we might just say location.X += momentum.X and Location.Y += Momentum.Y.  With Vectors in programming, there are certain overridden operators, like + and – and * and \ and =.  This means that I can say Location += Momentum, and it will Add the momentum to my location, taking care of both X and Y.  If you are using DirectX, XNA or other game engines, this is probably already taken care of,  but if you are writing your own classes, you will either need to add your own methods, or you will need to learn about Overriding Operators and add methods for +, +=, -, -=, etc…


//GameObject
{
                Vector2 Location;
                Vector2 Momentum;
                Void Move()
                {
                                Location += Momentum;
                }
}


                In addition to Adding Vectors, you can also Multiply or Divide them by a single number.  Like Friction.  Such as, Momentum += 0.85. 


//GameObject
{
                Float Friction = 0.85;
                Vector2 Location;
                Vector2 Momentum;
                Void Move()
                {
                                Momentum *= Friction;
                                Location += Momentum;
                }
}


                I leave this article off with a request for you to go try it.   The only way to gain experience and timing knowledge is to practice this. 

No comments: