Presentation is loading. Please wait.

Presentation is loading. Please wait.

I have to use math? I am out of here… Telerik School Academy Unity 2D Game Development.

Similar presentations


Presentation on theme: "I have to use math? I am out of here… Telerik School Academy Unity 2D Game Development."— Presentation transcript:

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

2 1. Introduction to Linear Algebra 2. Vectors 1.What is a Vector? 2.Adding, Subtracting 3.Scalar Multiplication 4.Length, Distance 5.Dot Product 3. Quaternions 4. Other helpful functions 2

3 But… I thought math is for kids!

4  Why should I care?  Linear Algebra is the study of vectors Linear Algebra Linear Algebra  Vectors are really helpful in game development  2D or 3D  Used for positions, directions and velocity in Cartesian coordinate system Cartesian coordinate system Cartesian coordinate system  The better you understand linear algebra, the better your games! Deal with it!  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 / http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1 / 4

5  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! 5

6  How to create vectors in Unity with C#? 6 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

7  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) 7 var one = new Vector2(2.5f, 3.8f); var two = new Vector2(-2, 5.2f); var result = one + two; // (0.5, 9)

8  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) 8 var one = new Vector2(2.5f, 3.8f); var two = new Vector2(-2, 5.2f); var result = one - two; // (4.5, -1.4)

9  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) 9 var velocity = new Vector2(5, 0); var waterFactor = 0.7f; var waterVelocity = velocity * waterFactor; // (3.5, 0)

10  How to find the length (magnitude) of a vector?  Written with |V| - length of vector V  Calculated with Pythagorean theorem Pythagorean theoremPythagorean theorem  Example – (4, 3)  |V| = sqrt(4 2, 3 2 ) = 5 10 var velocity = new Vector2(4, 3); var length = velocity.magnitude; // 5 // use velocity.sqrMagnitude for comparison

11  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(1 2 +2 2 ) = sqrt(5) = 2.2361 11 var first = new Vector2(1, 2); var second = new Vector2(3, 3); var distance = Vector2.Distance(first, second); // 2.23 second); // 2.23

12  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) 12 var position = new Vector2(3, 4); var normalizedVector = position.normalized;

13  How to calculate dot product of two vectors?  First multiply the components and then add the results  (a 1,a 2 )(b 1,b 2 ) = a 1 b 1 + a 2 b 2  Example:  (3,2)(1,4) = 3*1 + 2*4 = 11 13 var position = new Vector2(3, 4); var direction = new Vector2(1, 0); var dotProduct = Vector2.Dot(position, direction);

14  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  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  DV = (1, 1)(2, -1) = 2 – 1 = 1  1 is positive => the guard sees our hero! 14

15  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:  AB = |A||B|cos(θ)  We can solve that for θ  θ = acos((AB)/(|A||B|))  If we normalize A and B:  θ = acos(AB) 15 var position = new Vector2(3, 4); var direction = new Vector2(1, 0); var angle = Vector2.Angle(position, direction);

16 Here comes the bad guy!

17  What is Quaternion? 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 17

18 Math? Again? Come on!

19  Mathf is helper class provided by Unity Mathf  Like System.Math in.NET but extended and with full floating point numbers support System.Math  Besides the normal Sin, Abs, etc. members:  Deg2Rad and Rad2Deg  Clamp and Clamp01  Lerp and LerpAngle  ClosestPowerOfTwo 19

20  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 20

21  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 21

22 форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране http://academy.telerik.com

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


Download ppt "I have to use math? I am out of here… Telerik School Academy Unity 2D Game Development."

Similar presentations


Ads by Google