Part I. Basic Maths for Game Design



Similar documents
Figure 1.1 Vector A and Vector F

1.3. DOT PRODUCT If θ is the angle (between 0 and π) between two non-zero vectors u and v,

PHYSICS 151 Notes for Online Lecture #6

Section 1.1. Introduction to R n

A vector is a directed line segment used to represent a vector quantity.

13.4 THE CROSS PRODUCT

3. KINEMATICS IN TWO DIMENSIONS; VECTORS.

Vectors 2. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996.

Adding vectors We can do arithmetic with vectors. We ll start with vector addition and related operations. Suppose you have two vectors

Introduction and Mathematical Concepts

28 CHAPTER 1. VECTORS AND THE GEOMETRY OF SPACE. v x. u y v z u z v y u y u z. v y v z

Solving Simultaneous Equations and Matrices

EDEXCEL NATIONAL CERTIFICATE/DIPLOMA MECHANICAL PRINCIPLES AND APPLICATIONS NQF LEVEL 3 OUTCOME 1 - LOADING SYSTEMS

discuss how to describe points, lines and planes in 3 space.

Problem Set 5 Due: In class Thursday, Oct. 18 Late papers will be accepted until 1:00 PM Friday.

Vectors VECTOR PRODUCT. Graham S McDonald. A Tutorial Module for learning about the vector product of two vectors. Table of contents Begin Tutorial

Lab 2: Vector Analysis

Vector Math Computer Graphics Scott D. Anderson

GeoGebra. 10 lessons. Gerrit Stols

Example SECTION X-AXIS - the horizontal number line. Y-AXIS - the vertical number line ORIGIN - the point where the x-axis and y-axis cross

Unified Lecture # 4 Vectors

Geometry of Vectors. 1 Cartesian Coordinates. Carlo Tomasi

5.3 The Cross Product in R 3

South Carolina College- and Career-Ready (SCCCR) Pre-Calculus

Difference between a vector and a scalar quantity. N or 90 o. S or 270 o

Lecture L3 - Vectors, Matrices and Coordinate Transformations

Linear algebra and the geometry of quadratic equations. Similarity transformations and orthogonal matrices

Solutions to old Exam 1 problems

Definition: A vector is a directed line segment that has and. Each vector has an initial point and a terminal point.

Chapter 3 Vectors. m = m1 + m2 = 3 kg + 4 kg = 7 kg (3.1)

3. INNER PRODUCT SPACES

Equations Involving Lines and Planes Standard equations for lines in space

Recall that two vectors in are perpendicular or orthogonal provided that their dot

C relative to O being abc,, respectively, then b a c.

Physics 235 Chapter 1. Chapter 1 Matrices, Vectors, and Vector Calculus

One advantage of this algebraic approach is that we can write down

9 Multiplication of Vectors: The Scalar or Dot Product

Vectors and Scalars. AP Physics B

Bedford, Fowler: Statics. Chapter 4: System of Forces and Moments, Examples via TK Solver

Lectures notes on orthogonal matrices (with exercises) Linear Algebra II - Spring 2004 by D. Klain

LINES AND PLANES CHRIS JOHNSON

VECTOR ALGEBRA A quantity that has magnitude as well as direction is called a vector. is given by a and is represented by a.

Gerrit Stols

BALTIC OLYMPIAD IN INFORMATICS Stockholm, April 18-22, 2009 Page 1 of?? ENG rectangle. Rectangle

ex) What is the component form of the vector shown in the picture above?

9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

11.1. Objectives. Component Form of a Vector. Component Form of a Vector. Component Form of a Vector. Vectors and the Geometry of Space

Name DATE Per TEST REVIEW. 2. A picture that shows how two variables are related is called a.

FURTHER VECTORS (MEI)

Lecture 3: Coordinate Systems and Transformations

DEFINITION A complex number is a matrix of the form. x y. , y x

Chapter 3B - Vectors. A PowerPoint Presentation by Paul E. Tippens, Professor of Physics Southern Polytechnic State University

Section V.2: Magnitudes, Directions, and Components of Vectors

Section 9.1 Vectors in Two Dimensions

Lecture 14: Section 3.3

Chapter 11 Equilibrium

Biggar High School Mathematics Department. National 5 Learning Intentions & Success Criteria: Assessing My Progress

L 2 : x = s + 1, y = s, z = 4s Suppose that C has coordinates (x, y, z). Then from the vector equality AC = BD, one has

13 MATH FACTS a = The elements of a vector have a graphical interpretation, which is particularly easy to see in two or three dimensions.

Vector Algebra CHAPTER 13. Ü13.1. Basic Concepts

Exam 1 Sample Question SOLUTIONS. y = 2x

Vector has a magnitude and a direction. Scalar has a magnitude

AP Physics - Vector Algrebra Tutorial

Section 1: How will you be tested? This section will give you information about the different types of examination papers that are available.

Physics 590 Homework, Week 6 Week 6, Homework 1

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Geometric Transformations

Examples of Physical Quantities

GCE Mathematics (6360) Further Pure unit 4 (MFP4) Textbook

Georgia Standards of Excellence Frameworks. Mathematics. GSE Pre-Calculus Unit 7: Vectors

v 1 v 3 u v = (( 1)4 (3)2, [1(4) ( 2)2], 1(3) ( 2)( 1)) = ( 10, 8, 1) (d) u (v w) = (u w)v (u v)w (Relationship between dot and cross product)

Section 10.4 Vectors

Concepts in Calculus III

Eigenvalues and Eigenvectors

F B = ilbsin(f), L x B because we take current i to be a positive quantity. The force FB. L and. B as shown in the Figure below.

Algebra and Geometry Review (61 topics, no due date)

Module 8 Lesson 4: Applications of Vectors

Functions. MATH 160, Precalculus. J. Robert Buchanan. Fall Department of Mathematics. J. Robert Buchanan Functions

Mathematics 205 HWK 6 Solutions Section 13.3 p627. Note: Remember that boldface is being used here, rather than overhead arrows, to indicate vectors.

6. Vectors Scott Surgent (surgent@asu.edu)

Linear Algebra Notes

Cross product and determinants (Sect. 12.4) Two main ways to introduce the cross product

a.) Write the line 2x - 4y = 9 into slope intercept form b.) Find the slope of the line parallel to part a

Vector Algebra. Addition: (A + B) + C = A + (B + C) (associative) Subtraction: A B = A + (-B)

Vectors Math 122 Calculus III D Joyce, Fall 2012

Useful Mathematical Symbols

2 Session Two - Complex Numbers and Vectors

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

Recall the basic property of the transpose (for any A): v A t Aw = v w, v, w R n.

sin(θ) = opp hyp cos(θ) = adj hyp tan(θ) = opp adj

α = u v. In other words, Orthogonal Projection

Review A: Vector Analysis

1.5 Equations of Lines and Planes in 3-D

Math 241, Exam 1 Information.

Math 1050 Khan Academy Extra Credit Algebra Assignment

Essential Mathematics for Computer Graphics fast

Solutions to Exercises, Section 5.1

Chapter 1: Statics. A) Newtonian Mechanics B) Relativistic Mechanics

MATHEMATICS FOR ENGINEERING BASIC ALGEBRA

Content. Chapter 4 Functions Basic concepts on real functions 62. Credits 11

Transcription:

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 ) ) ;