8

Yesterday I came across Craig Reynolds' Boids, and subsequently figured that I'd give implementing a simple 2D version in Java a go. I've put together a fairly basic setup based closely on Conrad Parker's notes.

However, I'm getting some rather bizarre (in my opinion) behaviour. Currently, my boids move reasonably quickly into a rough grid or lattice, and proceed to twitch on the spot. By that I mean they move around a little and rotate very frequently.

Currently, I have implemented:

  1. Alignment
  2. Cohesion
  3. Separation
  4. Velocity limiting

Initially, my boids are randomly distributed across the screen area (slightly different to Parker's method), and their velocities are all directed towards the centre of the screen area (note that randomly initialised velocities give the same result). Changing the velocity limit value only changes how quickly the boids move into this pattern, not formation of the pattern.

As I see it, this could be:

  1. A consequence of the parameters I'm using (right now my code is as described in Parker's pseudocode; I have not yet tried areas of influence defined by an angle and a radius as described by Reynolds.)
  2. Something I need to implement but am not aware of.
  3. Something I am doing wrong.

The expected behaviour would be something more along the lines of a two dimensional version of what happens in the applet on Reynolds' boids page, although right now I haven't implemented any way to keep the boids on screen.

Has anyone encountered this before? Any ideas about the cause and/or how to fix it? I can post a .gif of the behaviour in question if it helps.

Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60

4 Answers4

11

Perhaps your weighting for the separation rule is too strong, causing all the boids to move as far away from all neighboring boids as they can. There are various constants in my pseudocode which act as weights: /100 in rule 1 and /8 in rule 3 (and an implicit *1 in rule 2); these can be tweaked, which is often useful for modelling different behaviors such as closely-swarming insects or gliding birds.

Also the arbitrary |distance| < 100 in the separation rule should be modified to match the units of your simulation; this rule should only apply to boids within close proximity, basically to avoid collisions.

Have fun!

Conrad Parker
  • 854
  • 10
  • 23
  • 1
    Wow! The man himself! Thanks a ton - your pseudocode page was a huge help when getting to grips with understanding how to implement flocking behaviour. I'll have a play with the various weights later today - hopefully it helps. – Iskar Jarak Sep 07 '11 at 05:48
  • My rule two also leads to twitching, but the boids still move. Any other suggestions? I've tried making an inverse squared version of rule 2, but it doesn't help, just increases the frequency and decreases the magnitude of the twitching. – TheTedinator Dec 06 '11 at 04:36
2

If they see everyone, they will all try to move with average velocity. If they see only some there can be some separated groups.

And if they are randomly distributed, it will be close to zero.

If you limit them by rectangle and either repulse them from walls or teleport them to other side when they got close) and have too high separation, they will be pushed from walls (from walls itself or from other who just were teleported, who will then be pushed to other side (and push and be pushed again)).

So try tighter cohesion, limited sight, more space and distribute them clustered (pick random point and place multiple of them small random distance from there), not uniformly or normaly.

Alpedar
  • 1,314
  • 1
  • 8
  • 12
  • As far as I am aware, I should not need to distribute them in clusters. However, I have implementing limiting the range on alignment and cohesion (separation was already done in Parker's pseudo-code). With a fairly small cohesion limit radius, I get the same problem manifesting in a different way - the boids now form clusters and then oscillate as they did before. This is an improvement, at least, since we have clusters (this makes sense, since cohesion is no longer global). – Iskar Jarak Jun 02 '11 at 21:07
  • If it helps, my boids are triangles with a height and width of 30x20, the separation radius is now at 50, the cohesion radius at 75, and the alignment radius at 80. – Iskar Jarak Jun 02 '11 at 21:10
1

I encountered this problem as well. I solved it by making sure that the method for updating each boid's velocity added the new velocity onto the old, instead of resetting it. Essentially, what's happening is this: The boids are trying to move away from each other but can't accelerate (because their velocities are being reset instead of increasing, as they should), thus the "twitching". Your method for updating velocities should look like

def set_velocity(self, dxdy):
    self.velocity = (self.velocity[0] + dxdy[0], self.velocity[1] + dxdy[1])

where velocity and dxdy are 2-tuples.

CSCSH
  • 11
  • 1
  • A good suggestion for something to check, but I assure you that this is not the cause of my problem. I am updating velocity correctly, by setting it to the sum of the current velocity and the calculated acceleration for that boid. – Iskar Jarak Jul 18 '11 at 04:39
0

I wonder if you have a problem with collision rectangles. If you implemented something based on overlapping rectangles (like, say, this), you can end up with the behaviour you describe when two rectangles are close enough that any movement causes them to intersect. (Or even worse if one rectangle can end up totally inside another.)

One solution to this problem is to make sure each boid only looks in a forwards direction. Then you avoid the situation where A cannot move because B is too close in front, but B cannot move because A is too close behind.

A quick check is to actually paint all of your collision rectangles and colour any intersecting ones a different colour. It often gives a clue as to the cause of the stopping and twitching.

Community
  • 1
  • 1
shamp00
  • 11,106
  • 4
  • 38
  • 81
  • A good thought, but I'm not using rectangles. I'm not doing any collision detection, just modelling the boids as points and relying on the separation force applied to prevent collisions from ever happening. As I understand it, that is pretty standard for flocking. – Iskar Jarak Jan 08 '12 at 20:44