Part I Basic Maths for Game Design 1
Chapter 1 Basic Vector Algebra 1.1 What's a vector? Why do you need it? A vector is a mathematical object used to represent some magnitudes. For example, temperature is represented by a number: 5 C, but position is represented by two numbers in a plane. Also, to express velocity I need the speed (5 m/s) but also direction. In fact, a vector is a set of numbers which give me information about the length and direction of the magnitude. In game mechanics they're really important to repressent physical magnitudes. I will write vectors as: v = (x, y, z) Note the sign to denote that v is a vector and not a number. The x, y, z quantities represent the magnitude of the vector in every axis (or the coordinates). I'm going to focus, from now, in 2D vectors (z = 0) because they're easy to draw in a plane. For example, the vector u = (2, 3) is represented with 2 units in axis X and 3 units in axis Y. The vector w = (3, 0) is represented with 3 units in axis X and 0 in axis Y. It only has vertical component. 2
CHAPTER 1. BASIC VECTOR ALGEBRA 3 Figure 1.1: The vector v = (2, 3). Note that is represented as an arrow from B to A. If it were from A to B, it would be ( 2, 3) 1.2 Operations with vectors 1.2.1 Sum We can sum vectors. If we have two vectors, u = (a, b) and v = (x, y) then u + v = (a + x, b + y) For example, if u = (1, 2) and v = ( 3, 1) then u + v = (1 3, 2 + 1) = ( 2, 3).
CHAPTER 1. BASIC VECTOR ALGEBRA 4 1.2.2 Product by scalar Figure 1.2: Graphical sum of u and v A scalar is a number in the tradional sense. If you have a number k and a vector v, then k v = (k x, k y) For example, u v = u + ( 1) v = (1, 2) + (3, 1) = (4, 1). Exercise. If we have two vectors, u = (a, b) and v = (x, y), and a scalar k, show that k ( u + v) = k u + k v
CHAPTER 1. BASIC VECTOR ALGEBRA 5 1.2.3 Scalar product If we have u = (a, b) and v = (x, y), we dene the scalar product as u v = ax + by Some considerations: ˆ ˆ Note that the result from this operation is a scalar, not a vector. The dot between the vector is really important, as you will see later. 1.2.4 Vectorial product In this case, I have to use 3D vectors. This operation cannot be done in 2D, since it returns a vector perpendicular to the input vectors. Then, if we have u = (a, b, c) and v = (x, y, z), u v = det Some considerations: i j k a b c x y z = (bz cy, az cx, ay bx) ˆ i, j,k are the base vectors, or versors. They're ˆ i = (1, 0, 0) j = (0, 1, 0) k = (0, 0, 1) det means the matrix determintant. Since I've not introduced matrices yet, I also wrote the nal formula. ˆ The result from this operation is another vector. 1.2.5 Dyadic product The dyadic product of two vectors u = (a, b) and v = (x, y) is dened as ( ) ax bx u v = ay by which is a 2x2 matrix. Note we haven't written the dot between vectors. 1.3 Properties of vector. Applications of operations Now we know what's a vector and how to operate with it. Now we're going to focus in its practical properties and how to calculate them.
CHAPTER 1. BASIC VECTOR ALGEBRA 6 1.3.1 Length or modulus tt says us the magnitude of the vector. If the vector is velocity, its modulus is speed (3 m/s, for example). As you can see, it is really useful, since when I code I want to control the speed and the direction of the velocity, but I don't want to deal with the vector components. The modulus of the vector can be dened as v = v v If we're working in 2D, then v v = x 2 + y 2 and v = x 2 + y 2. If v = 1, we say the vector is normalized. We can normalize any vector calculating 1 v v 1.3.2 Angle Now, I want to know the angle α between two vectors u and v. This angle is given by u v cos α = u v This is valid in 2D and also in 3D. That also give us an alternate denition for scalar product, useful if you know the modulus and the angle: u v = u v cos α Let's say that u = (1, 0) is an horizontal vector in 2D. Then, the angle α measured from the X axis is cos α = x v 1.3.3 Vector between two points Let's say I have two points A = (a, b) and B = (x, y) and I want to join them with a vector. What I can do? Simply write A as A and B as B. Now see the image of sum of vector. It is clear that So our vector is v = (x a, y b). B A = v 1.3.4 Writing a vector from its angle and modulus This is a very common situation. I want a velocity of 5 m/s oriented 20 º from X axis, for example. Let's call the vector v = (x, y). What I have to do to nd x and y? Using simple trigonometry we can see that v = ( v cos α, v sin α) if α is the angle measured from X axis.
CHAPTER 1. BASIC VECTOR ALGEBRA 7 Figure 1.3: Vector components. Note that a vector is the sum of its components. Applying trigonometry to our triangle give us the vector x and y Exercise Show that if we measure the angle from Y axis, then v = ( v sin α, v cos α) In 3D, it can be more long, but the method is the same. We have to deal with two angles, φ and α. First one is angle from the vertical and second one angle from X axis. Then, I project the vector in two components: one in the vertical Z axis, and another one in the plane. After that, I do the above 2D projection to obtain: v = ( v cos α sin φ, v sin α sin φ, v cos φ) 1.3.5 Finding perpendicular vectors Let's say I have a 2D vector u = (a, b) and I want to nd a vector perpendicular to it. If two vectors are perpendicular, then the angle between them is 90º.
CHAPTER 1. BASIC VECTOR ALGEBRA 8 Using that: u v = u v cos α And using that cos 90º = 0, the two vector are perpendicular (i.e u v) if and only if u v. That give us a powerful tool to nd a perpendicular vector. Let's say we have u = (2, 3) and v = (x, y). Let's choose x = 1 (any other value is valid), and now make the scalar product u v = 2 + 3y = 0 y = 3 2 So the vector v = ( 1, 2) 3 is perpendicular to u. Now, we can calculate v = 1 + 9 4 = ( 13 2 to nd the normalized vector v = 2 13 1, 3 1). If we multiply this vector with a scalar k, we will have a vector perpendicular to u with length k. In 3D, I have to choose the values of x and y instead of only x. And, if you want a vector perpendicular to other two vectors, you simply have to calculate the vectorial product of that two vectors. 1.4 Examples in Game Programming 1.4.1 Screen and absolute coordinates Imagine you code on a game engine without Camera or Viewport class. Or you may want to build a custom one. Then, you have a big background image or map, bigger than your screen. We have two coordinate systems: screen coordinate system, with (0,0) in the bottom left corner of our viewport rectangle, and absolute coordinate system, with (0,0) in bottom left corner of you map. Since your camera can be moved, the bottom left corner of the viewport rectangle has (x, y) coordinates in the absolute system. This can be a problem. For example, LibGDX always draw sprites using the screen coordinates, so the objects will be moved with the camera. Another problem: even if the objects are drawn in absolute coordinates, the Mouse.getX functions always return the position relative to screen coordinates. How to make a change between coordinates? Simply look again at the image placed at vector sum section. After that, take a look to this one, which represents our example:
CHAPTER 1. BASIC VECTOR ALGEBRA 9 They look pretty similar. In fact, v is our viewport position, a is the object position in absolute coordinates and s the object position in screen coordinates. Now, by comparation of the images, you can see that a = v + s Surprise! We have here a formula that solves the problem. Suppose I know the absolute coordinates a of the object and I want to obtain its s coordinates to compare with mouse click. Then, s = a v. As you can see, we can solve a dicult problem easily using basic vector algebra. 1.4.2 Shooting! Let's say I'm making an arcade 2D shooter game. I want a cool trap to our spaceship. Something like this:
CHAPTER 1. BASIC VECTOR ALGEBRA 10 Five dangerous energy balls in a pentagon. Well, you can see this is a common situation in this type of games. How to move this things correctly? First, we have 5 shoots, separated by the same angle, which is α = 360/5 = 72. The 5 points will be at the same distance of a circunference of radious r. Then, the object j will be a vector with modulus r and angle 72j from X axis. If we want them to get closer, we only have to change r, for example using
CHAPTER 1. BASIC VECTOR ALGEBRA 11 r = 0.05 f ; and recalculating the vector from modulus and angle as we've seen above: p o s i t i o n [ j ] = new Vector2 ( r *Math. cos (72 * j ), r *Math. s i n (72 * j ) ) ;