• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML mp_grid_path cuts corners

L

Lks10

Guest
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
Code:
grid = mp_grid_create(0,0,room_width/32,room_height/32,32,32);
mp_grid_add_instances(grid,wall,false);
This is the step event of the red square.
player is the name of the green rectangle object.
Code:
if target = -1{
    if instance_exists(player){
        target = player.id;
    }
}

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){
    path_start(path,spd,path_action_stop,true)
}
Is there a way to prevent the red square from cutting corners?
 

TheBroman90

Member
In a collision event with the wall, try this:

Code:
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
perfectly.

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.


GML:
    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!!
 
Top