ai part 1
DESCRIPTION
Steering behaviors and pathfinding, by Dan SosnowskiTRANSCRIPT
INTRO TO INTELLIGENT AND AUTONOMOUS AGENTS
Steering behaviors and pathfinding
Game AI
What we’re covering today And what we’re not
How AI can improve your games Autonomous agents
Steering Behaviors
What they are When they’re used
Quite often http://www.youtube.com/watch?v=e2YYt
SJhmJg Movies
Let’s try some out!
Seek
Move entity from current position to target position as quickly as possible
Our entity is the triangle. How do we get to the target?
Desired velocity = target.position - myPosition
You can get a vector pointing from A to B by B - A
Don’t worry about overshooting, it’ll be corrected
Other steering behaviors
Arrive Obstacle Avoidance
Make sure we don’t have a force that’s too big!
Using SBs in your Entities ChasePlayer State
Obstacle avoidance, Pursue or Seek Idle State
Wander, maybe flocking?
Sheep Flocking, Wander, Obstacle Avoidance,
Evade
Steering behaviors ftw :?
Wouldn’t this be lovely? Well then, let’s find a path
A graph of points! (navigation graph)
Building a Nav Graph
Goal: A list of nodes and edges What is needed:
Start point, cast distance, interval (optional)Max number of nodes
GraphNode
Vector3 positionGraphEdge[4] edges
Data Structures
GraphEdge
GraphNode fromNodeGraphFrom toNode(Float weight)
List<GraphNodes> Nodes
For ( i = 1; i <= xCastDistance; ++i)For (j = 1; j <= zCastDistance; ++j){
cast a ray downward from above for each potential neighbor node(i.e. (curX + invertal, curZ), (curX, curZ + interval), etc.)if raycast hit the ground{ new GraphNode(GraphNode(Raycast hit point)) if we haven’t already found this node { Add new graph node to Nodes }
(calculate edge cost) new GraphEdge(current node, new graph node, cost)) Add new graph edge to the current node’s Edge list}
Start from initial position, start casting out!
What happens when node cast hits out of bounds?
New position
Nothing new, just adding another node/edge
:o A box!Hit an object not tagged ground, skip it, keep moving
:OOOOO But we already processed that node!Result depends on algorithm implementation. Easy route (the way the pseudo code was setup) is to have each node responsible for itself. So in our case add the edge and ignore the node.
Improvements Worth Mentioning
Reducing node density Indexing GraphNodes and
GraphEdges
GraphNode
int indexVector3 positionGraphEdge[4] mEdges
Data Structures for Indexing
GraphEdge
int fromNodeIndexint toNodeIndexfloat weight
Instead of storing the GraphNodes that a GraphEdge is connected to, westore the index of each GraphNode, so we’re not storing a GraphNode twice
Take a look at that graph
Search Algorithms
Depth-first search Breadth-first search Dijkstra’s algorithm A* (aka Dijkstra++)
^ yahtzee
Dijkstra’s Algorithm
Usable with weighted graphs. Graphs without weight can be processed by assuming edges of equal weight
Guaranteed to find shortest path, if it exists.
Implementation Examine nodes on our search frontier,
find the one with the smallest total weight, and add it to our path and keep going
Picture from “Programming Game AI by Example” by Mat Buckland
Picture from “Programming Game AI by Example” by Mat Buckland
A*
Dijkstra with an additional weight factor (heuristic) Heuristic: Making a decision based on some
knowledge
There’s something troublesome in the Dijsktra implementation that seems like it would be an easy fix
We know where our target is, so let’s approximate the cost from the nodes we are looking at to the target and factor that into what we choose
In this case, Euclidean distance
Manhattan Distance
ResultsPicture from “Programming Game AI by Example” by Mat Buckland
Picture from “Programming Game AI by Example” by Mat Buckland
Review
Steering behaviors Navigation graphs Search algorithms
So how do they work together? You have a point on the graph you want to
reach. You find a sequence of nodes to follow. Use steering behaviors to get from point to
point, and create interesting behavior on the way
Next time…
Organizing these lower level decisions into functional, autonomous agents (in a smart, extendable, debug-friendly way )
Questions?
(can someone let Jordan know he can wake up now? kthx)