Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

iam trying to implement pathfinding algorithm using PATHFINDING.JS in 3D world using webgl.

iam have made a matrix of 200x200. and placed my enemy(swat) in it .iam confused in implmenting the path. i have tried implementing the path by compparing the value of each array value with swat's position . it works ! but **

  • THE ENEMY KEEPS GOING FROM THE UNWALKABLE AREA OF MY MATRIX....like the enemy should not move from 119,100(x=119,z=100) but its moving from that co-ordinate too .....

can any one help me out in this regard ..

*prob facing :* enemy (swat character keeps moving from the wall /unwalkable area)

wanted solution : enemy does not move from the unwalkable path.. **

function draw()
{
    grid = new PF.Grid(200, 200);

    grid.setWalkableAt( 119,100, false);
    grid.setWalkableAt( 107,100, false);        
    grid.setWalkableAt( 103,104, false);        
    grid.setWalkableAt( 103,100, false);        
    grid.setWalkableAt( 135,100, false);        
    grid.setWalkableAt( 103,120, false);        
    grid.setWalkableAt( 103,112, false);        
    grid.setWalkableAt( 127,100, false);        
    grid.setWalkableAt( 123,100, false);    
    grid.setWalkableAt( 139,100, false);    
    grid.setWalkableAt( 103,124, false);        
    grid.setWalkableAt( 103,128, false);    
    grid.setWalkableAt( 115,100, false);    
    grid.setWalkableAt( 131,100, false);    
    grid.setWalkableAt( 103,116, false);    
    grid.setWalkableAt( 103,108, false);
    grid.setWalkableAt( 111,100, false);    
    grid.setWalkableAt( 103,132, false);

    finder = new PF.AStarFinder();

    f1=Math.abs(first_person_controller.position.x);
    f2=Math.abs(first_person_controller.position.z);

    ff1=Math.round(f1);
    ff2=Math.round(f2);

    s1=Math.abs(swat.position.x);
    s2=Math.abs(swat.position.z);


    ss1=Math.round(s1);
    ss2=Math.round(s1);

    path = finder.findPath(ss1,ss2,ff1,ff2, grid);

    size=path.length-1;

    Ai();
}

function Ai(){
    if (i<size)
    {
        if (swat.position.x >= path[i][0])
        {
            swat.position.x -= 0.3;
            if(Math.floor(swat.position.x) == path[i][0])
            {
                i=i+1;
            }
        }
        else if(swat.position.x <= path[i][0])
        {
            swat.position.x += 0.3;
            if(Math.floor(swat.position.x) == path[i][0])
            {
                i=i+1;
            }
        }
    }

    if (j<size)
    {
        if((Math.abs(swat.position.z)) >= path[j][1])
        {
            swat.position.z -= 0.3;  
            if(Math.floor(Math.abs(swat.position.z)) == path[j][1])
            {
                j=j+1;
            }
        }
        else if((Math.abs(swat.position.z)) <= path[j][1])
        {
            swat.position.z += 0.3;
            if(Math.floor(Math.abs(swat.position.z)) == path[j][1])
            {
                j=j+1;
            }
        }
    }
}
share|improve this question
I think this question is too localized for the site. You should review a known good implementation of A*. Use the debugger to trace your code. – Byte56 Sep 29 '12 at 19:34
yeah i have been tryin that from couple of weeks but failed to get ghe desired results. i want to know if there is any mistake in my logic for moving my enemy character – faiz Sep 29 '12 at 19:53

closed as too localized by Tetrad Oct 16 '12 at 15:34

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

To solve the path finding issue, the common strategy used is:

  1. Generate an array of (vertices/nodes) describing areas the enemies can occupy.
  2. If enemies can move directly between two areas, create a pair (edege) between the two nodes.
  3. Save the Graph data with the level data (do not recalculate it during init phase)
  4. Use Dijkstra to find the best path.

http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

share|improve this answer
thankx for responding Arthur :) – faiz Sep 30 '12 at 10:38
actually many recommended me too use A* ALGO – faiz Sep 30 '12 at 10:39
Then i just found this library to implement path finding...that worked fine for me ...even iam getting the path from my enemy to first person shooter the prob iam facing is that i dont know how to iterate through the values ..iam getting array of values like (140,141) ,(140,142).. & so on – faiz Sep 30 '12 at 10:41
Maybe you can upload a demo to dropbox and add elaborate on the question cause I am not sure we understand why the AI is moving through the walls? That is why I suggested using Dijkstra - it lets you specify where you can walk to from each place. – Arthur Wulf White Sep 30 '12 at 10:50
Basically Dijkstra is pretty similar to A* only slower and since you could (and should) be calculating the paths in advance and saving them as data, I think it would be easier to debug. You can even write tests for it. So basically I suggest to write a simple demo showing the problem, and post it with the code, right now, it seems it would be time consuming to try and narrow it down. – Arthur Wulf White Sep 30 '12 at 10:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.