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

The Bane of My Existence

B

baconlovar

Guest
Okay, so I've been using Game Maker a lot and I'm getting better and better but there is this one thing I can never seem to do.

And that is collisions in a non-physics world.

And I don't mean collisions as the event, but the collisions where you don't fall through the floor.
I've tried everything from collision_rectangle,circle,place_meeting...etc.

What I'm trying to do is have an enemy follow a player's position but have the enemy's spaced out so they don't walk into each other (I'm using path finding)

It's probably really simple but I've never gotten it down, always worked around it.
~Any help is appreciated
 
B

baconlovar

Guest
Basically, i have multiple enemies all moving to one location, and at some point they all merge together into what looks like one enemy. How do i stop them from overlapping?
 

Mick

Member
This is not an easy task, I have been experimenting with this for a game I'm working on. I have been tackling this by calculating the distance along the path for every enemy to the player. If there is a collision between enemies, the enemy that is further from the player will stop for a second and let the other enemy move.

My system so far works like this: first every enemy object checks for any collision with another character and if so checks collisions with all characters and stops if colliding with another character that is nearer to it's goal. The objects you need to know about for this code are obj_character (any moving character), obj_enemy (obj_character is parent) and obj_hero (obj_character is parent).
global.mpgrid is the mp_grid that has walls setup as forbidden cells.

This is just a start, at the moment the enemies are not trying to find new ways to get to the player, but they will move closer if blocking enemies are destroyed.

Behaves like this:




obj_character create event:
Code:
distance_to_hero = 0;
obj_enemy create event:
Code:
event_inherited();

path = path_add();
distance_to_hero = 5000; // Hero (player) object also need the variable distance_to_hero = 0
path_length = 0;
sp = 0.5;
 
if(mp_grid_path(global.mpgrid, path, x, y, obj_hero.x, obj_hero.y, true))
{
  path_length = path_get_length(path);
  path_start(path, sp, path_action_stop, true);
}
obj_enemy step begin event:
Code:
distance_to_hero = path_length * (1-path_position);
obj_enemy step event:
Code:
if(path_speed != 0) // Is enemy moving?
{
  if(place_meeting(x,y,obj_character)) // Is enemy colliding with other moving character?
  {
    var stop = false;
    with(obj_character) // Cycle through all other character instances
    {
      if(id != other.id) // if not self
      {
        // If distance to hero for this instance is less than the "other" instance and there is a collision, set stop to true
        if(distance_to_hero < other.distance_to_hero && place_meeting(x,y,other.id))
          stop = true;
      }
    }
    if(stop) // Stop enemy and set to continue moving after a second with an alarm
    {
      path_position = path_positionprevious;
      path_speed = 0;
      alarm[0] = 60;
    }
  }
}
obj_enemy step end event (not important):
Code:
depth = -y;
obj_enemy alarm[0] event:
Code:
path_speed = sp;
 
Last edited:
B

baconlovar

Guest
Awesome! thanks, I'm experimenting with it but it's not perfect. What is in that alarm[0]?
 

Mick

Member
Oh, alarm[0] sets the enemy moving again, the code: path_speed = sp; (I added this to my previous post.)
 
Top