Geeks With Blogs

News

Code.Blog Code Talk for the Game Developer

There are numerous useful types available in XNA that will significantly help you design and build your next game.  Here are two of the most useful (in my opinion) and how you can use them in your game (I will focus on mostly 2-dimensional uses).

Vectors

Vectors are amazing!  They are seriously, probably one of the coolest types on the block, especially when taking into account that they are used for almost everything in games.  The most common forms of Vectors used in XNA are the Vector2 and the Vector3, which are defined as:

Vector2(float X, float Y)
Vector3(float X, float Y, float Z)

Also useful, is Vector4 which is similar to a Vector3, but adds a float W parameter on the end.  Vector's are obviously useful anytime you need a small array of floats for any reason (movement speeds, health of 2-4 characters, etc...)  The most practical and useful application of Vectors however is all the mathematical properties they work with.  They are awesome for a quick matrix v. vector multiplication (primarily used in 3D), and are great in 2D for specifying a movement direction, position, and even a scale amount!

Rectangle

Rectangle's are an interesting type.  It took me years before I understood how to effectively utilize the Rectangle type.  There are lots of functions that take Rectangle's as parameters and the sooner you use them, the better your code will look.  There is a default empty definition for a Rectangle, but the most useful in my mind is:

Rectangle(int X, int Y, int Width, int Height)

The X,Y coordinates are the X- and Y-coordinates for the left and the top edges of the rectangle, respectively.  This essentially defines the upper left corner of the rectangle.  The Width and Height obviously, determine the width and height.  Now that you have Rectangle's being used, you can use them to store global variables useful for determining screen size, specifying the source rectangle from a content file, or useful as a bounding box for collision detection.

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: Learn XNA: Useful Types