Intro to the Vector Dot Product: How to code a Field of View in Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the dot product is an essential operation that allows us to compare the angle between two vectors with it you can do things like detect whether the player can walk on a slope or not or detect if an object or a character is within an ai's field of view the dot product itself is a mathematical operation that take two sequences of numbers of the same length and it returns a Scala value that is to say a single number so it can be expressed as the formula on the top where you multiply the vectors coordinates but the one on the bottom is the one we're interested in on a 2d plane the dot product of two vectors is the product of their length or magnitude multiplied by the cosine of the angle between them with normalized vectors though the length of these is one which means you can remove this part of the equation you end up with the dotproduct of two vectors equals the cosine of the angle between them and you can calculate the exact angle with the r cosine function with unit vectors or normalized vectors the dot product being the cosine of the angle will always be equal to something between minus1 and 1 if the two vectors have the same direction their dotproduct is equal to one exactly if are at a 90° angle the dot product is equal to zero and if they are opposite the dot product will be minus one now there's a bit more to it than that if the two Vector are facing in the same direction so if the angle is less than 90° their dotproduct is positive if however they are at more than a 90° angle the dot product will be negative that way in a top down game where your character can rotate freely you can see if two characters are facing towards one another or away from one another only with the dot product you don't need to calculate the angle with that in mind let's jump on to gdo and see how we can code a field of view there's a starting demo on GitHub in the start folder number 11 to follow along in this simple demo the player turns towards the mouse and you can see his field of view and his Direction with the green Vector now we're going to turn the field of view red whenever it detects something whenever something inside of it you can also see the various objects these could be enemies or whatever they have a direction Vector that's facing away from the player to get the angle from the dot product we'll need to compare the player's Direction Vector to the enemy's Direction Vector so here's how it works to get the player's angle I get its position then I subtract the player position to the mouse position and I normalize the vector this gives us our look Direction the reason I take 90 and I subtract the angle obtained from the vector is that gdo doesn't Orient the angles like in trigonometry when we use the a cost function to get the angle from the dot product we'll use trigonometry in gdo the angle when it's equal to zero it's pointing down on top of that the angles are clockwise while in trigonometry the zero is pointing towards the right and the angles are counterclockwise this 90 minus V angle gives us that an angle that when equal to zero corresponds to the right side of the screen and that's counterclockwise so if we go up the angle gets bigger and if we go down the angle goes negative to know if we detected anything we use a variable called detect count there's some code related to the drawing down there if we detect anything the drawing color will be red then the enemies are all part of a group in gdo you find groups under the node tab they are next to the signals these are strings or tags that you can put on any number of objects in scripts you can get a list of everything that's in a group using the get tree. get noes in group function here all the object nodes are in the detectable group before we do the calculations with the dot product we have to be sure that the objects are in range to do that I check the distance from the player to the node this is a member function in the vector 2 class you can find it in the docks and to get the distance from one vector to the next you have to write the name of the first Vector here the player position dot call the distance to method and as a parameter you pass the second Vector if this distance is less than the detection radius it's equal to 200 pixels here then we will check that dot product let's start with the dot product um to get it you do it similar to the method called above there's a DOT method on the vector 2 class so you write the name of a vector 2 variable the direction is one you call the dot method on it and you give the second Vector as its parameter and it will be our nodes Direction note that the node is the element we get on every iteration of the tree it's one of the objects from the scene it will be object then object one object two object three Etc this gives us a value between minus1 and one because all the vectors are normalized remember the dot product is the product of the magnitude of the vectors times the cosine of the angle to calculate the angle now we just have to take the arc cosine of the dot product for that you have the a cost function and you'll passed the dot product that's not enough though because all the angle functions return radians and not degrees we want degrees to work with for that we use the radian to degree function and we leave the angle and radian inside of it with those two lines the angle to the node is the angle between the two vectors and now we just have to check if the angle is smaller than half of the field of U you'll find the F of variable able at the top it's 80° so we have to check if it's less than 40° because while just comparing the character's direction to the enemy's Direction and the field of view is cut into two halves by the player's direction if the angle to the node is smaller than the field of view divided by two then we add one to the detect count with those few lines of code whenever something inside of cone of vision it turns red so that's an example to see how the do product works now G has a function that calculates the angle for us so instead we can call the vector's name Direction and call the angle to Method followed by the nodes Direction you'll see it doesn't work out of the box and that's due to the fact that gor returns angles that can be positive or negative so with I would check if one of the values is negative the condition is always true because of that we have to get the absolute value of the angle and now it works just like before the dot product has other uses for example if you want to detect whether the player can walk on a slope or not and it's part of the game developers core tool set so you do want to know a little bit about it hopefully the video was still insightful that's it be creative have fun and see you in the next one bye-bye
Info
Channel: GDQuest
Views: 42,062
Rating: undefined out of 5
Keywords: Dot product, field-of-view, angle between two vectors, AI, tutorial, game creation, Godot game engine, angle between vectors, Godot 2, Vector tutorial, game engine, GDScript, geometry, programming, gamedev, gdquest, nathan lovato, vector, dot, product, angle, vectors, godot tutorial, game development, game creation tutorial, artificial intelligence, open source, game programming, game design
Id: dC4-TSFv32Y
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Sat Jun 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.