>>101083935
I've been thinking how to make this better for game programming. I realize I didn't put in a scaling factor, while not strictly necessary it will be hard to balance the FAR_ATTRACTION_PARAMETER and NEAR_REPULSION_PARAMETER, so here's a cleaned up redo of the code with that added in:
FAR_ATTRACTION_PARAMETER = 4 # play with this parameter, higher is that they'll group together faster, lower means slower
NEAR_REPULSION_PARAMETER = 2 # good rule of thumb to leave fixed
SCALING FACTOR = 5 # this should be thought of as in-game units, I recommend you start with 1.5x the width of your enemy
for enemy in enemy_group:
for other_member in enemy_group:
d = distance(enemy, other_member)
aggregation_direction = (other_member.position - enemy.position) / d
scaled_d = d / SCALING_FACTOR
attraction_speed = scaled_d ** FAR_ATTRACTION_PARAMETER
repulsion_speed = scaled_d ** NEAR_REPULSION_PARAMETER
enemy.velocity = (attraction_speed - repulsion_speed) * aggregation_direction