Troubles with pursuit AI.

JacobV

Member
So for my game, I have an enemy type that hunts down the player, and upon establishing a line of sight charges towards them. So far, everything is working fine; except for the AI which actually pursues the player. I've tried various methods; starting with mp_potential_step, which was stupid and choppy. Then I moved to grid-based AI, which has various problems itself; they will only move in the 8 base directions, and tend to overlap into what looks and acts like one entity.

I'd like to use grid-based, but what I have so far doesn't cut it and I am very new to coding top-down AI. Anyone have any ideas on how I can fix these problems?

I've attached some of my code to help.

Code:
// Creation Event

path = path_add();
pos = 1;
xx = path_get_point_x(path,pos)
yy = path_get_point_y(path,pos)
Code:
// Step Event
xx = path_get_point_x(path,pos)
yy = path_get_point_y(path,pos)


if instance_exists(obj_player)
    {
    mp_grid_path(grid,path,x,y,obj_player.x,obj_player.y,1)
    path_set_kind(path, 1);
    turn(point_direction(x,y,obj_player.x,obj_player.y)+90,13)  // This is to handle the image angle, has no impact on the actual movement
    motion_add(point_direction(x,y,xx,yy),1)
    }
 
P

ph101

Guest
While you are creating a path you are not starting it with path start. A typical use of pathfinding is this:

Code:
with (obj_Enemy)
    {
    path = path_add();
    if mp_grid_path(grid, path, x, y, obj_Player.x, obj_Player.y, 1)
       {
       path_start(path, 0, 3, 0);
       }
    }
which I took directly from the manual which you can access by pressing f1 in GM.

You also need to create your grid and add any obstables to your grid before hand. To create your described behaviour of line of site - you will need more than what you have shown no doubt.
 

JacobV

Member
The grid is being created in a different object to improve performance. The reason I am not using path_start is because it restricts how I can customize and smooth the movement.

The object navigates the path to the player well enough; it just doesn't look smooth while doing it.
 
P

ph101

Guest
The object navigates the path to the player well enough; it just doesn't look smooth while doing it.
AH ok I see you are stepping along the path - you must have not posted some of your code? So pos is increased at some point? Yeah this seems a good way. I don;t know what you mean by smooth though. Perhaps you have some lag caused by the fact you are computing the path every step. If its a grid of any reasonable size, this could cause slow down if lots of things are doing it at once. You only really need to do it once when you first detect the target, then you can step through the points of the paths like you are doing, without calculating the path every step which as I say is super slow. I use a similar system except I actually mp_potential_step to go to the points of the path for added lifelike behaviour should anything get int the way (eg other enemies/npcs).
 

JacobV

Member
No, there's no lag, though I will consider your suggestion. The problem, as I stated in the OP, is that the objects overlap and can only move in the eight base directions (up, down, sideways, and diagonally.) This looks really bad and I need some way to make them more smooth in their movements.
 
C

Carbiner

Guest
I think what you want is path_set_precision.
when you create the path, put
Code:
path = path_add();
path_set_precision(path, x);
where x is a number between 0 and 8. This smooths out the path and makes it less choppy. You can find relevant pictures in the GM handbook.
 
Top