I have searched the Resource section of the forums and I couldn’t find a pathfinder that was easy to work with and allows tracking to an “enemy”. Some of them had even the motion of the AI in the script.
Is there a very easy to use pathfinder that doesn’t interfere with the rest of the object’s logic?
Something like a “path to targets” kind if thing.
blender 2.6 has an actuator which is designed for pathfinding. It is a result of the recast & detour GSoC project. The actuator is called ‘steering’ and there is now a section in UI which is for generating a navmesh. Its pretty much all straight forward. Hardest part is getting the navmesh settings right ^^ good luck
I have make a pathfinder with python , but in 2.5x anyway , so the sintax is a bit different.(so , surely there some word to change)
then the grid is make with many cubes (instead of one single mesh as now)
this is to attach to the obj seecker, and the cube must have a property “obstacle” , no other , seem
from bge import logic as BG
from bge import events
c = BG.getCurrentController()
o = c.owner
sce = BG.getCurrentScene()
key = BG.keyboard.events
FKEY= key[events.FKEY]
if FKEY==1:
######################################################################
##################---SETUP THIS--#####################################
OBSTACLE="obstacle" #choose the property must be in to object obstacle
TARGET= "target" #choose the property must be in to object target
gridLenX=20 #default is 100 (equal to 100 meter)
gridLenY=20 #default is 100 (equal to 100 meter)
direction=4 #default is 4 can change in to 8
######################################################################
######################################################################
gridLenX =round(gridLenX)
gridLenY =round(gridLenY)
direction=round(direction)
if gridLenX%2==0:gridLenX+=1
if gridLenY%2==0:gridLenY+=1
if direction!=4 and direction!=8: direction=4
playerX=round(o.worldPosition[0])
playerY=round(o.worldPosition[1])
xx=gridMinX=playerX-(gridLenX//2)
yy=gridMinY=playerY-(gridLenY//2)
gridMaxX=gridMinX+gridLenX
gridMaxY=gridMinY+gridLenY
########## INIT GRID ##############
localGrid={}
for i in range(gridLenY):
for i2 in range(gridLenX):
localGrid[(str(xx)+"|"+str(yy))]=0 ;xx+=1 ;print("localGrid",(str(xx)+"|"+str(yy)))
if xx==gridMaxX:
xx=gridMinX ;yy+=1
for i in sce.objects:
if OBSTACLE in i:
localGrid[str(round(i.worldPosition[0]))+"|"+str(round(i.worldPosition[1]))]=1
if TARGET in i:
localGrid[str(round(i.worldPosition[0]))+"|"+str(round(i.worldPosition[1]))]=3
dist=o.getDistanceTo(i)
###################################
winnerPath=0
path=[[]] ;path[0].append(list([playerX,playerY]))
compare=str(str(playerX)+"|"+str(playerY))
if dist<max(gridLenX,gridLenY) and localGrid[compare]==0:
print("PATH FIND START");print("-"*30);print("-"*30)
localGrid[compare]=1
WIP=1
nPathClosed=0
for r in range(gridLenX*gridLenY):
if WIP==0:
break
cont=len(path)
if nPathClosed>100:
print("*WARNING* START cleaner--->>>len(path)",len(path))
##cleanlist
cont=len(path) ;cont2=0
for i in range(cont):
if path[cont2]=="X":
del(path[cont2]) ;cont2+=0
else:
cont2+=1
if cont2>cont:
break
##cleanlist
print("*WARNING* after cleaner......len(path)",len(path))
nPathClosed=0
WIP=0 ;cont=len(path)
for i in range(cont):
pathN=int(i)
if winnerPath!=0:
break
if path[pathN]!="X":
lastlast=len(path[pathN])-1 ;recCell=list(path[pathN][lastlast])
foundcell=0
for i4 in range(direction):
tryPos=list(recCell)
if direction==4:
if i4==0: tryPos[1]+=1
elif i4==1: tryPos[1]-=1
elif i4==2: tryPos[0]-=1
elif i4==3: tryPos[0]+=1
elif direction==8:
if i4==0: tryPos[1]+=1
elif i4==1: tryPos[1]-=1
elif i4==2: tryPos[0]-=1
elif i4==3: tryPos[0]+=1
elif i4==4: tryPos[1]+=1 ;tryPos[0]-=1
elif i4==5: tryPos[1]-=1 ;tryPos[0]+=1
elif i4==6: tryPos[0]-=1 ;tryPos[1]-=1
elif i4==7: tryPos[0]+=1 ;tryPos[1]+=1
inRangeGrid=0
if tryPos[1]<gridMaxY and tryPos[1]>gridMinY and tryPos[0]>gridMinX and tryPos[0]<gridMaxX:
inRangeGrid=1
if inRangeGrid==0:
WIP+=1
if inRangeGrid==1:
compare=str(str(tryPos[0])+"|"+str(tryPos[1]))
if localGrid[compare]!=1:
foundcell+=1 ;WIP+=1
if localGrid[compare]==0:
localGrid[compare]=1
if foundcell>0:
if foundcell==1:
path[pathN].append(list(tryPos))
if foundcell>1:
foundcell=1 ;path.append(list(path[pathN])) ;lastlast=len(path[pathN])-1
del(path[pathN][lastlast]) ;path[pathN].append(list(tryPos))
if localGrid[compare]==3:
if foundcell>1:
foundcell=1 ;lastlast=len(path[pathN])-1
del(path[pathN][lastlast]) ;path[pathN].append(list(tryPos))
elif foundcell==1:
path[pathN].append(list(tryPos))
winnerPath=list(path[pathN])
v1=0 ;v2=len(winnerPath)-1 ;v3=list(winnerPath)
o["path"]=[v1,v2,v3] ;report=list(path)
if i4==direction-1:
if foundcell==0:
path[pathN]="X"
nPathClosed+=1
##########################
if winnerPath!=0:
print("--REPORT--") ;print() ;print() ;print("report (path) ->",path)
print("lenght patwinner ->",len(winnerPath));print("patwinner ->",winnerPath )
else: print() ;print() ;print("--PATH NOT FOUND--")
###########################
if not "path" in o:
o["path"]=0
if o["path"]!=0:
p=o["path"] ;p0=p[0] ;p1=p[1] ;p2=list(p[2])
x=p2[p0][0] ;y=p2[p0][1];z=round(o.worldPosition[2])
pathPos=[x,y,z]
vec=o.getVectTo(pathPos) ;dist=vec[0]
o.alignAxisToVect(vec[1],1,0.5) ;o.applyMovement([0,0.2,0],1)
if dist<0.5:
if o["path"][0]<o["path"][1]:
o["path"][0]+=1
elif dist<0.2:
o["path"]=0
make a plane (name it pathFinder… better) subdivide it in various faces , this is the path …
then go in physic panel and choose “Navigation mesh” (as kind of obj)
(press also the button navmesh reset index value…not sure if necessary , but better make it)
now need to 2 obj , the seeker , and the target , both must touch the pathfinder (or must be near)
to the obj seeker , put a sensor , AND and Steering as actuator
in this actuator choose “pathfinder” > give the target and the obj as pathfinder .(named pathFinder)
thats should be all
I counsill to use obj static , or no collision the first time , else can make some strange things