I moved to a new URL! Check it out!

Dev Log: Anti-clumping Made Easy

Image


Starting to get back into a bit of a groove on this game thing. One little fun thing to work on was the enemy anti-clumping code. I don't want any enemies to stack on top of each other (although it would be smart of them to do so in order to conceal their numbers.) With a short bit of code I can have enemies push each other away, similar to how babies and rocks push away from each other in Offspring Fling.
public virtual void PushAway() {
if (Hitbox != null) {
if (Overlap(X, Y, (int)Tags.Enemy)) {
var e = Overlapped as Enemy;
if (e.Mass >= Mass) {
var push = new Vector2(X - Overlapped.X, Y - Overlapped.Y);

if (push.X == 0 && push.Y == 0) {
push = new Vector2(Rand.Float(-1, 1), Rand.Float(-1, 1));
}

push.Normalize(pushAwayForce);
pushAwaySpeed += push;
}
}
}

var length = Util.Approach((float)pushAwaySpeed.Length, 0, pushAwayForce * 0.5f);

pushAwaySpeed.Normalize(length);


X += (float)pushAwaySpeed.X * 0.01f;
Y += (float)pushAwaySpeed.Y * 0.01f;
}
Pretty neat! This is actually pretty similar to the original code in the prototype many many years ago, except now it's in fancy C# instead of GML. Enemies will call PushAway() every update.
new comment!

Post your comment!

Name
Email
Comment