GML mp_grid_path cuts corners



The red square moves towards the green rectangle using mp_grid_path, but it appears that the red square likes cutting corners and walking through the wall. How can I fix this?

This is the creation code of the control object
grid = mp_grid_create(0,0,room_width/32,room_height/32,32,32);
This is the step event of the red square.
player is the name of the green rectangle object.
if target = -1{
    if instance_exists(player){
        target =;

if target = -1{ exit }

tx = (target.x div 32)*32+16
ty = (target.y div 32)*32+16
if mp_grid_path(control.grid,path,x,y,tx,ty,1){
Is there a way to prevent the red square from cutting corners?


In a collision event with the wall, try this:

var dir = point_direction(other.x, other.y, x, y);

x += lenghtdir_x(2, dir);
y += lenghtdir_y(2, dir);
Then it should slide against the wall instead.
Answering just for anyone else who finds this looking for an answer.

The reason the motion planning is cutting corners despite having diagonal movement set to 0 is because you are updating it every frame.
Run your mp_grid_path function and your path_start function only once.

If you run it every frame, you're constantly making a new path every frame. What these functions are doing, is mp_grid_path CREATES a path.
It automatically adds the "Path Points" to the path you tell it to. Then when you do "path_start" your player will move towards it's goal

Below is an example of me clicking into the room, and the player will go to the nearest grid point without cutting corners.
This is because, thanks to the mouse_check_button_pressed event, the two functions will only run one time unless I click again.

    if mouse_check_button_pressed(mb_left)
        target_x = round(mouse_x/64) * 64;
        target_y = round(mouse_y/64) * 64;
        mp_grid_path(grid, path, x, y, target_x, target_y, 0);
        path_start(path, 2, 0, 0);
Spent a while trying to figure this one out, so hopefully this can help someone else out in the future!!