Basic Math in 2D Game Development

Slides:



Advertisements
Similar presentations
10.2 Vectors and Vector Value Functions
Advertisements

VECTORS IN A PLANE Pre-Calculus Section 6.3.
Vector Calculus Mengxia Zhu Fall Objective Review vector arithmetic Distinguish points and vectors Relate geometric concepts to their algebraic.
55: The Vector Equation of a Plane
CS 4731: Computer Graphics Lecture 6: Points, Scalars and Vectors Emmanuel Agu.
ME 221 Statics (Angel). ME221Lecture 22 Vectors; Vector Addition Define scalars and vectors Vector addition, scalar multiplication 2-D.
CSCE 590E Spring 2007 Basic Math By Jijun Tang. Applied Trigonometry Trigonometric functions  Defined using right triangle  x y h.
Vectors Sections 6.6.
Basic Math Vectors and Scalars Addition/Subtraction of Vectors Unit Vectors Dot Product.
Chapter 3 Vectors in Physics.
Chapter 2: Vectors Ian Parberry University of North Texas Fletcher Dunn Valve Software 3D Math Primer for Graphics and Game Development.
Lecture 1eee3401 Chapter 2. Vector Analysis 2-2, 2-3, Vector Algebra (pp ) Scalar: has only magnitude (time, mass, distance) A,B Vector: has both.
Vectors Sections 6.6. Objectives Rewrite a vector in rectangular coordinates (in terms of i and j) given the initial and terminal points of the vector.
Physics 106: Mechanics Lecture 05 Wenda Cao NJIT Physics Department.
Scalar and Vector Fields
Mathematical Fundamentals
UNIVERSITI MALAYSIA PERLIS
Vectors. Definitions Scalar – magnitude only Vector – magnitude and direction I am traveling at 65 mph – speed is a scalar. It has magnitude but no direction.
Vectors. Vectors and Direction Vectors are quantities that have a size and a direction. Vectors are quantities that have a size and a direction. A quantity.
7.1 Scalars and vectors Scalar: a quantity specified by its magnitude, for example: temperature, time, mass, and density Chapter 7 Vector algebra Vector:
Chapter 1 - Vector Analysis. Scalars and Vectors Scalar Fields (temperature) Vector Fields (gravitational, magnetic) Vector Algebra.
Vectors and Vector Multiplication. Vector quantities are those that have magnitude and direction, such as: Displacement,  x or Velocity, Acceleration,
H.Melikyan/12001 Vectors Dr.Hayk Melikyan Departmen of Mathematics and CS
Review: Analysis vector. VECTOR ANALYSIS 1.1SCALARS AND VECTORS 1.2VECTOR COMPONENTS AND UNIT VECTOR 1.3VECTOR ALGEBRA 1.4POSITION AND DISTANCE VECTOR.
Matthew Christian. About Me Introduction to Linear Algebra Vectors Matrices Quaternions Links.
Presented by: S. K. Pandey PGT Physics K. V. Khandwa Kinematics Vectors.
VECTORS (Ch. 12) Vectors in the plane Definition: A vector v in the Cartesian plane is an ordered pair of real numbers:  a,b . We write v =  a,b  and.
CGDD 4003 THE MATH LECTURE (BOILED DOWN, YET LIGHTLY SALTED)
Chapter 3 Vectors. Vector quantities  Physical quantities that have both numerical and directional properties Mathematical operations of vectors in this.
UW EXTENSION CERTIFICATE PROGRAM IN GAME DEVELOPMENT 2 ND QUARTER: ADVANCED GRAPHICS Math Review.
Copyright © Cengage Learning. All rights reserved. 6.3 Vectors in the Plane.
UW EXTENSION CERTIFICATE PROGRAM IN GAME DEVELOPMENT 2 ND QUARTER: ADVANCED GRAPHICS Advanced Math.
Computer Graphics Mathematical Fundamentals Lecture 10 Taqdees A. Siddiqi
I have to use math? I am out of here… Telerik School Academy Unity 2D Game Development.
Vectors Chapter 2.  Scalars and vectors : A scalar quantity: is a quantity that has magnitude only. Mass, time, speed, distance, pressure, Temperature.
Computational Geometry
Introduction to Seismology
Game Engine Architecture
Lecture Outline Chapter 3 Physics, 4th Edition James S. Walker
Done already for your convenience!
Google APIs and Facebook API
2D Graphics and Animations in Unity 3D
COMPLEX NUMBERS and PHASORS
Review for: Unit 2 – Vectors
3.1 Two Dimensions in Motion and Vectors
Outline Addition and subtraction of vectors Vector decomposition
Chapter 1 Vectors.
Chapter 4 Vectors.
Lecture Outline Chapter 3
Vectors Objective Students will be able to use basic vector operations to solve problems.
Binary, Decimal and Hexadecimal Numbers
Vectors Jeff Chastine.
Chapter 3 Vectors September 17, 2018 Chap 3.
Chapter 3: Vectors.
Lecture 03: Linear Algebra
Copyright © 2014 John Wiley & Sons, Inc. All rights reserved.
Physics 111 Practice Problem Solutions 01 Units, Measurement, Vectors SJ 8th Ed.: Ch , 3.1 – 3.4 Contents: 1-7, 1-9, 1-10, 1-12, 1-15, 1-21* 3-5,
Introduction and Mathematical Concepts
4.3 Vectors.
Lecture Outline Chapter 3 Physics, 4th Edition James S. Walker
Introduction and Mathematical Concepts
Lecture Outline Chapter 3 Physics, 4th Edition James S. Walker
Math review - scalars, vectors, and matrices
Math-7 NOTES What are Two-Step Equations??
Game Programming Algorithms and Techniques
CHAPTER 3 VECTORS NHAA/IMK/UNIMAP.
Mathematics Vectors a b c LabRat Scientific © 2019.
Lecture Outline Chapter 3 Physics, 4th Edition James S. Walker
Math with Vectors I. Math Operations with Vectors.
Presentation transcript:

Basic Math in 2D Game Development I have to use math? I am out of here… Unity 2D Game Development Telerik Academy Plus http://academy.telerik.com

Table of Contents Introduction to Linear Algebra Vectors Quaternions * Table of Contents Introduction to Linear Algebra Vectors What is a Vector? Adding, Subtracting Scalar Multiplication Length, Distance Dot Product Quaternions Other helpful functions (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

But… I thought math is for kids! * Linear Algebra But… I thought math is for kids! (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Linear Algebra Why should I care? * Linear Algebra Why should I care? Linear Algebra is the study of vectors Vectors are really helpful in game development 2D or 3D Used for positions, directions and velocity in Cartesian coordinate system The better you understand linear algebra, the better your games! Deal with it!  *NOTE: images and information taken from: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/ (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

What is a Vector What is a Vector? A vector is just a set of numbers * What is a Vector What is a Vector? A vector is just a set of numbers (2, -3) or (2,5,-1) or (1, 0) Doesn't give much information without context In games – position, direction or velocity It is important to keep track of your units! (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

What is a Vector How to create vectors in Unity with C#? * What is a Vector How to create vectors in Unity with C#? using UnityEngine; … Vector2 position2D = new Vector2(2.5f, 3.8f); // 2D vector Vector2 direction2D = new Vector2(0, 1); // another one Vector2 zero = Vector2.zero; // (0, 0) Vector2 one = Vector2.one; // (1, 1) Vector2 right = Vector2.right; // (1, 0) Vector2 left = -Vector2.right; // (-1, 0) Vector2 up = Vector2.up; // (0, 1) Vector2 down = -Vector2.up; // (0, -1) Vector3 position3D = new Vector3(1, 2, 3); // 3D vector (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Vector Addition How to add vectors? Just add each component * Vector Addition How to add vectors? Just add each component (1, 2, 3) + (3, 4, 5) = (1+3, 2 + 4, 3 + 5) = (4, 6, 8) Commonly used in game's physics Example: Velocity – (1, 3) Gravity – (0, -1) var one = new Vector2(2.5f, 3.8f); var two = new Vector2(-2, 5.2f); var result = one + two; // (0.5, 9) (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Vector Subtraction How to subtract vectors? * Vector Subtraction How to subtract vectors? Just subtract each component (4, 3, 1) - (5, 8, 0) = (4 - 5, 3 - 8, 1 - 0) = (-1, 6, 8) Commonly used when you need to get a vector that points from one position to another Example: Player – (1, 2) Enemy – (4, 3) var one = new Vector2(2.5f, 3.8f); var two = new Vector2(-2, 5.2f); var result = one - two; // (4.5, -1.4) (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Scalar-vector Multiplication * Scalar-vector Multiplication How to multiply a vector by scalar? Scalars are just individual numbers: 5, 6, -3 Just multiply each component by the scalar 0.7 * (10, 20) = (0.7 * 10, 0.7 * 20) = (7, 14) Example: Player's velocity on ground – (5, 0) Player's velocity in water – 0.7 * (5, 0) var velocity = new Vector2(5, 0); var waterFactor = 0.7f; var waterVelocity = velocity * waterFactor; // (3.5, 0) (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Length of a Vector How to find the length (magnitude) of a vector? * Length of a Vector How to find the length (magnitude) of a vector? Written with |V| - length of vector V Calculated with Pythagorean theorem Example – (4, 3) |V| = sqrt(42, 32) = 5 var velocity = new Vector2(4, 3); var length = velocity.magnitude; // 5 // use velocity.sqrMagnitude for comparison (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Distance Between Two Vectors * Distance Between Two Vectors How to find the distance between two vectors? Just subtract the vectors and find the length of the result vector Distance = |(4, 3) – (3,1)| = |(3,1) – (4,3)| = |(1,2)| = sqrt(12+22) = sqrt(5) = 2.2361 var first = new Vector2(1, 2); var second = new Vector2(3, 3); var distance = Vector2.Distance(first, second); // 2.23 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Normalization What is normalization? How to normalize a vector? * Normalization What is normalization? Making a vector's to have length of 1 Commonly used with directions How to normalize a vector? Just divide each component by the vector's length (3, 4) => (3/5, 4/5) = (0.6, 0.8) var position = new Vector2(3, 4); var normalizedVector = position.normalized; (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Dot Product How to calculate dot product of two vectors? * Dot Product How to calculate dot product of two vectors? First multiply the components and then add the results (a1,a2)•(b1,b2) = a1b1 + a2b2 Example: (3,2)•(1,4) = 3*1 + 2*4 = 11 var position = new Vector2(3, 4); var direction = new Vector2(1, 0); var dotProduct = Vector2.Dot(position, direction); (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Dot Product Ok, but why dot product is useful? Example: Solution: * Dot Product Ok, but why dot product is useful? Example: Hero at (3, 0) Guard at (1, 1) facing (1, 1) direction and has 180º field of view Is the guard seeing our hero? Solution: Find the vector between the two: V = H – G = (2, -1) Find the dot product between V and D D•V = (1, 1)•(2, -1) = 2 – 1 = 1 1 is positive => the guard sees our hero! (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* Dot Product Dot product shows the extend to which two vectors are pointing in the same direction But to what extend? What is the angle? The exact rotation is: A•B = |A||B|cos(θ) We can solve that for θ θ = acos((A•B)/(|A||B|)) If we normalize A and B: θ = acos(A•B) var position = new Vector2(3, 4); var direction = new Vector2(1, 0); var angle = Vector2.Angle(position, direction); (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Quaternion Here comes the bad guy! * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Quaternion What is Quaternion? Used to represent rotations * Quaternion What is Quaternion? Used to represent rotations They use complex numbers and are quite hard to understand right away Luckily most often we would not use them (Unity does all the heavy lifting) Quaternion.Euler – rotate by Euler rotation Quaternion.Slerp – interpolate rotations Quaternion.identity – rotation origin Quaternion.Angle – angle between two rotations (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Other Helpful Math Functions * Other Helpful Math Functions Math? Again? Come on! (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Mathf Mathf is helper class provided by Unity * Mathf Mathf is helper class provided by Unity Like System.Math in .NET but extended and with full floating point numbers support Besides the normal Sin, Abs, etc. members: Deg2Rad and Rad2Deg Clamp and Clamp01 Lerp and LerpAngle ClosestPowerOfTwo (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Random The random generator is also enhanced * Random The random generator is also enhanced It is the static class Random Range - random floating point number between min and max (max is exclusive) value - random number between 0.0 and 1.0 rotation - random rotation There are more but these are the most useful (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Summary Quaternions – used for rotation Mathf – helper class for math * Summary Linear Algebra Vectors – used for game math Properties of vectors – length, distance Operations with vectors – adding, subtracting Quaternions – used for rotation Mathf – helper class for math Random – helper class for random numbers (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Basic Math in Unity 2D http://academy.telerik.com

Free Trainings @ Telerik Academy C# Programming @ Telerik Academy csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com