57. Broad Phase
Determine which things the ray might collide with.
Narrow Phase
Test the ray against each possible thing to determine
What it hits and where
Welcome Everybody…This session is about Bringing Life Into your Games My name is Dave Geurts and my Twitter handle is sims11tz
We have been asked to encourage all of you to tweet about what you are learning and having fun.. So pull out those cell phones and tweet away The hash tag for the conference is #UtahDigiConI’m going to start off by tweeting a pic of all you
We are going to have pop quizes throughout the session and we will keep score… you can win prizes so pay attention!!!!!!
Really quick intro for myself… - I started in the Games Industry when I was in highschool working at Access Software and Microsoft doing QA and Photoshop work on I then learned Flash and did some games for mtv.com back in 99-2000 and some other Marketing flash games for Books and CompaniesThen I moved to Waterford Institute and worked on Educational Games for young children
Now I work for a Game Studio called Smart Bomb Interactive
We have a browser flash kids MMO called AnimalJam
We also released an Ios / Android Game called TunnelTown that reached the top 10 in the ios charts for a while
We also released a game called AjJump for IOS and Android that also briefly reached the top 3 games for Ipad
Explain the Quiz and the scoring systemLoad Quiz #1
Ok so lets get started into good stuff - We are gonna talk about how to bring life into your games - Games would be super boring without any type of Artificial Intelligence or Smart Enemies in your gamesWe are gonna learn about Pathfinding which is how enemies chase you down in games or navigate levels to do tasksWe will also learn about RayCasting which is one way you can make your Enemies have eyes and see things and can also be used to Shoot bullets out of guns in your game.
First of we are gonna talk about PathFinding Some examples of pathfinding would be -Enemies in a Zelda Temple running around finding their through a room trying to kill you-Enemies in a 3d space like in Minecraft where the Zombies, Creepers and Skeletons run around trying to find you and kill you-Or even in a 2d side scroller like Terraria where the different baddies try to find and hurt you-This concept can also be used for other things then enemies like in Star Craft when you select troops and Command them to attack yourEnemies base,… they have to be able to find the best way to run around obstacles and find their way to the destination of your command.Pathfinding is in almost all games and GAMES would be boring without it
One of the most common basic and effective Path finding algorithms is called A StarYou have a level in your gameYou have a hero and they need to get from Point A to Point BThe first thing in A* , is you need to break your game up into a grid system which we call our Search AreaYou don’t have to show the grid to your players visually and you don’t have to do squares, you can use any shape.
Our Search Area Grid system is made up of squares that we call nodes-Nodes need property that says if they are Walkable – Nodes that blocks on them are not walkable-To make your levels more fun, You can also add nodes that have things like sand on them or water or Tall Grass that are walkable but take longer to walk across
The next thing we need to learn about in A* is that you have List’s an Open List and a Closed List - These lists are basically Arrays that we use to keep track of Nodes we want to use in our path or nodes we don’t want to walk on. Kind of like a shopping list of nodes we are considering moving our character on. - The open list are Nodes that we are currently considering as nodes that will be in our path. - The closed list are Nodes that we have made a finalized decision on for better or worse
The next concept that we need to know is how to Calculate an F score for a Node The magic algorithm to find an F score is F = G + HSimply put G = the movement cost to move from the Starting point to the Node in questionH = the distance from the Node in question to the Finish Point - the easiest way to find H is count the number of horizontal and vertical moves it takes to get the finish point
Like I was saying G is the movement cost to move from the Starting point to the Node in questionThe starting point is the green square that has a score of 1We then move to the Node in question which is pink … Its super easy… 1 + 2 + 4 = 7So the G score for the Pink Node is 7----We will talk more later about to initally calculate the G Score
The H score is a bit more difficult to calculate and there a lot of different ways to calculate it. Some more accurate then others. First off The H is in the opposite direction of the G… - The G was from the Starting point to the target Node… - the H is from the pink target Node to the Yellow Destination Node We will use the Manhattan Method , Which is very simple and accurate enough The Manhattan Method is counting the number of Horizontal/Vertical only moves it takes to get from the target Node to the Destination Node and then multiply by 10 So basically if you count from the Pink Target node you must move left 3 nodes and up 2 nodes to get the Yellow Destination.So our count is 5 we then multiply our count by 10 and we have your H score of 50H ignores non walkable zones
The entire recipe for A* is Breaking your level up into a search area that is made up of nodes that are walkable and nodes that are not walkableNext you need an open and closed listThen you need to be able to caclulate an F scoreRise and RepeatOk lets look at an example
Okay now that we know all of the concepts (open lists/close lists… F = G + H) we can find a path from 1 Node to another Node in any level !Lets walk through this with an example. We are going to find the shortest path from the Green Starting Node to the Yellow destination Node for our hero.
First We add our Starting Node to the Open List and set it as the Target Node and then we enter the Recursive A* loop A recursive loop is a section of code or a function that calls itself over and over until it is done processing its algorithm
After we have a Target Node We need to look at all off the Target Nodes Neighbors and calculate an F Score for each of them So lets walk through calculating all of the F scores in detail
First we calculate all of the G Scores of the starting points Neighbors We didn’t previously learn what the definition of a G score is we charge 10 movement points for a Horizontal/Vertical Moveand we charge 14 points for a Vertical Movement as walking to a vertical Node is a little bit Further distanceEarlier I mentioned that you could also have Nodes like Long grass or Sand that are slow to walk on and you would give them a high G score like 20
Next we calculate all of the H – hueristic scores for all the starting nodes neighbors - Like we learned earlier we count the number of horizontal+vericle moves it takes to get the the Destination Node - We count non walkable nodes just as walkable nodes - And then we multiply the count by 10
- Now that we have the G and H scores for all of the neighbors we can easily calculate their F scores
Here are all of the neighbors to the Starting Node and all of their F ScoresSo the next step is that we find the neighbor with the lowest F Score
The neighbor with the lowest f score is Node N2
Next we find all of the Node 2’s neighbors that are walkable and not already in the open list and not in the closed listWe add n10 and n11 to our open listYou will notice to the upper left diagnol there is a white walkable node but we don’t add this because in our game we don’t allow youTo walk through block nodes even while going diagnol
So we start with N10 we need to calculate the G score… this is all previous F scores to get to here added together plus 10 for a Vertical moveSo the G score for N10 is 44
Now we get N10’s H score… so we count 2 moves to the left (Remember we go through non walkable blocks for H Score)
Sooooooo 44 + 30 = 74 so N10’s F score is 74
Now we get the G Score for the next neighbor N11So 34 + 14 for a diagonal move is 48
Then we get the H score for N11 so 3 moves to the left and 1 down… yet again for the H score we go through non walkable nodes
So 40 + 48 = 88…. So the F Score for n11 is 88
Next we select the neighbor to our Target Node (N2) with the lowest F ScoreSo We make n5 the Current Target node and add it to the closed listTHIS is the point we recursively call our logic again… so now we go through the whole process again with our updated open and closed lists and N5 as our Target Node
Next we find all of the Target Node 5’s neighbors that are walkable and not already in the open list and not in the closed listThese are Node 12 and Node 13 so we add nodes 12 and 13 to the open list
First we look at N12…So we add 40 from N5 and then add 10 for a horizontal moveSo the G score for n12 is 50
For the H score We got left 1 node and up 1 node and multiply by 10So the H score for n12 is 20
We add 50 and 20 together and get 70 for n12’s F Score
Next we look at n13We add 40 form n5 + 54 from n7 and add 10 for a horizontal move we get 104 for the G Score
We go left one and up 2 multiplied by 10 And we get 30 for the h score for n13
So we add 104 + 30 and get 134 for the F score for Node 13
Next we select the neighbor with the lowest F score and make it the Target Node!!!!That would be Node 12 with an F score of 70So we make Node 12 the next target Node and add it to the closed lsit
Now we enter the recursive loop again and do all of the logic all over… Don’t worry we are almost there!!!We get all of the Target nodes walkable neighbors that are not in the open list and not in the closed list which is n14 and n15We cant walk directly to the destination Node by going upper left because it has a block in its diagonal
So the G for n14 is 40 from n5 + 70fomr n12 + 10 for 1 horizontal movementAnd we get G Score of 120
The h is easy… 1 veritcal movement mulitplied by 10
So 120 + 10 = 130 so n14 has an F score of 130
So we add 40 from n5 and 70 from n12 and add 14 for a diagonal movement and getA g score of 124 for n15
Then for the h it is 2 up moves multiplied by 10 for an H score of 20
So 124 + 20 = an F score of 144 for n15
So now we make the neighbor with the lowest F score the Target Node which is n14Now we check all the neighbors and one of them is our destination node!!!!!!
We have the shortest possible path from one Node to another !!!!!!!!!!!!!!!!!!!!!!!!Woooooooooohoooooooooooooo
Next we are going to learn about a concept called Raycasting it is essentially drawing a straight line or shooting out a ray from 1 point and when it collides with something it stops and tells your code what it collided with you can do a lot of fun things with this you can shoot a ray out of shotgun and when it hits something that’s where the bullets hit you can shoot a ray out of your hero like in terraria and when it hits terrain that is where your hookshot sticks in you can shoot a ray out of enemies and when they hit the hero they fire!!! Like the turrets in portalOr you could use them to detect how far away an enemy is from a hero so it knows how far to jump and things like that
So you will see the green circle is an enemy in our game and the blue are walls… if you we shoot 2 rays out and slightly offset angles we get a realistic field of vision that the human eyes would see- This makes it so that we could sneak up on an enemy in a game as their 2 ray casts wouldn’t hit the hero if they go from behind
Raycasting. broad phase: determine which things (tiles and objects) the ray might collide with. narrow phase: test the ray against each thing to determine collision information, such as the point of intersection
So we have a level…. And we have to break our level up into a grid of nodes called a Search Area just like in path findingOur Enemy is the blue dot and the green squares are walls in our levelHe is trying to scan the level for the hero so he can path find and kill him!
So our enemy is a robot and he uses his eyes and looks in a direction… to do this we shoot a ray out
It seems like the simplest thing to do would be to test for collisions every x amount of pixels but this has issues you cant look at all measurements asIt would run very slow and if you can see if we don’t have our checks close enough together the pink circles miss the green squares sometimesSo this would either be accurate and VERY slow to process or quick and not accurate
So we first use a Broad phase search… we detect all of the nodes that our line hits, this is easy because we know the width and height of a nodeSo we can easily determine all the nodes we touch and still process it fast enough
Next we do the narrow phase…. Where we do collision detection on all of the broad phase nodes against all of the walls this is a very quick operation