Practical Game AI Programming
eBook - ePub

Practical Game AI Programming

  1. 348 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Practical Game AI Programming

About this book

Jump into the world of Game AI developmentAbout This Book• Move beyond using libraries to create smart game AI, and create your own AI projects from scratch• Implement the latest algorithms for AI development and in-game interaction• Customize your existing game AI and make it better and more efficient to improve your overall game performanceWho This Book Is ForThis book is for game developers with a basic knowledge of game development techniques and some basic programming techniques in C# or C++.What You Will Learn• Get to know the basics of how to create different AI for different type of games• Know what to do when something interferes with the AI choices and how the AI should behave if that happens• Plan the interaction between the AI character and the environment using Smart Zones or Triggering Events• Use animations correctly, blending one animation into another and rather than stopping one animation and starting another• Calculate the best options for the AI to move using Pruning Strategies, Wall Distances, Map Preprocess Implementation, and Forced Neighbours• Create Theta algorithms to the AI to find short and realistic looking paths• Add many characters into the same scene and make them behave like a realistic crowdIn DetailThe book starts with the basics examples of AI for different game genres and directly jumps into defining the probabilities and possibilities of the AI character to determine character movement. Next, you'll learn how AI characters should behave within the environment created.Moving on, you'll explore how to work with animations. You'll also plan and create pruning strategies, and create Theta algorithms to find short and realistic looking game paths. Next, you'll learn how the AI should behave when there is a lot of characters in the same scene.You'll explore which methods and algorithms, such as possibility maps, Forward Chaining Plan, Rete Algorithm, Pruning Strategies, Wall Distances, and Map Preprocess Implementation should be used on different occasions. You'll discover how to overcome some limitations, and how to deliver a better experience to the player. By the end of the book, you think differently about AI.Style and approachThe book has a step-by-step tutorial style approach. The algorithms are explained by implementing them in #.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Navigation Behavior and Pathfinding

In this chapter, we'll be explaining in detail how the AI character moves around and understands where he can go and where he cannot. For different types of games, there are different solutions and we'll be addressing those solutions in this chapter, exploring common methods that can be used to develop a character that can move correctly on the map. Also we want our character to calculate the best trajectory to arrive at a certain destination, avoiding obstacles and accomplishing goals while doing it. We will introduce how to create a simple navigation behavior, then we will move on to a point to point movement and finally explore in depth how to create a more complex point to point movement (RTS/RPG system).

Navigation behavior

When we talk about navigation behavior, we are referring to the actions of a character that is confronted with a situation where they need to calculate where to go or what to do. A map can have many points where it is necessary to jump or climb stairs in order to arrive at the final destination. The character should know how to use these actions to keep moving correctly; otherwise, he will fall down a hole or keep walking into a wall where he should be climbing some stairs. To avoid that, we need to plan all of the possibilities available for the character while he is moving, making sure that he can jump or perform any other movement necessary to keep moving into the right direction.

Choosing a new direction

One important aspect that the AI character should have is choosing a new direction when he is confronted by an object that is blocking his way and that he cannot pass through. The character should be aware of the objects that are in front of him and, if he cannot keep moving forward in that direction, he should be able to choose a new direction, avoid colliding against the object and keep walking away from it.

Avoid walking against walls

If our character is facing a wall, he will need to know that he cannot pass through that wall and should choose another option. Unless we allow the character to climb the wall or destroy it, the character will need to face a new direction that is not blocked and walk in that new unblocked direction.
We will start with a simple approach that i can be often very useful, and is perhaps the best option, depending on the type of game that we are creating. In the example that we'll be demonstrating, the character in question needs to keep moving around the level just like the Pac-Man enemies. Starting with a basic example, we give our character the freedom to choose which direction to move in, and later on we will add more information to our character's AI so he can pursue a specific objective on the map using this method.
We have created a grid and painted in black the squares where the character AI is not allowed to walk. Now we are going to program our character to move forward until he finds a black square in front of him; then, he will need to choose to turn right or left, making that decision randomly. This will allow our character to move freely on the map without any specific pattern. The code for this is as follows:
 public float Speed;
public float facingLeft;
public float facingRight;
public float facingBack;
public static bool availableLeft;
public static bool availableRight;

public bool aLeft;
public bool aRight;

void Start ()
{

}

void Update ()
{

aLeft = availableLeft;
aRight = availableRight;

transform.Translate(Vector2.up * Time.deltaTime * Speed);

if(facingLeft > 270)
{
facingLeft = 0;
}

if(facingRight < -270)
{
facingRight = 0;
}

}

void OnTriggerEnter2D(Collider2D other)
{

if(other.gameObject.tag == "BlackCube")
{
if(availableLeft == true && availableRight == false)
{
turnLeft();
}

if(availableRight == true && availableLeft == false)
{
turnRight();
}

if(availableRight == true && availableLeft == true)
{
turnRight();
}

if(availableRight == false && availableLeft == false)
{
turnBack();
}
}
}

void turnLeft ()
{
facingLeft = transform.rotation.eulerAngles.z + 90;
transform.localRotation = Quaternion.Euler(0, 0, facingLeft);
}

void turnRight ()
{
facingRight = transform.rotation.eulerAngles.z - 90;
transform.localRotation = Quaternion.Euler(0, 0, facingRight);
}

void turnBack ()
{
facingBack = transform.rotation.eulerAngles.z + 180;
transform.localRotation = Quaternion.Euler(0, 0, facingBack);
}
For this example, we added colliders to the black squares to let the character know when he is touching them. This way, he will keep moving until colliding with a black square, and at that point there will be three options: turn left, turn right, or go back. To know which directions are unblocked, we created two separate colliders and added them to our character. Each collider has a script that gives the information to the character to let it know whether that side is free or not.
The availableLeft Boolean corresponds to the left side, and availableRight corresponds to the right side. If the left or right collider is in contact with the black square, the value is set to false. Otherwise, it is set to true. We are using aLeft and aRight simply to check in real time if the values are working correctly. This way, we can see whether there are any issues:
 public bool leftSide; public bool rightSide; void Start () 
{ if(leftSide == true)
{ rightSide = false; } if(rightSide == true)
{ leftSide = false; } } void Update () { } void OnTriggerStay2D(Collider2D other) { if(other.gameObject.tag == "BlackCube") { if(leftSide == true && rightSide == false) { Character.availableLeft = false; } if(rightSide == true && leftSide == false) { Character.availableRight = false; } } } void OnTriggerExit2D(Collider2D other) { if(other.gameObject.tag == "BlackCube") { if(leftSide == true) { Character.availableLeft = true; } if(rightSide == true) { Character.availableRight = true; } } }
When we start the game, we can see that the character AI starts moving around on the white tiles and turning left or right every time he faces a black tile:
But...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Author
  5. About the Reviewer
  6. www.PacktPub.com
  7. Customer Feedback
  8. Preface
  9. Different Problems Require Different Solutions
  10. Possibility and Probability Maps
  11. Production System
  12. Environment and AI
  13. Animation Behaviors
  14. Navigation Behavior and Pathfinding
  15. Advanced Pathfinding
  16. Crowd Interactions
  17. AI Planning and Collision Avoidance
  18. Awareness

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Practical Game AI Programming by Micael DaGraca in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Games. We have over one million books available in our catalogue for you to explore.