Unknown

From DarkAI

PATH FINDING DEMO


This demo shows a single entity working its way through a cluster of obstacles to a user defined point by calculating the shortest available path around the obstacles to its destination. The demo incorporates obstacles created with both automatic calculation, using an existing object to create a convex shape, and manual creation, to create the boundary around the outside. The smaller obstacles are randomly positioned each the demo is run to create a different scenario every time.

This document will detail the AI commands used to create and control the demo in the context they are used. For general usage of the commands you can find them in the command list section of the help file.
The demo begins with:

AI Start

AI Set Radius 2.5

This creates the AI system and sets the radius for all entities to 2.5 and all obstacles added will use this new value. It is advised that the Set Radius command be called early in the setup since using it once several obstacles have been added means the system has to spend time re-adjusting those obstacles.

make object box 111,80,10,2
position object 111,-10,5,-10

make object box 112,80,10,2
position object 112,10,5,10

rem add them to the AI system
AI Add Static Obstacle 111
AI Add Static Obstacle 112


We then create the two long horizontal walls you see in the screenshot, position them and add them to the AI system. You must position your obstacles before you add them since the Add Static command will use the object's current position for its internal use, and this cannot be changed once added. Using default values these obstacles are created as full height obstacles and added to container 0 (container 0 is created by default when the AI system is started).

for i=113 to 120

make object box i,rnd(10)+5,5,rnd(10)+5
position object i,rnd(100)-50,2.5,rnd(100)-50
yrotate object i,rnd(360)

AI Add Static Obstacle i

next i


This section creates the smaller randomly placed obstacles which are also added to the AI system. The obstacles also have a Y rotation which will be taken into account, again this must be done before calling Add Static and only the Y angle will be read.

AI Start New Obstacle 10
AI Add Obstacle Vertex -50,-50
AI Add Obstacle Vertex 50,-50
AI Add Obstacle Vertex 50,50
AI Add Obstacle Vertex -50,50
AI End New Obstacle 0,1


Next the boundary is created that prevents the entity moving beyond the edges of the floor. It is specified in an anti-clockwise direction (assuming X,Z co-ordinates) to enclose the area and make everything 'behind' it as one big obstacle that the entity will avoid. In this case we have given the obstacle an id of 10, which would only be used to remove it later and does not link it to anything with that number. More than one obstacle can have the same id in which case they would both be removed together. It has no other effect on an obstacle. We then finish the new obstacle and add it to container 0, as a full height object.

AI Complete Obstacles

We call this command to tell the AI system we have finished adding our static obstacles so that it can process them and create waypoint and visibility data all at once instead of doing it every time we add an obstacle. This can be a slow process when lots of obstacles are involved but it only needs to be performed once.


make object cone 2,5
xrotate object 2,90
fix object pivot 2
position object 2,0,2.5,40


AI Add Enemy 2
AI Set Entity Speed 2,10.0


This adds our single entity to the system, making sure to use the same value for the entity as the object since the default values link the entity to the object. We also set the entity speed to 10 units per second which, with the level being 100 units across, means it would take 10 seconds for the entity to get from one side to the other (100/10 = 10 seconds).

That completes the setup and we now enter the main loop where we use the mouse position to place the mouse marker object (object 1) on our floor. We then find:

if mouseclick()=1 then AI Entity Go To Position 2,object position x(1),object position z(1)

Which sets the entity destination to the mouse marker's position whenever the mouse is clicked. The destination will automatically be moved outside any obstacles where possible, you can see this by watching the path's final point which represents the destination. There may be a situation when the entity and its destination are completely blocked by obstacles in which case the entity will not attempt to move towards it. There are also 4 debug controls defined like this:

if keystate(2)=1 and ptimer<timer()

ptimer = timer()+300
pMode = 1-pMode

if ( pMode=0 ) then AI Debug Hide Paths else AI Debug Show Paths 2.5

endif


This one registers when the number 1 key is pressed and toggles the display of the current entity path. The timer variable prevents the display toggling too often and the mode variable switches between showing and hiding the path. This is the same format for the other 3 debug controls.

print "Entity State: ";AI Get Entity State$(2)

The Get Entity State command is used during automatic mode to get the current state that is controlling the entity. In automatic mode an entity switches between states based on what it thinks it should be doing given it current surroundings and recent events. A state can be as simple as keeping an entity en route to a destination such as in the 'Go To Destination' state, or a sequence of actions to perform an action as in the 'Search Area' state.

AI Update
sync

And finally we update the system and draw it.