Jump to content


Photo

Preventing objects from overlapping/bunching up


  • Please log in to reply
3 replies to this topic

#1 Passa

Passa

    GMC Member

  • GMC Member
  • 74 posts
  • Version:Unknown

Posted 26 February 2012 - 02:04 PM

In my game, enemies follow a player object at a speed slower than the player.

It all works fine, but the enemy objects will overlap/stack indefinitely if you circle around. Here is a visual example, forgive my programmer graphics.

Posted Image
This is the enemies moving towards the player with only a little stacking.

Posted Image
Here, the enemies have stacked many times over.

What I need is a method to make them refuse to overlap, or preferably overlap only a little rather than completely. I tried to code a solution myself but it does not work as expected, making them jump around and avoid eachother by large margins. Here is my code (in the step event of obj_killer):

if (distance_to_object(obj_killer) > 0)
    {
    move_towards_point(obj_player.x,obj_player.y,3);
    }
else
    {
    direction = point_direction(x,y,obj_killer.x,obj_killer.y) + 180
    speed = 3
    }

  • 0

#2 RangerX

RangerX

    GMC Member

  • GMC Member
  • 926 posts
  • Version:GM8.1

Posted 26 February 2012 - 02:43 PM

If you don't want them to overlap, the need to treat each other as collisions. Do you have collisions somewhere in your game or some other of your games?
  • 0

#3 creativebunch

creativebunch

    The Bunchiest

  • GMC Member
  • 886 posts
  • Version:GM:Studio

Posted 26 February 2012 - 02:44 PM

I usually do it by having a collision event with itself and then move it away from the colliding instance, here are two ways I've done it:

motion_add(point_direction(other.x, other.y, x, y), 2);
or

var i;
i = point_direction(other.x, other.y, x, y);
x += lengthdir_x(2, i);
y += lengthdir_y(2, i);

  • 1

#4 Passa

Passa

    GMC Member

  • GMC Member
  • 74 posts
  • Version:Unknown

Posted 27 February 2012 - 03:10 AM

Thanks, that second bit of code worked perfectly and the logic is very clear. I feel stupid for not thinking of a similar solution.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users