5.3 Flee, Pursue, Evade - The Nature of Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again where i last left off in the previous video i implemented the steering behavior seek this little triangle is seeking this red circle and i am on a quest i would like to implement every single one of these behaviors that craig reynolds outlines in the 1999 paper steering behaviors for autonomous characters so i haven't gotten very far i've just done seek i don't know that this round of videos that i'm making in this year this moment right now 2021 if i will get to all these i'm going to get to a bunch but hopefully sometime in the future when you're watching this you will write a comment saying congratulations you finally have a video on every single one of these for now all i have is seek good news in this video i'm going to knock out flea pursue evade and arrival all at once let's make a list as a reminder the seek behavior is defined as steer towards the target at maximum speed this is the desired velocity and the length of that velocity is max speed we're going to be done with flea in about 30 seconds you start the clock flee is defined as steer away from the target at maximum speed so this would be the desired velocity for flee going back to the code it's as easy as finding the seek function looking for the force and multiplying it by negative one if i hit run there it goes it's going to flee that target and i think that i did this in the 30 seconds a lot but i actually want to pause and take a little bit of moment to breathe here because really i have a goal i don't want to build examples that only do one thing i eventually want to be able to mix and match all of these different behaviors within one sketch but i'm getting ahead of myself let's live where we are for the moment in the present with this example and how could i take this force multiply negative one write a function called flee and then not have to copy paste all this code into there though there's any number of clever ways i could do this with different arguments and overloading but i think it would be advisable for me to actually in these steering functions never directly call apply force the goal of a steering function should be to return the steering force and then whatever is calling that steering function can figure out whether or not to apply it or not so i want it to look like this call the function seek get a vector back apply that force so remove apply force here and replace it with return force let's make sure this is the same it is now guess what fleeing a target is what return the result of seeking the target multiplied by negative one and i'm just going to rename this to steering here so it's more generic and there we go fleeing the target makes use of the seek function and just adjusts the direction of the vector in the opposite direction we're moving along here flea is done next up pursuit the key difference between seeking a target and pursuing a target really has nothing to do with the steering behavior itself and more has to do with what is the target doing pursuit refers to seeking a moving so now i'm going to draw this as a triangle as well i could give it a different color does that help and the target also has a velocity now on the one hand i could say hey this vehicle it wants to reach this target even though it's moving just go in the direction of the target this desired velocity would work fairly well but the idea of an autonomous agent is having this limited perception in the environment so maybe this target has been kind of moving along this path this vehicle is it has this intelligence to it it's able to perceive the way the target is moving kind of out of the corner of its eye you can think of and it understands that in a few moments it's actually going to be over here so it's perception that environment involves making a prediction about the future location of this moving target so that its desired velocity should then be move at maximum speed towards its predicted future target and it will recalibrate that future target every frame so if this vehicle were to suddenly turn and go over here you know a couple frames later it's going to be looking at its future target as being here its future target might also be further away or closer based on how fast the vehicle is moving in order to demonstrate this what i'd like to do is make both the vehicle and the target vehicles i'm going to call one the pursuer the other the target and make them both vehicles now the vehicle should be seeking the target's position that's without this new pursuit algorithm and i've got to change that to pursuer i'd like to make these vehicles a little bit more visually distinctive which one is the pursuer which one is the target so i'm gonna go back to my inheritance lesson from chapter four and just override the show function and make it a simple circle and also give it um a color and change this from a vehicle to a target so interestingly enough if i'm going to write a class that extends another class i've got to put this one after it great now i just need to put this target into motion so i can hard code in a velocity and make sure that i call update on the target and now there we go that target's in motion i have this edges function from before that i can also just call just to have the target wrap around wrap the edges so the pursuer is not wrapping around and i actually get this sort of fun behavior because the the target keeps like disappearing and reappearing somewhere else and it kind of just can't catch it so uh you know i could probably let the steering force be larger let the maximum speed be higher and get it to catch the target but let's just see now what happens if i add this idea of pursuing its future predicted location so i'm going to add a function called pursue give it the argument target and then i've just realized something i don't want to give it a vector the position of what i need to pursue is not enough information i need to look at this whole vehicle know where its position is and its velocity to predict its future location so this should actually be another vehicle and the target is the vehicle's position make a copy of it then add to it the vehicle's velocity now this isn't everything because i'm just adding its velocity so i'm just looking at where it will be the next frame and i probably want to look like 10 frames ahead or 20 frames ahead but let's just see what this gets us so now i'm going to call pursue that target and it looks kind of the same let's draw some debugging information so i can see where that target is and then let's look a certain number of frames ahead how about 10. so now what it's actually seeking is that position in front of that target vehicle itself oh oh oh i've actually made a terrible mistake here i was about to explain how i didn't make the mistake and i made the mistake i was assuming i was okay because i copied the vehicle's position before i started messing with it but i didn't copy the vehicle's velocity and it's not actually causing a problem because i've got a limit on the vehicle's velocity so i'm multiplying it by 10 and then it ends up getting limit but this is this is bad and so to be safe let me just have a separate variable called velocity which is a copy of the vehicle's velocity i can then multiply that by 10 and i can chain those to have that at dot copy dot multiply but and then i can say target dot add that velocity and you know what i'm going to call this um prediction just to it's not it's no longer really a velocity it's helping us with our prediction about its future location and this should look the same but i've now less prone to error here now i've got pursue done let me quickly add evade which is exactly the same as pursuit but just in the opposite direction so now i call the pursue function multiply the result by negative one and then return that i can change this to evade and run it ooh oh i made a spelling error pursuit oh my god how do you spell pursuit it's with you there we go now the vehicle is just going to leave and never come back i could add let's see we're just i'm just out of curiosity let's add the edges to the pursuer i also am going to take out the sort of debugging the circle and now we can see this triangle is always going to try to evade that other moving circle i'm actually kind of curious and maybe i'm going off on too many tangents here but what if i add a second vehicle target and have it pursue one and evade the other so two targets call evade on one pursue on the other apply both of those forces i don't know let's see what happens which one is it pursuing which one is it evading i don't know and of course i should probably be thinking about using an array and uh designing things that it wants to pursue versus things that wants to evade in a more intentional way but you're hopefully you're seeing the beginning steps of how you might start to take multiple steering behaviors and apply them into one sketch putting this back to just a single pursuit vehicle and one target getting rid of the idea of edges i think another effective demonstration here would be when the pursuer reaches the target the target disappears and reappears in a new location with a new velocity so i can look at the distance between the pursuer's position and the target's position if that distance is less than the sum of their radii then recreate the target at a random location let's give ourselves more space to work with let's also let the pursuer have a higher top speed and i'll also set the pursuer back to the center of the canvas let me add the edges to the target and finally let me also give the target a random velocity and this is a nice demonstration of a moving target and a vehicle pursuing that target this would probably be a much more compelling demonstration if the target itself had its own rules for motion beyond just a constant velocity maybe it bounces off the edges maybe it's its own has its own set of steering behaviors that governs how it moves around the window i'm going to look at a behavior called wander in the in a future video what if it were wandering and then this thing was pursuing it so many possibilities there i'd also be curious to see what kinds of ways can you annotate this to make all the parameters of the system more clear beyond just drawing like one green dot ahead of it where it's actually uh seeking and in fact how far ahead you look could be a parameter that you tune up and down so many possibilities i'm going to stop here uh you know i have now successfully gone through seek flee pursue and evade and i really was going to do arrive in this video but i think it actually makes sense to pause here let you play around with that particular example try your own version of it and i will come back this time i really will come back in the next video and look at what it means for a vehicle to seek a target and slow down as it's getting closer the arrival behavior [Music] you
Info
Channel: The Coding Train
Views: 30,625
Rating: 4.9816175 out of 5
Keywords:
Id: Q4MU7pkDYmQ
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Thu Jun 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.