Announcement

Collapse
No announcement yet.

trouble navigating around obstacles

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    trouble navigating around obstacles

    so I've been at this for weeks programming a bot that works similar to the sims. my main problem is primarily with navmesh pathing. currently if my bot sees an actor goes to it in a straight line which works fine but with the addition of obstacles it just ends up walking up against a wall if somethings in it's path and then will just give up other wise. i tried integrating mougli's follow player tutorial and a combination of random patrol but i cant seem to get it right. hopefully someone can make sense of what im doing wrong

    Code:
    /*
    Purpose:    Custom AI Controller for Practical Feng Shui./
    Authors:    Ted Pereira
    Date:       October 10, 2014
    Credit:     Mougli's portfolio -  UDK – AI Pawn movement http://www.moug-portfolio.info/udk-ai-pawn-movement/
    */
    class PFS_AITestController extends AIController;
    
    //
    var array<Actor> target;
    var array<bool> beenHere;
    var() Vector TempDest;
    var vector FinalDest;
    var int counter;
    var int currentcount;
    var int destCount;
    var bool pointFound;
    var bool Reached;
    var float distanceToNode;
    var float nextToNode;
    var float perceptionDistance;
    var array<Actor> Points;
    
    var Vector hitLoc, hitNormal;
    
    simulated function PostBeginPlay()
    {
    	local PFS_Furniture A;
    	super.PostBeginPlay();
    	
    
    	foreach WorldInfo.AllActors(class'PFS_Furniture',A)
    	{
    		Points.AddItem(A);
    	}
    }
    event Possess(Pawn inPawn, bool bVehicleTransition)
    {	
    	//local InterpActor A;
    	
       super.Possess(inPawn, bVehicleTransition);
       Pawn.SetMovementPhysics();
       
    } 
    
    auto state Search
    {
    	function bool FindClosest()
    	{
    		local PFS_Furniture A;
    		local int i;
    		foreach AllActors(class'PFS_Furniture',A)
    		{   
    			
    			currentcount = counter;
    			counter++;
    			distanceToNode = VSize( Points[currentcount].Location - Pawn.Location);
    			if(destCount >= Points.Length)
    			{
    				destCount = 0;
    				for(i = 0; i < beenHere.Length; i++)
    				{
    					beenHere[i] = false;
    				}
    			}
    			if(distanceToNode  < perceptionDistance && !beenHere[currentcount])
    			{
    				beenHere[currentcount] = true;
    				MoveTarget = Points[currentcount];
    				Worldinfo.Game.Broadcast(self, "Found Furniture");
    				pointFound = true;
    				destCount++;
    				break;
    			}
    			if(counter >= Points.Length)
    			{
    				counter = 0;
    				
    			}
    		}
    		
    		return pointFound;
    	}
    Begin:
    	if(!FindClosest())
    		GotoState('PatrolNavMesh');
    	else
    		GotoState('MoveToSpot');
    }
    state MoveToSpot
    {
    	function bool FindNavMeshPath()
    	{
    		// Clear cache and constraints 
    		NavigationHandle.PathConstraintList = none;
    		NavigationHandle.PathGoalList = none;
    		NavigationHandle.bDebugConstraintsAndGoalEvals = true;
    		
    		// Create constraints
            class'NavMeshGoal_At'.static.AtActor( NavigationHandle, MoveTarget,32 );
    		//class'NavMeshPath_Toward'.static.TowardGoal( NavigationHandle,MoveTarget );
    		class'NavMeshPath_EnforceTwoWayEdges'.static.EnforceTwoWayEdges(NavigationHandle);
    		
    		// Find path
    		return NavigationHandle.FindPath();
    	}
    Begin:
    	if( NavigationHandle.ActorReachable( Trace(hitLoc,hitNormal,MoveTarget.Location,Pawn.Location)) )
        {
           
    		DrawDebugLine(Pawn.Location,MoveTarget.Location,255,0,0,true); 
    		DrawDebugSphere(MoveTarget.Location,16,20,255,0,0,true);
            //Direct move
            MoveToward(Trace(hitLoc,hitNormal,MoveTarget.Location,Pawn.Location),Trace(hitLoc,hitNormal,MoveTarget.Location,Pawn.Location) );
            FlushPersistentDebugLines();
    		Worldinfo.Game.Broadcast(self, "Arrived at Furniture, going back to patrol");
    		Sleep(1);
    		pointFound = false;
    		GotoState('PatrolNavMesh');
        }
    	else if(FindNavMeshPath())
    	{
    		NavigationHandle.SetFinalDestination(NavigationHandle.PathCache_GetGoalPoint());
    
    		NavigationHandle.DrawPathCache(,TRUE);
    
    		FinalDest = NavigationHandle.FinalDestination.Position;
    
    		DrawDebugLine(Pawn.Location,FinalDest,255,0,0,true); 
    		DrawDebugSphere(FinalDest,16,20,255,0,0,true);
    
    		MoveTo(FinalDest,MoveTarget);
    		FlushPersistentDebugLines();
    
    		Worldinfo.Game.Broadcast(self, "Arrived");
    		Sleep(1);
    	}
    	else
    	{
    		Worldinfo.Game.Broadcast(self, "Can't reach Furniture, going back to patrol");
    		Sleep(1);
    		GotoState('PatrolNavMesh');
    	}
    	goto'Begin';
    }
    state PatrolNavMesh
    {
    	// If we see a player or pawn, ignore it
    	function bool FindNavMeshPath()
    	{
    		 WorldInfo.Game.Broadcast(self,"Roaming");
    		// Clear cache and constraints 
    		NavigationHandle.PathConstraintList = none;
    		NavigationHandle.PathGoalList = none;
    		NavigationHandle.bDebugConstraintsAndGoalEvals = true;
    		
    		/** this makes sure the bot wont wander into an area 
    		where it will get stuck */
    		
    		
    		// set his goal. 
    		class'NavMeshGoal_Random'.static.FindRandom(NavigationHandle);
    		class'NavMeshPath_EnforceTwoWayEdges'.static.EnforceTwoWayEdges(NavigationHandle);
    		//class'NavMeshPath_Toward'.static.TowardGoal( NavigationHandle,MoveTarget );
    		// Find path
    		return NavigationHandle.FindPath();
    	}
    Begin:
    		if(FindNavMeshPath())
    		{
    			
    
    			FlushPersistentDebugLines();
    			FinalDest = NavigationHandle.FinalDestination.Position;
    			NavigationHandle.SetFinalDestination(NavigationHandle.PathCache_GetGoalPoint());
    			NavigationHandle.DrawPathCache(,TRUE); 
    
    			if( NavigationHandle.GetNextMoveLocation( FinalDest, Pawn.GetCollisionRadius()) )
    			{
    			    DrawDebugLine(Pawn.Location,FinalDest,255,0,0,true); //These two lines handle drawing the red line and sphere for debugging purposes
    			    DrawDebugSphere(FinalDest,16,20,255,0,0,true);
    			    
    			    MoveTo(FinalDest);
    				FlushPersistentDebugLines();
    			}
    			Worldinfo.Game.Broadcast(self, "Reevaluating");
    			Sleep(0.5);
    			GotoState('Search');
    		}
    		goto'Begin';
    		
     }
    
     
    DefaultProperties
    {
    	currentcount = 0
    	counter = 0
    	perceptionDistance = 1200
    	pointFound = false
    	destCount = 0;
    	Reached = false
    }

    #2
    I would suggest that you rebuild (atleast rebuild bot paths) the map using your code and then test your code using the UNcooked version of the map. It sounds like the maps bot path network maybe broken. Also if you are trying to add a new actor to a existing path network, this can NOT be done dynamicly (at runtime). You would have to, add the actor, to the path network during map creation and then rebuild the map to update the paths.

    Comment

    Working...
    X