Game Programming Episode 92 - Basic Random AI

The Cherno · Intermediate ·12y ago

Key Takeaways

The video demonstrates how to implement basic artificial intelligence in a game using Java, creating random movement for non-player characters, and utilizing tools like the random class and FPS monitoring.

Full Transcript

hey what's going down my name is the channel it's the first time I've changed my intro probably the last time as well but welcome to episode 92 of game programming so we're going to talk about some very very very basic AI okay because I want to start off basic and and very Advanced so so last time we left off with um with this animated Sprite stuff and actually making our dummy uh guy move okay well kind of move right not really move just um basically just uh well we gave him the ability to move but not the not not the artificial intelligence not the intelligence to decide how he wants to move okay but we gave him the ability to move of course and you can see he's moving down right now which is pretty awesome and that's the only Direction he can go right now so we're going to change that uh right after I put a strawberry into my mouth pretty good um yeah so as you can see I'm pretty professional with my programming tutorials all right sorry about that I just I just made you guys sit through me eting a strawberry um uh anyway so um right so to do that um we need to actually give him um a bunch of if statements here um that basically say if this is happening move this way if this is happening move this way right um so we're going to give him some very basic Ai and I really like this type of AI and the reason is it really it's really good for ambient okay and what I mean by that is sometimes you just want entities or Ms specifically and you know NPCs that um basically just uh they just exist right they're not they might they're just there to kind of make your world feel a bit more lively okay um and they have no purpose okay they they don't go anywhere like it's in they don't have a set task they have to do they're not an enemy they're not going to attack you they're just there literally for the environment right for Ambience and because of that we don't have a predefined uh AI kind of structure that we want them to actually follow right we don't have like a okay if the player gets this close beat him to death no we don't have that stuff right or um you know make sure you go to this wayp point and do that go to then go to this way point do that if the player uh gives you $600 then give him this you know we don't have any of that stuff right the only thing that um is going on these guys are just there just to kind of fill up our Ambience okay and because of that we want to kind of set an AI script here that that essentially just um you know just just makes them kind of we don't want them standing still we just kind of want them to move kind of subtle you know and just um just kind of exist okay um so what we're going to cover today is basically how to do that okay some very basic random um AI movement okay so right now we have this uh this poor little King Cho here who's standing up to uh next to Jesus over here in a suit um and um we want him to just kind of you know move over there maybe stand over there for a while then change direction maybe move over here and you you know something like that okay um so the way we can do that is first of all we need to make a variable here called time okay and we're going to set that equal to zero and basically as long as the guy is alive we'll set we'll just increment his time okay and because it's in the update method this will be equivalent to incrementing uh this time variable 60 times per second so every second this time variable will be a multiple of 60 that's how it's going to work um and so because of that um that that doesn't really tell us much but that that just make sure that for example if you do something like uh time um mod 60 right um or time you know if you're really cool time and 59 um that will actually be equivalent to uh one once per second so that that's kind of going to give you a measurement okay of um of time here so time mod 60 will be actually equal to 1 second so another in other words if you set something to happen when time mod 60 equals 0 well obviously sorry that very bad phrasing here time mod 60 is not going to give you zero oh sorry is not going to give you 1 second what I mean is that um time mod 60 if time mod 60 will equal zero that's going to happen once per second as long as we keep incrementing this time uh variable 60 times per second okay so when time 60 equal 0 that is 1 second okay um so that's kind of how we're going to measure this stuff okay now that we've covered that um let's make him basically move so what we can do here is say that if time for example this is just to give you guys um a bit of a just just a bit of knowledge as how as to how this is going to work so if time mod 60 equals 0 which is just what I um explained this is this is the event that will happen once per second so maybe just for now let's make him change direction once per second so we'll make XA uh Plus+ here sorry let's okay make sure that um this stuff is before our int XA and in ya declarations um we'll make X A++ and we'll say that um uh H so what we might want to do here is not actually set them to zero every frame let's move these int X and Y declarations up to the top here so they are class variables and the reason is if we keep creating them as local variables every update it's obviously going to set them to zero so what we'll do is we'll uh make XA be uh Plus+ here or rather we'll set XA to be uh equal to 1 okay and um we'll say that if time uh oh maybe I don't want to do it that way all I want to do here is let's actually set XA equal to 1 to begin with okay and what I'm going to do is I'm going to say that if time 60 is equal to zero so once per second let's reverse his X Direction so XA * equals -1 okay or you can obviously do it XA = XA same thing right let's actually do this way um so what this will do is once per second it will actually change the direction so let's uh let's hit the debug button and we'll see what that looks like okay just like that and you can see the animation is still a bit dodgy I haven't fixed that yet but that's what it's going to do so he's just going to roam left and right okay and once per second he going to change his uh direction if we change this mod here to be 120 you can imagine that it's going to happen once every two seconds so he's going to move that way 2 seconds passes is going to change direction 2 seconds passes is going to change direction again okay that's how that that's basically how that kind of works okay uh and we can obviously do the same thing for ya so yaal y except let's initialize it with one okay and there he is sorry I must have uh XA still set to one let's set that back to zero so he's going up and he's going down okay very very simple okay wouldn't really call this AI either but it's um it's very simple okay that's kind of how this is the basis of how this is going to work so instead of us going ahead and saying yeah okay let's change this direction to something something that's predictable we can use Java's random class to actually randomize this a bit so instead of this what we can say is um is instead of this uh if time let's just say once per second at least for now is equal to zero then let's set XA equal to random. next int uh 3 okay now what what what is that going to do let's talk about what this is going to do random next in with the parameter of three will actually choose um an integer obviously it just say random next int it will choose an integer between 0 and two so in other words it will give us a number that is either 0 1 or two now that doesn't really help us much because um X if XA is two well he's just going to be moving um horizontally a lot faster right that's not going to help us we don't want it to be too what we do want to want XA to be because remember for XA and ya our possible values here not this is just excluding speed by the way our possible values are netive 1 0 and 1 right if it is negative 1 we are moving left if it is zero we are stationary on the A on the x axis and if we are if XA is positive one then we are moving right so what we can do is actually just subtract one from this and what that's going to do is this will give us a number between zero sorry this will this random. next in three will give us either 0 1 or two and no matter what it gives us that value will subtracted by one which basically translates XA to have the value of either -1 0 or 1 okay and we can do the same thing for let's just not do the same thing right now let's just check out let's just check out what this looks like so if we hit thebug um of course bit of an idiot here let's set uh y equal to Z here so so he starts off here then he moves and then he stops as you can see then he moves further then he might move left maybe doesn't really feel like it move right there we go so yeah you can see that this kind of has this kind of you know this kind of has um some pretty weird Behavior here right once per second he will actually change his direction or or he could just stop as you can see he does there okay let's let's do the same thing for ya so yaal random. next int 3 -1 all right and let's check out let's check that out so he's stationary oh he decided to go here now he's moving this way way now he's moving this way now he moving this way now he's moving this way now it's just going that way it's completely random okay and the cool thing about this is that um he hasn't actually stopped yet but um the cool thing about this is that um it actually gives him a um gives him some life okay and to make him stop if we just wanted him cuz that guy in my opinion was moving a bit too fast we could just say that if random next int uh let's just say um let's just say he has a one and five chance to actually just stop so if random extent 5 equals 0 then we'll just set x a equal to 0 and y equal to Z okay simple as that um and we might actually just uh uh move this up here into this method just so that it happens once every 60 seconds uh sorry once every um second here so you might move a bit it'll stop you move this way and then he might stop hopefully there we go yeah that might be a bit uh not frequent enough let's make a three okay so he has a pretty strong chance here of just stopping you can see he does that okay so that's pretty basic now the only thing that I'm not a big fan of here is the fact that obviously he he he only has the ability to actually change once every second right so that's very predict able we know that only once per second will he change where he's going um so instead of doing that we could of course speed up the speed at which he does that if we set time to be Mod Five or something um then what you'll see here is that he just you know he kind of like stutters all over the place and the reason is he's getting random values all the time so the more you decrease this the less smooth his walking will be the more you the more you decrease the value that you mod um this time variable by so what we could do is actually set the um I guess the interval of time by which he changes direction to be a random value so random. next in um let's see here so we want it to be within reason um we want it to probably be uh okay well we want it to at least be 20 right we don't want it to change direction F faster than say I don't know 20 faster than three times per second okay any faster than three times per second is kind of too fast to change direction um in fact I might actually put this up to 30 and then over here um what variation do we want to get give to that 30 so it will be at least 30 but how much more than 30 will it be that's what we can change so maybe if we put 40 in here for example I'm just going to put 50 in here if we put 50 in here what that means is that um he will actually have a random chance to change direction um either essentially um between 30 or 80 here so we've given um so time will be moded by by anywhere between 30 80 now what that will give us is essentially the maximum ability to to change direction twice per second or um the minimum he will at least change direction pretty much um well just just over 1 second probably about 1.2 is uh every 1.2 seconds okay and that will be random every time so let's check out what that looks like crashy crash um arithmetic exception uh H okay I'm actually make sure that you um let's uh let's put this in Brackets to make sure that it's because the reason it g gave us an arithmetic exception is probably because it gave us zero and of course you can't divide by zero here um so make sure that um that it's modding by this entire equation not just by random the next in 50 and then it's adding 30 okay let's launch that again so yeah there he goes and he has a high chance to stay still let's get rid of this right now so this random next three business let's uh comment that out for now so that is not stationary so he's moving you know he has a random chance to change direction you can see he's pretty uh whoa well that Collision kind of failed there didn't it looks like he's offset by one TI for some reason we'll fix that up later um all right and yeah so you can see how that's kind of working out okay um but the thing is again he's he's almost never stationary I want him to be so that's why I brought in this code so maybe if we change this to about four all right that's pretty cool yeah he moves and he's stationary we we could make him not move diagonally for example that could be another addition but what you what you get here is a is basically just this guy who Minds his own business right um and I I really like that type of AI okay it's as you can see it's it's ridiculously simple to implement um but not only ridiculously simple to implement it's also really effective it just looks nice it's good for AI that you just want to fill your fill your scenery fill your environment um and of course if we wanted to add more of these if we go to our spawn level class if we want to add more of these instead of adding one dummy we could actually put this into a four Loop so 4 in IAL 0 I is less than 10 for example if we want to add 10 let's add 10 of these new dummy objects now of course they will all be separate and they will all move in different directions because um they are new they are they're actually new new instances of dummy so if we um check that out what we get as you can see is a bunch of uh just basically ambient guys here right they're just moving around minding their own business whatever not colliding with stuff because I just don't care um and they're pretty simple right and you know I'm sure you guys will have a lot of fun with this you can add like 100 of these guys and uh well yeah and you can probably see the FPS here does drop a little bit but hey there are a lot of guys here but we're still running at like 2,700 FPS here um so yeah and this will as you can see this just looks really nice there might there probably too many here but you you know you do get my drift here um it is really nice for for creating like some environment um fillers I guess just to make your uh map look a lot more alive here so that's how that um that's how that works okay so um that's going to basically conclude this episode of game programming uh next time we're actually going to take a look at very very simple chasing algorithm um just in case you guys are not aware of it of course uh it's very very simple but we will um Talk a bit more in detail about how we can improve that a little bit and um after that of course we're going to cover way points and after that we're going to cover the a star search algorithm and that'll basically be our finale for AI for now at least okay and then we'll move on to more things so I hope you guys enjoyed this episode of game programming if you did again please hit the uh like button if we do hit 300 likes that is two videos per day otherwise 200 likes equals one video per day but um I do really hope that you guys are enjoying this these um game programming episodes as much as I enjoy them and um you know if you're not or if you are just leave a comment being like you know rock on Cho um or something of the type and uh apart from that though guys I will see you next time [Music] goodbye

Original Description

200 likes = 1 video per day, 300 likes = 2 videos per day!!! ------------------------------------------------------------------------------------------------- In this video: - How to implement basic artificial intelligence in a game - How to add random movement to a mob or non-player character (NPC) ------------------------------------------------------------------------------------------------- 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 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches how to implement basic artificial intelligence in a game, creating random movement for non-player characters, and utilizing tools like the random class and FPS monitoring. The lesson covers the use of Java, random number generators, and simple AI implementation.

Key Takeaways
  1. Create a variable called time and set it to zero
  2. Increment time variable in the update method
  3. Use Java's random class to randomize direction
  4. Set up random number generator to create movement
  5. Add chance for character to stop moving
  6. Make interval of time between movement changes random
💡 Using a random interval between 30 and 80 to change direction creates a simple AI that moves and stays still with a random chance, making the game environment more alive.
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →