Game Programming Episode 94 - Basic Chaser AI
The Cherno
·
Intermediate
·12y ago
Key Takeaways
The video demonstrates basic Chaser AI in game programming using a star search algorithm and implementing a system to handle players in multiplayer mode.
Full Transcript
hey what's up guys my name is the Cho and welcome to episode 94 of game programming so we' got a pretty exciting episode today because we're going to talk about how our Chaser is actually going to work so what we have right now just so you guys can see is um we have a bunch of these dummy mobs that we made in episode 92 I believe and we have this Chaser mob that we made last episode episode 93 and you can see that he's distinguished by the red appearance here because we actually used a quick um if statement to essentially reassign all of the purple pixels to be read when um when this actually gets rendered so we did that last episode of course now today we're going to talk about actually uh giving this Chaser some Behavior so I'm actually going to right click on this Chaser thing up here and go close others and that'll just close every other class we have open right now except for the Chaser because that's really all we need for this episode now let's talk about his behavior in fact before I do that I'm actually going to go to the spawn level and I'm actually going to get rid of these dummies that of spawning okay and all we'll do here is we'll actually just spawn one Chaser okay this makes this full loop obviously redundant since we're only doing it once we could have done that that would have been the same thing but the reason I'm keeping this here of course is because um I might want to change that in the future I want to have to retap that for Loop so yes just to clear that up so all we're doing right now if we launch our game is spawning one single Chaser up here and what we want him to do is follow the player yeah that's all we want him to do and this Chaser entity um this this Chaser mob is is going to have an extremely simple Behavior okay he's not going to have some some elaborate you know let's let's make this guy uh sniff you out like a dog and I don't know work his way around a walls and find the closest possible path to no this is going to be simple okay the more advanced mechanics of pathf finding and all those algorithms that will that we'll actually use to to essentially uh transport a mob from point A to point B you know around walls and all that stuff that'll be done in future episodes but right now we want to start off pretty simple because these are these are quite these are quite stupid mobs okay so you know you have to it's kind of like think of this whole AI uh section of game programming that we're doing now think of it as as almost like like the life of a human right you start off like a baby yeah so like your your behavior is pretty like you know I guess limited you can't do really much you're not super intelligent or anything um and as you as you kind of grow up you get more and more smart and you can suddenly find your way around walls whereas as a baby maybe if you see something out a window you're like oh I I want to get to that and you can't cuz there a window in the way and you don't actually realize that so you know that's that's what I mean by that that was a smooth metaphor I just met up on the spot by the way but anyway um uh it's like a it's like a life kind of span okay so um that's why we're going to start off start off small um also because if I dive into the complicated stuff you guys are going to be like Joo do those in-depth episodes you mentioned like half the series ago and never actually did um about this stuff so that's why we're starting off pretty small so um this is going to be simple but of course not everyone actually knows how to do this so please remember that for people who are going to complain about stuff like oh po a star search algorithm that's easy why don't you do something more no okay chill out I hate it when people comment um being all like pretty pretty selfish and stuff being like why not you going over this because dude like thousands of people watch this series Okay thousands and the thing is not everyone's going to understand um so that's that's that's the way the cookie crumbles um always wanted to say that I do on a daily basis to my friends what friends anyway um so yes we're going to make a actual method here um what should I call this uh we might make a method here in update called um think a name here uh uh I I would call it move but move method yeah okay we'll make one called move okay that's fine we'll make a we'll make it a private method so private void move okay and the reason I'm doing this we're actually going to move this up here by the way the reason I'm doing this the only reason I'm doing this is because I I I just want to keep our code grouped over here so that we can we don't need this update stuff we don't need this all this crap here we just need to focus on the Move method now the update method it's uh probably a good thing to note has all of our actual move code and move of course references our um our mob class here with a public word move which really should be protected but anyway um not going to change that now um and of course it handles our Sprite directions as well as um animations for walking okay that's worth know and make sure you actually do have the move method in the update method otherwise we we're not going to move anyway so um how the move method is going to work is essentially XA and ya I believe is uh the way that um if we go over here okay this move method should really have this move as well right over here so let's actually cut that and um yeah I'm not sure if this is a good idea that's all right this will work though it's just that because like um all of this stuff is really like this Direction stuff it really does have a lot to do with actually moving but that's all right because remember um move actually in in update this stuff is running this this stuff isn't running concurrently it's actually running uh in you know in um what's the right word for this I guess it's just running in um in in order essentially um sequentially here so because of that um this will get executed first and everything else later that's probably fine yeah and then at the start we can clear our XA and ya that works for me okay cool so in move right just to test this out all I'm going to do is uh we're going to actually set XA equal to Z every frame and y equal to Z every frame and then uh let's just animate uh XA by setting it equal to Plus+ here so if we launch our game right now we should see moving right and he does okay brilliant so that works just testing that okay so now we can actually focus on the code that um is the the I guess the AI so what again our objective for Chaser is just to literally chase the player yeah and we can do that by um a series of if statements okay this is quite simple you guys could probably work this out if you did this um in your heads or on paper or whatever if you just thought about it for a minute so what we kind of want to do right is we want to actually make him follow the player now if you think about it for a minute um in order for us to make him follow the player we need to know the player's position don't we yeah we do okay um we need to know the players's position we also need to know uh well that's pretty much it that's it that's actually it all we need to know is the Players position and the Chasers position Chasers position we've got cuz we're in here players position we do not have right now so because of that um we're actually going to have to make a method to get the player because currently we're unaware of what the player is now how are we going to do that okay there's multiple ways to do this of course that's what programming is all about but the best way to do this probably at this point is uh in the level class because every entity hasn't has access to level and what level could have is a public void here that is uh essentially get player okay sorry not void public play G player now the reason why I'm kind of cautious in doing this is because of course this game will support multiplayer um in fact it'll be kind of like an M an MMO setup here so um it will have a lot of players um and because of that uh we will need to make a system that deals with players as in like an array list that deals with players uh for every single player in the game um but that's all right because all we'll do then is we'll just simply uh change uh this method to this and that's it okay and then we'll just return the list of players so don't worry about that now okay because I know that a lot of you guys are worried about future proofing this don't worry I've thought thought about the future like every episode so I am doing things um generally making this series Pretty sustainable as far as uh as far as what I want to implement in the series goes so we won't have to write we won't have to rewrite tooo much code um so public player get player and we essentially need to return an entity that's a player so how can we do that well first thing we need to do is actually find the player now the player I believe does not have his own uh no he doesn't Okay so he's still an entity right and we need to find him so I'm just thinking in fact we should probably future prove this right now that's what I'm going to do and the reason is it's going to be because this this method is going to be um Rewritten what I was going to do by the way is just make a full loop so if uh sorry I'll just show you guys what I was going to do so for in IAL z i is less than entities do size all you have to do here i++ of course is just say simply that if entities dogi is an instance of player since there's only one then we can just return entities. get I okay and that will work in fact let's try let's do it that this this way for now just because I've already written it out um and of course we need to cost player um okay and that's pretty much it otherwise we can just return null so in other words there's no player which would be weird but um anyway so what we need to do is first of all get a player object going so play a player yeah uh and player player will of course equal level. get player and that's really that that's really how simple it is now if you wanted to be really good with performance you could do this something like this but um we we will get it every frame because I'm it's probably the best way to do it so uh if player. getet oh we don't have any of this so the entity class not this entity class The Entity class here should really have uh get X and get y not sure why it still doesn't I'm pretty sure I implemented that episodes go but might have been a dream public void get X return X public oh void int public int get y return y okay that's all we need so back here in Chaser now player. getet X as you can see that disappeared so if player. get X is actually less right than or in fact Let's do let's do it this way if our current X location is less than play. get X then let's actually make sure we go forward so in other words by forward I mean right okay pretty simple statement here um what we're saying here is that if our current if if if the current chases uh x coordinate is less than the player's x coordinate then make then let's make him move in the positive X Direction because that would get him closer to the player as far as the x axis goes now if we copy and paste this another three a further three times we can make make sure that we handle if x is greater than play. get X in that case we need to actually move left if x if Y is less than the player. get y then we of course need to increase our Y and of course same thing goes for if Y is actually greater than player. get y we need to actually um subtract y to balance that out okay and that's all we need to do so if we hit this up we'll get a crash here with a null pointer exception apparently um which is a weird thing here so hang on am I right in saying that players are not that am I right in saying the player um gets added to entities I think it does it would have to right let's take a look at how we add a player first of all um so player let's see do we have player okay we do have a player in here so player equals new player player. init level um and oh look at that interesting so we never actually add player as an entity that is pretty horrible design that I did well it's not horrible design it's just um it's just a weird way of doing it um and I guess we do use him here for the rendering logic that is fine okay interesting okay interesting so this game is made slightly differently than I thought it would be and I'm sorry it's just that it's been it's been like a year since I started this series and of course um my conventions and my way of things have evolved um and this series really hasn't well in in that sense anyway so let's go back to level and let's implement it the way that I was going to implement it and the way we're going to do this is we're actually going to make a private list uh that contains player the object player we'll call it players and it's going to equal a new array list of course player right and let's import everything there I think everything's already imported though um so now we have a list of players um and so what we need to do here is in player. getet player we need to actually change this to be a list of players change this to be players and um and we can just simply uh let's see here we can just simply return the list of players now one other thing we might want to do is get a public player get player at or something and put the index as a parameter and then simply return players. get I or index in this case sorry index uh and one one other thing we might want to get is uh public player get client player okay and and kind of thinking Network thing here that'll just basically return players.get zero because no matter uh how many players are in the game uh your player the player on your client side will will always be the very first one there because of course that will get created first as well as if it doesn't it still get sorted so we just need to really get the client player here okay so now that that's sorted out um and we've got three methods to actually get players uh we'll go to um we'll go to game here which is our main class and instead of um initializing player this way and all this stuff we're literally just going to hit up level. add player okay and because we've done this um and sorry we need to actually change this to an ad player or in fact did we make this Universal I don't remember yeah we did so let's put another else if here so else if e is an instance of player that does not work out so if e is an instance of player then we'll go players. add e all right uh and what's up oh make sure we Cass this of course okay and of course we we will initialize it here um so so if we go back to game here we can see that we do actually add it and over here in update we no longer need this and over here in render we no longer need this either all right so player X and Y are actually uh we should really change this to get X and get y because they will return the same thing and they'll actually be in the entity class so over here in player because of that we can now make sure that uh X and Y which don't even exist to begin with where are we getting them from were we getting them from entity yeah so they used to be public let's make them private um and what will that do oh sorry not private protected protected okay and we should have no errors with that and we don't brilliant so back in this is a long episode isn't it back in uh here yeah okay so we removed it from up we've removed it from render good so level. render is obviously now where the player will get rendered so so we need to duplicate this for Loop we've got a lot of them here I know um and we'll go for I is less than players do size um oh top layer that's stuff from um I should get rid of that stuff shouldn't I this is from something else I was working on that I forgot to uh do I don't think we've covered that yet no we haven't um I was just I was doing I was trying to make a visual uh visual way to actually show Collision detection and I need needed um I needed a layer of entities or a layer of Sprites at least that rended on top of everything else so I had to make a top layer thing don't worry about that that's relevant we'll cover that later if need be even it's more of a diagnostic tool so if I is less than this is in remove let's get rid of this uh I guess we could players do size we'll put this in remove as well I'm pretty sure we've done this oh have we I don't know this is the remove method I should really I keep forgetting to duplicate this project um anyway so for eyes yeah blah blah blah less play size what we'll need to do here is actually uh um oh no this is fine so if players don't get eyes removed then we need to remove that one that's pretty simple um we also need to make sure that we're doing it for update so copy and paste that so for players size we need to make sure that we update every player since we've removed it from the main games update method and we need to make sure that we r it finally now players we want to probably render over here we actually got this thing here so players.get I do render with screen of course as the parameter and that's it so if we run our game now uh making sure that we uh get rid of this if we run our game now okay what we should get is uh this player you can see chases us now one thing that's not good about this is there's like literally no delay that's a bit weird as well oh so he's a bit offset actually as you can see but he moves like the same way that we do here um which is fine uh it's pretty funny okay so if we try and run away from this guy you can see that he literally follows us see um he doesn't come to our exact position because I think the deal is that uh his coordinate is offset um so what we could do is if we really wanted to clean this up uh I think the deal is if we go into entity is it entity no it's Chaser um the way that we render him is actually at uh X and Y whereas if we go to player we can see the play is red at x - 16 y - 16 so it's kind of like the center so if we go back into Chaser and that's actually why Collision wasn't working now that I think about it from the previous episode um x - 16 y - 16 we launch this he should stop pretty much exactly where we are as you can see okay so the coolness about this is that well here's perfect and that's bad he's actually perfect you can see he's actually on us exactly um and that's that's not a good thing usually not a good thing at least because he'll always get to us no matter what um because he always takes the most efficient path and but he he will lose us lose us through walls okay so if I was to for example go to player sorry go to game and make sure that the player uh the spawn location the player here is atle coordinates it's maybe a bit H like 42 I don't know there probably spawns in a wall no okay so yeah okay that kind of works so I think I can okay I'll be able to okay let's run let's run so he's behind us um but I believe if I wait for him to go here yeah okay see he can't get past the wall obviously he's colliding with it um and this is both this this this is quite nice as well cuz if you imagine like a zombie type Behavior he might not be able to get past that like you know that might be your game design idea um if you want them to get past that uh you'll have to work out a pathf finding algorithm so he has to look at this and say okay I can't get to you using this method what is the shortest path or the longest path I don't know or any path or a random path I can use to get to your location that doesn't that doesn't include walls that's where the a star search algorithm comes in now the point is though um we want to kind of get away with making AI as simple as we can okay and while of course we will cover the AAR Sear gthm um in depth uh well I guess normally as we do in this game programming series which is fairly in-depth yeah um we will cover that of course but before we do that I do want to play around with this algorithm not today of course cuz we're at 20 minutes already but we will play around with this method because you know as game developers we want to kind of get away with making things as simple as we can okay especially if you're doing something like lotum there where you don't have time okay and if you even if you do have time this method is hell of a lot faster then um this method here which is what we're using here is a hell of a lot faster than having to um actually Implement a algorithm for each of your mobs okay without a doubt so because of that um it's it's a this is much a much Simple Solution so next time we're going to take a look at actually kind of randomizing this making him a bit slower to think making him a bit slower to change direction as well as making him um just move a bit slower or maybe just you know not be as sure as whether he should actually follow the player or not or basically I guess not be as sure as to where the player is exactly and that'll just give him a more realistic less robotic kind of approach to this but anyway guys I hope you enjoyed this episode of game programming if you did please hit the like button 200 video 200 videos 200 likes equals uh one video per day and 300 likes equals two videos per day and um so yeah hit the like button if you guys enjoyed this video if you didn't please tell me and I'll see you guys next time [Music] goodbye that's [Music]
Original Description
200 likes = 1 video per day, 300 likes = 2 videos per day!!!
-------------------------------------------------------------------------------------------------
In this video:
-
-------------------------------------------------------------------------------------------------
Website: http://www.thecherno.com
Twitter: http://www.twitter.com/thecherno
Facebook: http://www.facebook.com/thecherno
Subreddit: http://www.reddit.com/r/thecherno/
Steam Group: http://www.steamcommunity.com/groups/thecherno
-------------------------------------------------------------------------------------------------
Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from The Cherno · The Cherno · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
More on: Tool Use & Function Calling
View skill →
🎓
Tutor Explanation
DeepCamp AI