Javascript A* path finding ENEMY MOVEMENT in 3D environment

Posted by faiz on Game Development See other posts from Game Development or by faiz
Published on 2012-09-29T19:22:02Z Indexed on 2012/09/29 21:51 UTC
Read the original article Hit count: 313

Filed under:
|
|
|
|

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;
            }
        }
    }
}

© Game Development or respective owner

Related posts about JavaScript

Related posts about ai