Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Math in 2D Game Development

Similar presentations


Presentation on theme: "Basic Math in 2D Game Development"— Presentation transcript:

1 Basic Math in 2D Game Development
I have to use math? I am out of here… Unity 2D Game Development Telerik Academy Plus

2 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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

4 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: (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

5 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

6 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

7 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

8 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

9 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

10 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

11 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) = 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

12 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

13 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

14 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

15 * 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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

17 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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

19 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

20 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

21 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

22 Basic Math in Unity 2D

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


Download ppt "Basic Math in 2D Game Development"

Similar presentations


Ads by Google