Game developers primarily use simple vector operations—addition, subtraction, length calculation using the Pythagorean theorem, division (normalization), and scalar multiplication—to create game mechanics like character movement, distance calculations, and physics simulations, making linear algebra much more approachable than commonly perceived.
Vector Math for Game Development: A Linear Algebra Primer
Added:anyone who has ever even had a thought about making their own game has without a doubt run into one main problem math or more specifically linear algebra if you so much as google linear algebra you are bombarded with cryptic formulas and scribbles of what seems to be the thesis for a master's degree in mathematics this is where the problem arises an abundance of linear algebra resources is far from a bad thing but for someone who is curious about game development this can make it seem very daunting this is even more problematic when the reality is out of all the topics that fall under the umbrella of linear algebra your average game developer will only encounter a very small subset of these one stands out above the rest for me that one is vectors and the operations you can perform on them like adding and subtracting there's a lot of incredible things you can do with even the most basic of operations regarding vectors so i'd like to get you to the point where you can experiment for yourself first off what is a vector i'm sure many of you have had a section on this in school but if you're anything like me it didn't really do much in terms of understanding them if that sounds familiar i want you to forget about how i was taught and approach this with a fresh mind starting off we'll be focusing exclusively on 2d right now but all of these concepts can be expanded to 3d with a varying amount of effort a vector is simply a collection of two numbers typically represented as x and y these numbers can be either positive or negative whole number or decimal looking at a grid we can plot a point using the x and y components essentially the same as a coordinate but what's the point we can move it around by changing either the x or y or both but this doesn't really do anything for us and it's a bit boring instead we can use this vector as the position of a character you may see where this is going but this is exactly how every 2d game you've ever played knows where your character is so in the case of position data a vector is essentially just a set of coordinates and now for the first in my opinion the most important operation for a vector addition along with being the most important it's the easiest to explain if we have a vector a and a vector b we can just add both of their x's then both of their y's and that's our resulting vector it doesn't get much simpler than that if you're more of a visual learner you can think of adding two vectors as sticking the start of one vector onto the end of another it also doesn't matter which way you do this as you will always end up with the same resulting vector let's revisit our character from earlier we were moving him around by changing his position directly but we can also store a velocity vector then at set intervals we can simply add his velocity to his position and set the resulting vector as his new position this is the basics of movement in video games following addition it only makes sense to move into subtraction subtraction is very much the same vector a minus vector b is simply the result of subtracting x and y of the two vectors however it's more important to note subtraction of vectors does matter about the order and we'll create two vectors based on which one is first on paper the resulting vector from subtraction doesn't seem to be very useful and it doesn't change if we visualize it from 0 0 however if we instead show the vector at the end of a or b we can see that vector a minus vector b essentially creates a vector pointing from b to a if we reverse the order it will instead start at a and point at b in games this can be used to either get a vector between the player and an object to move along or more commonly get a vector with which we can calculate the player's distance speaking of distance we'll take a quick step away from arithmetic operations and instead talk about how to calculate the length of a vector thankfully pythagoras has figured this out for us and the same way you find the hypotenuse the length of a vector can be calculated pretty much the same as if we had a triangle with side lengths x and y square x square y and add them together then take the square root that's the length of the vector and if the vector we're calculating is the result of subtraction then it's the distance between the two objects moving back to operations i'm going to skip multiplication for now and instead opt to show division as it ties in nicely with the length we just learned about division differs from what we learned about addition and subtraction in that instead of say adding two vectors together or subtracting two vectors we're instead dividing a vector and a number if we have a vector a and a number s which we will call the scalar the resulting vector is the x and y of vector a individually divided by s this essentially shrinks the vector unless of course we're dividing by a decimal number between zero and one bringing back our length from before if we divide a vector by its length we end up with a vector pointing in the same direction but it instead has a length of one this is useful for finding the direction between two objects and having a vector cross a consistent length rather than just covering the entire distance the resulting vector actually has a special name in this case since it's now just a direction that name we give it is normalized so if you ever hear a vector described as either a normal vector or normalized that means it has a length of one division has a few use cases but it's most often used alongside our last operation multiplication multiplication is much more in line with division in the way that it involves a vector and a scalar i'm sure you can figure this out but vector a multiplied by scalar s will produce a vector whose components are a's components times s the resulting vector will also have length equal to a's length times s which is useful for turning a normalized vector into any length vector and that's it that's all the math you'll be learning in this video however i do feel like it would be a bit boring to leave this here and i think it would be quite nice to end on an example of some sort while i could create a player script in unity and show off all the different things i think something a bit more visual and appealing to those outside of game development can really sell the point that these simple mathematics can create something amazing so let's make something together we'll approach this in a code agnostic way so regardless of what language you're comfortable with you'll be able to translate this into code rather than show you the final product first i think it'd be cool to set up the rules together and then reveal it at the end for a real feeling of awe we'll start by defining what i'll call a particle it's a simple dot and right now it holds a single vector with an x and y which represents its position it's a bit boring so we'll add another vector to the particle which will represent its velocity every frame or set amount of time we will add the velocity and update the particle's position unfortunately now it just moves off toward infinity so perhaps we should add something to pull it back towards the center let's define a black hole is also having a position vector in every frame we'll first subtract the black hole's position from the particle's position which will give us a vector pointing from the particle to the black hole we'll label this vector a we can calculate the distance between these two store it and normalize vector a which we'll call direction this is just a vector with length one that points towards the black hole if we take this direction vector and divide it by the distance we will get a force vector which is kind of how gravity works in real life with a few creative liberties if we add this force vector to the particle's velocity every frame this is where things start to get interesting we can see that the particle is sort of orbiting the black hole where this really becomes impressive is if we put a bunch of particles and have them all use the same rule remember this was all created using the simple vector mathematics we discussed earlier hopefully this is a good introduction to vectors thanks for watching and thanks for getting just a bit smarter
Up Next

Build an Image Classifier in PyTorch: Step-by-Step Guide
@DrDataScience
416 views•2024-10-02

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science












































