Static ObstaclesΒΆ

STATIC OBSTACLES:

https://www.youtube.com/watch?v=mn2kX0C8XdM


In PandAI, use it via:

addStaticObstacle(NodePath obstacle);

The code for this tutorial:

  1# This tutorial provides an example of creating a character and having it walk
  2# around using PandAI pathfinding with static obstacle avoidance
  3
  4from direct.showbase.ShowBase import ShowBase
  5from panda3d.core import *
  6from direct.showbase.DirectObject import DirectObject
  7from direct.interval.IntervalGlobal import *
  8from direct.task import Task
  9from direct.actor.Actor import Actor
 10import sys
 11import os
 12from direct.gui.DirectGui import *
 13from direct.gui.OnscreenText import OnscreenText
 14
 15from panda3d.ai import *
 16
 17base = ShowBase()
 18
 19speed = 0.75
 20
 21# Figure out what directory this program is in.
 22MYDIR = os.path.abspath(sys.path[0])
 23MYDIR = Filename.fromOsSpecific(MYDIR).getFullpath()
 24
 25font = loader.loadFont("cmss12")
 26
 27
 28# Function to put instructions on the screen.
 29def addInstructions(pos, msg):
 30    return OnscreenText(text=msg, style=1, fg=(1, 1, 1, 1), font=font,
 31                        pos=(-1.3, pos), align=TextNode.ALeft, scale=.05)
 32
 33
 34# Function to put title on the screen.
 35def addTitle(text):
 36    return OnscreenText(text=text, style=1, fg=(1, 1, 1, 1), font=font,
 37                        pos=(1.3, -0.95), align=TextNode.ARight, scale=.07)
 38
 39
 40class World(DirectObject):
 41
 42    def __init__(self):
 43
 44        self.keyMap = {"left": 0, "right": 0, "up": 0, "down": 0}
 45
 46        addTitle("Pandai Tutorial: Adding Dynamic Obstacles")
 47        addInstructions(0.95, "[ESC]: Quit")
 48        addInstructions(0.90, "[Enter]: Start Pathfinding")
 49        addInstructions(0.85, "[Arrow Keys]: Move Arrow")
 50        addInstructions(0.80, "[1]: Small box")
 51        addInstructions(0.75, "[2]: Big box")
 52        addInstructions(0.70, "[Space]: Place box")
 53
 54        #base.disableMouse()
 55        base.cam.setPosHpr(0, -210, 135, 0, 327, 0)
 56        self.box = 0
 57        self.pointer_move = False
 58
 59        self.loadModels()
 60        self.setAI()
 61
 62    def loadModels(self):
 63
 64        self.environ1 = loader.loadModel("models/skydome")
 65        self.environ1.reparentTo(render)
 66        self.environ1.setScale(1)
 67
 68        self.environ2 = loader.loadModel("models/skydome")
 69        self.environ2.reparentTo(render)
 70        self.environ2.setP(180)
 71        self.environ2.setH(270)
 72        self.environ2.setScale(1)
 73
 74        self.environ = loader.loadModel("models/groundPlane")
 75        self.environ.reparentTo(render)
 76
 77        # Create the main character, Ralph
 78
 79        #ralphStartPos = self.environ.find("**/start_point").getPos()
 80        ralphStartPos = Vec3(-51, -64, 0)
 81        self.ralph = Actor("models/ralph",
 82                           {"run": "models/ralph-run",
 83                            "walk": "models/ralph-walk"})
 84        self.ralph.reparentTo(render)
 85        self.ralph.setScale(2)
 86        self.ralph.setPos(ralphStartPos)
 87
 88        self.pointer = loader.loadModel("models/arrow")
 89        self.pointer.setColor(1, 0, 0)
 90        self.pointer.setPos(60, -60, 0)
 91        self.pointer.setScale(3)
 92        self.pointer.reparentTo(render)
 93
 94    def setAI(self):
 95        # Creating AI World
 96        self.AIworld = AIWorld(render)
 97
 98        self.accept("enter", self.setMove)
 99        self.accept("1", self.addBlock)
100        self.accept("2", self.addBigBlock)
101        self.accept("space", self.addStaticObstacle)
102
103        # Movement
104        self.accept("arrow_left", self.setKey, ["left", 1])
105        self.accept("arrow_right", self.setKey, ["right", 1])
106        self.accept("arrow_up", self.setKey, ["up", 1])
107        self.accept("arrow_down", self.setKey, ["down", 1])
108        self.accept("arrow_left-up", self.setKey, ["left", 0])
109        self.accept("arrow_right-up", self.setKey, ["right", 0])
110        self.accept("arrow_up-up", self.setKey, ["up", 0])
111        self.accept("arrow_down-up", self.setKey, ["down", 0])
112
113        self.AIchar = AICharacter("ralph", self.ralph, 60, 0.05, 15)
114        self.AIworld.addAiChar(self.AIchar)
115        self.AIbehaviors = self.AIchar.getAiBehaviors()
116
117        self.AIbehaviors.initPathFind("models/navmesh.csv")
118
119        # AI World update
120        taskMgr.add(self.AIUpdate, "AIUpdate")
121
122        # Movement task
123        taskMgr.add(self.Mover, "Mover")
124
125        self.dirnlight1 = DirectionalLight("dirn_light1")
126        self.dirnlight1.setColor((1.0, 1.0, 1.0, 1.0))
127        self.dirnlightnode1 = render.attachNewNode(self.dirnlight1)
128        self.dirnlightnode1.setHpr(0, 317, 0)
129        render.setLight(self.dirnlightnode1)
130
131    def setMove(self):
132        self.AIbehaviors.pathFindTo(self.pointer)
133        self.ralph.loop("run")
134
135    def addBlock(self):
136        self.pointer_move = True
137        self.box = loader.loadModel("models/box")
138        self.box.setPos(0, -60, 0)
139        self.box.setScale(1)
140        self.box.reparentTo(render)
141
142    def addBigBlock(self):
143        self.pointer_move = True
144        self.box = loader.loadModel("models/box")
145        self.box.setPos(0, -60, 0)
146        self.box.setScale(2)
147        self.box.setColor(1, 1, 0)
148        self.box.reparentTo(render)
149
150    def addStaticObstacle(self):
151        if self.box != 0:
152            self.AIbehaviors.addStaticObstacle(self.box)
153            self.box = 0
154            self.pointer_move = False
155
156    # To update the AIWorld
157    def AIUpdate(self, task):
158        self.AIworld.update()
159        #if self.AIbehaviors.behaviorStatus("pathfollow") == "done":
160        #    self.ralph.stop("run")
161        #    self.ralph.pose("walk", 0)
162
163        return Task.cont
164
165    def setKey(self, key, value):
166        self.keyMap[key] = value
167
168    def Mover(self, task):
169        startPos = self.pointer.getPos()
170        if self.keyMap["left"] != 0:
171            self.pointer.setPos(startPos + Point3(-speed, 0, 0))
172        if self.keyMap["right"] != 0:
173            self.pointer.setPos(startPos + Point3(speed, 0, 0))
174        if self.keyMap["up"] != 0:
175            self.pointer.setPos(startPos + Point3(0, speed, 0))
176        if self.keyMap["down"] != 0:
177            self.pointer.setPos(startPos + Point3(0, -speed, 0))
178
179        if self.pointer_move is True and self.box != 0:
180            self.box.setPos(self.pointer.getPos())
181
182        return Task.cont
183
184
185w = World()
186base.run()

The full working demo can be downloaded at :

https://sites.google.com/site/etcpandai/documentation/pathfinding/StaticObstacleDemo.zip?attredirects=0&d=1