[Tutorial]Game Engine AI Part 3: Pathfinding 1

Moderators: Yodaman921, Gabriel

[Tutorial]Game Engine AI Part 3: Pathfinding 1

Postby hydra_skillz » Sat Nov 28, 2009 12:26 pm

This is different from what I had planned for part 3, but I think it will make the actual ai implementation easier in the end. This part will be going over generating path data from a mesh. I will have the next part setting up and explaining the A* algorithm up in a few days. I made this separate since I don't think most people who use the game engine are familiar with accessing mesh data.

Basically, the vertexes will be the nodes and the edges will define valid paths. Start by making a path, you can make it any shape you want. Here is an example:

ss1.jpg
ss1.jpg (112.43 KiB) Viewed 6916 times


Split the view and open the text editor. Make a new file and name it pathfinder.py. I recomend turning on these options:

ss3.jpg
ss3.jpg (7.42 KiB) Viewed 6916 times


First we import Blender and declare a global variable nodelist and set it to None.
Code: Select all
import Blender

nodelist = None

We will start be making a class called Node. This will hold all the important information about a given vertex.
Code: Select all
class Node:
   name = ''
   co = [0,0,0]
   g_score = 0
   h_score = 0
   f_score = 0
   parent = None
   children = []

The variable name is just a string we will use for printing out the nodes and co is the coordinates of the node. I will explain the others later. Now let's define a function called gen_nodelist that takes an arguemnt ob_name.
Code: Select all
def gen_nodelist(ob_name):


What this function will be doing is getting an objects mesh, looping through it's vertexes appending Node instances to a list, then making connections between the Nodes using the mesh's edge data. Here is the code:
Code: Select all
# local list that will be returned
nodelist = []
# used for naming
x = 1
# the objects mesh
mesh = Blender.Object.Get(ob_name).getData(mesh=1)

# add Nodes to the nodelist using the mesh's verticies
# nodes will be named NodeX
for vert in mesh.verts:
   # make a new Node and initialize it's members
   node = Node()
   node.name = 'Node'+str(x)
   node.co = vert.co
   node.g_score = 0
   node.h_score = 0
   node.f_score = 0
   node.parent = None
   node.children = []
   # add the node to the list and increment the counter
   nodelist.append(node)
   x += 1

# use edge data to create links between nodes (children)
for edge in mesh.edges:
   for node in nodelist:
      # if this node is the first vert on the edge
      if node.co == edge.v1.co and edge.v2.co != node.co:
         # add all Nodes that are connected to this vert to the child list
         for n in nodelist:
            if n.co == edge.v2.co:
               node.children.append(n)
      # if this node is the second vert on the edge
      elif node.co == edge.v2.co and edge.v1.co != node.co:
         # add all Nodes that are connected to this vert to the child list
         for n in nodelist:
            if n.co == edge.v1.co:
               node.children.append(n)
# return the nodelist
return nodelist

Here we are using the children member of our Node class to hold all the Nodes that are connected to it. It is a good idea to print all this data out to verify that the code is working correctly. Add a new varibale called DEBUG to the function (make it 1) and right before the return statment add this code:
Code: Select all
if DEBUG:
   file = open('DEBUG.txt','w')
      for node in nodelist:
         nodes = []
         for n in node.children:
            nodes.append(n.name)
         file.write(node.name+' at '+str(node.co)+ ' : '+str(nodes)+'\n')

This will make a file namde DEBUG.txt in the same directory as the .blend file. We still have a little bit more to add before we can run this. Make a new file namde core.py. This script will be our main script. Its contents are simple right now:
Code: Select all
import pathfinder

def init():
   pathfinder.gen_nodelist('path')

Replace 'path' with the name of your path object. Add an empty to the scene and set it up like so:

ss2.jpg
ss2.jpg (29.08 KiB) Viewed 6916 times


You can now press p to play the game! Nothing special will happen visably, but you should get a text file with all the path data. You should verifiy that it is correct before moving on. The next part will be creating our A* (a-star) algorithm.

Here is the .blend.
Attachments
a star tutorial part 1.blend
(136.29 KiB) Downloaded 623 times
Last edited by hydra_skillz on Sat Nov 28, 2009 3:16 pm, edited 1 time in total.
GE API <- reference for game engine scripting
User avatar
hydra_skillz

Premium Member
 
Posts: 1321
Joined: Mon Dec 15, 2008 5:22 pm
Location: North Carolina

Re: Game Engine AI Part 3: Pathfinding 1

Postby Yodaman921 » Sat Nov 28, 2009 1:58 pm

Don't forget the [Tutorial] Tag.
Image
See my tutorials Here.
User avatar
Yodaman921

Site Admin
 
Posts: 2624
Joined: Sat May 17, 2008 8:49 pm
Location: University of Michigan

Re: Game Engine AI Part 3: Pathfinding 1

Postby hydra_skillz » Sat Nov 28, 2009 3:17 pm

Yodaman921 wrote:Don't forget the [Tutorial] Tag.

Thanks :oops:
GE API <- reference for game engine scripting
User avatar
hydra_skillz

Premium Member
 
Posts: 1321
Joined: Mon Dec 15, 2008 5:22 pm
Location: North Carolina

Re: [Tutorial]Game Engine AI Part 3: Pathfinding 1

Postby catchgokul » Fri Dec 04, 2009 2:02 am

awesome work
cannot wait for the next part ....
catchgokul

Member
 
Posts: 5
Joined: Sun Nov 01, 2009 1:38 pm

Re: [Tutorial]Game Engine AI Part 3: Pathfinding 1

Postby TmanThawk » Sun Sep 12, 2010 4:32 pm

Where did u learn all this programming stuff?
TmanThawk

Member
 
Posts: 21
Joined: Sun Sep 27, 2009 7:32 am

Return to User Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron