Game Development Community

Pathfinding

by Chad Freseman · in RTS Starter Kit · 08/12/2006 (12:22 am) · 9 replies

Hello,

I'm trying add a pathfingding system to the existing RTS SK demo. I found a usefull resource here but I'm having difficulties putting it to work.
(the resource im trying to use: http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=10603 )

I'm a C# coder but I'm just getting started with Torque. The problem is I can't fully understand how units move.

I found some movement related functions but but It's so hard to keep track of things. Maybe it's because I'm used to VisualStudio where you can easily navigate to the place where a function or class is defined.
I found so far inputHandler.cs and commands.cs to contain movent code. Also pathManger.cs has an empty definition for getNextWaypoint().

Can someone familiar with unit movement/pathfinding point the places where I should focus my attention?

Thanks.

#1
08/14/2006 (12:40 pm)
In the engine it has the function setMoveDestination or in processtick in the rtsmove profile. I have reworked my function a bit to get rid of that nasty animation jump when you reissue a command but basically it has a enum in the rtsunit class (mMoveState) which stores its state like stop, move and stuck. Also it has the variable mMoveDestination which is its heading and then all the magic happens in the profile RTSUnit_Move in the function process tick. All in RTSUnit.cc
#2
08/17/2006 (1:12 am)
Aha! Hello Chad!

Another C# lover, eh? :D

Personally I think the RTS-SK move-code is confusing... But hey.

Here's how it works:

* You Right-click on the terrain with units selected.

* The Point is stored, and getNextWaypoint is called with the point as the first argument. SCRIPT

* getNextWaypoint returns the same point, which is sent to the C++ setDestination.

* Every tick the C++ moves the unit x amount towards the point.

The units have no collision, so, it doesn't work perfectly.

All the pathFinding resource does is manage a list of waypoints for getNextWaypoint.

Here is my pathManager.cs:

function PathManager::getNextWaypoint(%curPos, %goal, %closed)
{
   return pathGetNextPoint(%curPos, %goal, %closed);
}

function isNodeBlocked(%to, %from)
{
   return false;
}

I then modified the call to getNextWaypoint in player.cs (line 367):

%nextWP = PathManager::getNextWaypoint(%this.getPosition(), %this.curGoal, %this.closedList);
   
%this.closedList.addPoint(%nextWP);
   
%this.setMoveDestination(%nextWP);

Next you have to add after (394):

%this.curGoal = %dest;

This line:

%this.closedList = new NodePath();

Finally, the pathfinding needs to be loaded each map add:

if(!$Pathfinding_Loaded)
{
   pathInit();
   pathSetMaxSlope(100);
   $Pathfinding_Loaded = true;
}

At the end of serverCmdMissionStartPhase3Ack in missionDownload.cs.

Hope that helps.
#3
08/22/2006 (10:45 am)
Thanks for your replies.

Hey Ricky, thanks for your post, that's a huge help.

I have a problem with NavMap initialization though. The game crashes when it tries to build the NavMap.


I've noticed VisualStudio Debugger breaks inside the 'PathFinder::KillNodes' funtion (aipathfinding.cc)
at line
mNodes[i]->deleteObject();

EDIT:

I've commented out the call to KillNodes() and the mission loads successfully. The pathfinding seems to work fine also. I wander thouth what the KillNodes function stand for.
#4
01/08/2007 (3:02 pm)
Love this resource, works like a dream. However i have noticed one problem which im unable to solve. If you position 2 buildings, positioned so the green circles overlap (even if theirs a wide gap between them) - then you have to guide your player through the buildings. By first clicking in between then, then on the opposite side. If you dont, then the player will auto take the long route around all the buildings. Is their a way of setting this resource up, so it detects this gap?

Hope someones already solved this.
#5
01/15/2007 (7:59 am)
Couldn't you just make the green circle smaller? Or replace it with a green square perhaps to fit the building better... not sure if thats possible with the kit or not.
#6
01/18/2007 (11:27 am)
It looks as if the units walk around the green circle... Does the Buldings have bounding box's? if so, can the code not be changed to work off these?
#7
03/08/2007 (1:42 am)
Hi all

I have small problem with implementation it works but when I select player and point somewhere on the terrain player moves always in same direction

this is the console log:

Mapping starting : IssueStop to index :7
Changing command state to : None
Player::setMoveGoal--object(1604) new moving to (-42 -64 246)
1604 - waypoint = 0.000 0.000 0.000

I look in the code it has to be OK but somehow way point is made on 0.000 0.000 0.000 -strange!?
#8
03/27/2007 (9:17 am)
Can anyone help? I also have the same problem as Affan.
#9
04/01/2007 (7:45 pm)
A FEW questions,

this is what i have done:

common\server\missionDownload.cs

this is what my serverCmdMissionStartPhase3Ack function looks like

function serverCmdMissionStartPhase3Ack(%client, %seq)
{
   // Make sure to ignore calls from a previous mission load
   if(%seq != $missionSequence || !$MissionRunning)
      return;
   if(%client.currentPhase != 2)
      return;
   %client.currentPhase = 3;

   // Server is ready to drop into the game
   %client.startMission();
   %client.onClientEnterGame();

//////////////////

if(!$Pathfinding_Loaded)
{
   pathInit();
   pathSetMaxSlope(100);
   $Pathfinding_Loaded = true;
}

///////////////////

}





my pathManager.cs , the source for the file looks like this...
function PathManager::getNextWaypoint(%curPos, %goal, %closed)
{
   return pathGetNextPoint(%curPos, %goal, %closed);
}

function isNodeBlocked(%to, %from)
{
   return false;
}




lastly, my player.cs looks like this

function Player::onReachDestination(%this, %dest)

function Player::onReachDestination(%this, %dest)
{
   if(%this.curGoal $= "")
   {
      echo(%this @ " - Arrived!");
      return;
   }

%nextWP = PathManager::getNextWaypoint(%this.getPosition(), %this.curGoal, %this.closedList);
   
%this.closedList.addPoint(%nextWP);
   
%this.setMoveDestination(%nextWP);


//   %nextWP = PathManager::getNextWaypoint(%this.getPosition(), %this.curGoal);
   
//   %this.setMoveDestination(%nextWP);
   
   if(VectorDist(%nextWP, %this.curGoal) > 0.01)
   {
      echo(%this @ " - waypoint = " @ %nextWP);
   }
   else
   {
      %this.curGoal = "";
   }
}


function Player::setMoveGoal(%this, %dest)

function Player::setMoveGoal(%this, %dest)
{
   %this.curGoal = %dest;

   %this.closedList = new NodePath();
   
   // Kick off the movement.
   %this.onReachDestination(%this.getPosition());
}




with all that, when i now run the game, my units do NOT move in to proper direction, they keep moving to the "north" position, and continue...

any ideas why?


also, since my units keep running north, i can not test to see if pathfinding works or not.....


again, any ideas?