Stop same sprite types from touching each other

Trying to think of a solution so that monsters chasing the player can’t colllide with each other, otherwise they all bunch up. I’ve tried things like…

Monster is in collision with Monster → Separate object (Move Monster away from Monster).

Doesn’t do anything, I’m not sure if it ends up counting the monster as colliding with itself and therefore just triggers endlessly.

Tried just checking the collision and then trying to manually bump them away from each other, but I’m not sure how the picking works in that instance because I’m doing something like ‘Monster, move X at angle away from Monster’, which is probably itself again. I’m aware of groups, so I’ve tried ‘Monster in collision with Group’ and then I have the right picks, but I still can’t get them to separate.

Perhaps the major problem here is that they’re all moving along a Pathfinding path to catch the player and the Pathfinding is probably forcing them to move straight back. I have stopped them on the path and it seems like initially they do stop, but soon bunch up again (so that eventually it looks like there’s only 1 of them).

So I also tried adding the monsters themselves as pathfinding obstacles, impassable, but it seems to just ignore it.

Think of the original Gauntlet, when the ghosts chased the player, they couldn’t overlap at all.

Just as a temporary solution, I’m having them chase to a position ‘near’ the player, rather than directly on top, this makes them move to slightly different positions so that I can at least see there are several of them, even though they’re overlapping a bit. Not ideal though.

A possible solution could be to:

  • Use the pathfinding behavior to find a path
  • Disable the pathfinding behavior
  • Use the Boids behavior to move to the first waypoint with the “intent” action
  • The Boids behavior handles the separation (the alignement and cohesion can be disabled if necessary)
  • when the 1st waypoint is almost reached, the 2nd one can be used and so on.

Placing random pathfinding points in a range can help make them look separate. So far there is no simple solution to be able to separate objects that are the same sprite or belong to the group you want to separate from. I have read in the forum options such as attaching an object to each enemy that follows it and separating that object from the rest of the enemies. However, this costs a lot in terms of performance. It would be very helpful to have this (even for me), but I don’t know how applicable it is in Gdevelop. Someone with programming skills can surely provide better information.

How many monsters is it?

Thanks, some helpful responses there. I didn’t know about the Boids behaviour, so I’ll have a look at that.

For now, my botched solution of having them home in on an area NEAR the player, rather than directly onto him works fine and makes them look a bit more random, as if they’re trying to be sneaky or something.

It was just 3 monsters at the moment, with the aim of having more spawn.

I just made this. I don’t know if it can help.
In this case there are 2 loops:

  • one with an x variable that loops the instances
  • another one inside, that loops instances (2nd for each event)

This way, each pair of instances can be analyzed. If distance between them is too small, one moves away (using a pathfinding here, but could be a force maybe…?).

Note that in my case, my moving spite, the one I controle with mouse, is not modifying its path. The other ones are steping aside. That’s why it might not help you with your issue.

A solution could be to move only the moving istance (which mean change the “=/” into “=” : the condition of the 2nd for each event becomes "The variable id of perso = Variable(movinginstance)
But in this case, maybe use a force to move away the sprite instead of pathfinding since the moving would already have a pathfinding, the one related to your game.

Well, all of this is just a try, it’s not as smooth as the “separate objects” event, and would need improvment, and of course adaptation to the game.
It’s just a try of workaround, when need to analyze all pairs among instances of a same object.

If I can find a way of comparing monster v monster and then picking one separately from the other, I could just use separate, but as far as I can tell, there’s no way of doing that. I’m not sure if your idea of a nested loop achieves this?

My first loop doesn’t really pick an object instance , it records the position of one object instance, and then the 2nd loop compares all objects (with exceptions) to this recorded position.

So at he current state my idea cannot make use of “separate 2 objects” action.

Oh I see. That might work though, if I store the x/y to be avoided and then push away any object that goes near that area, though obviously, not the picked object itself.

Is there something in GDevelop to store the ID of a picked object? Construct 3 has a ‘UID’ value that can be stored. That would allow me to store the area to be avoided, along with the ID and then not do the check for the object with that ID on the second loop.

here is the screenshot of my “creating instances” code.
I think this is the easiest to give id’s to instances.
When one is created, it is the one currently picked, so immediately after, you can give it an object variable (called “id” for example).

In my screenshot you’ll see I’ve created 10 instances, and the x variable helps me to give each an id from 1 to 10.

(in this code I also modified all instances speed and accelaration, in order that they move away quickly from the moving instance)

Cheers. I’ll tinker around with it, see how I can apply it to my game :slight_smile:

1 Like