• 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!

GameMaker How to avoid the collision of objects that move along a path?

There are 2 cars (same object) that go through a circuit:

1620686077760.png

Consider that the white part is an intention that they avoid (it would be as if it were a wall). The black part (circuit) would be just illustrative, it is not an object.

In the program the room looks like this:

1620686108924.png

  • Green -> background
  • Red square -> car
  • Other light colored squares -> points that are followed by the cars (to go through the circuit)
  • Black square -> wall

The walls are avoided as follows:

GML:
mp_grid_add_instances(grid,obj_Wall,false);
In addition to the walls, I would like cars to avoid each other. I did it as follows:

Code:
repeat(instance_number(obj_Car)){
    i++;
    
    if(instance_find(obj_Car,i-1).id!=id){
        carOtherID=instance_find(obj_Car,i-1).id;
        mp_grid_add_instances(grid,carOtherID,false);
    }
}
It happens more or less like this:

1620686294719.png

Cars avoid the starting position of other cars, but I would like you to avoid the current position of other cars. For that, I put this part of the code in the step event:

Code:
mp_grid_add_instances(grid,carOtherID,false);
I tried some changes, with some conditions, but none worked. Simply buggy, cars even go through walls (something that shouldn't happen, because I don't even change anything in them).

Documentation:

https://manual-en.yoyogames.com/#t=...m&rhsearch=mp_grid_path&rhhlterm=mp_grid_path
 

Nidoking

Member
Presumably, if you're putting the cars into the grid every step, you're also clearing the grid every step. Are you putting the walls back in after you clear the grid?
 
I haven't looked into this for quite some time, so my memory is a bit hazy.

I don't recall the pathing functions being all that useful for moving objects.

1)
They don't take into consideration the size of the object for actual collisions when calculating paths, and go instead on whether a cell in the grid is free. Doesn't matter to the pathing functions whether that space is actually big enough for the object to follow without passing through things. It just has to find a route there, and does so by testing the centre of a cell. If a wall "hangs" over part of it, but the cell it overlaps is not marked as occupied for whatever reason, then the pathing will go through it, but actual collision_checking will register that the way is not big enough to fit.

Your object is moving on a path, and as far as it's concerned that path is good. As long as the path is set, it will keep trying to follow it, and this is probably why you're seeing it pass through things it shouldn't. Being on the path keeps it moving forward, and negates any adjustments you might be making through checking for collisions. As it will add to an instances x / y value by setting it to points on the path. As long as it's path speed isn't 0, it will automatically force a change in position.

2)
Pathing around moving objects doesn't work with an mp_grid, as the cells constantly have to be re-set and updated. This is quite intensive, and needs more cells of a smaller size if it is to be anywhere near approximating the space an instance occupies. The other pathing functions are going on where an object is, and do not consider where it will be. When they plot a path around, it is on the assumption that the object to be avoided is static.

If a couple of instances try to avoid collision you will generally see them get stuck in a cycle of bumbling into each other. As one moves to avoid the other, the other doesn't know what it's doing, and try's to avoid as well. 99 times out of a 100 they'll end up just going in the other ones way (having made an avoidance path) and then repeat the process.

Generally when the topic was raised about AI, users on the community would suggest looking at implementing flocking behaviour
 
Top